You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

  1. package jsonschema
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net"
  6. "net/url"
  7. "path/filepath"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. type GrandfatherType struct {
  14. FamilyName string `json:"family_name" jsonschema:"required"`
  15. }
  16. type SomeBaseType struct {
  17. SomeBaseProperty int `json:"some_base_property"`
  18. // The jsonschema required tag is nonsensical for private and ignored properties.
  19. // Their presence here tests that the fields *will not* be required in the output
  20. // schema, even if they are tagged required.
  21. somePrivateBaseProperty string `json:"i_am_private" jsonschema:"required"`
  22. SomeIgnoredBaseProperty string `json:"-" jsonschema:"required"`
  23. SomeSchemaIgnoredProperty string `jsonschema:"-,required"`
  24. Grandfather GrandfatherType `json:"grand"`
  25. SomeUntaggedBaseProperty bool `jsonschema:"required"`
  26. someUnexportedUntaggedBaseProperty bool
  27. }
  28. type nonExported struct {
  29. PublicNonExported int
  30. privateNonExported int
  31. }
  32. type ProtoEnum int32
  33. func (ProtoEnum) EnumDescriptor() ([]byte, []int) { return []byte(nil), []int{0} }
  34. const (
  35. Unset ProtoEnum = iota
  36. Great
  37. )
  38. type TestUser struct {
  39. SomeBaseType
  40. nonExported
  41. ID int `json:"id" jsonschema:"required"`
  42. Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20"`
  43. Friends []int `json:"friends,omitempty"`
  44. Tags map[string]interface{} `json:"tags,omitempty"`
  45. TestFlag bool
  46. IgnoredCounter int `json:"-"`
  47. // Tests for RFC draft-wright-json-schema-validation-00, section 7.3
  48. BirthDate time.Time `json:"birth_date,omitempty"`
  49. Website url.URL `json:"website,omitempty"`
  50. IPAddress net.IP `json:"network_address,omitempty"`
  51. // Tests for RFC draft-wright-json-schema-hyperschema-00, section 4
  52. Photo []byte `json:"photo,omitempty" jsonschema:"required"`
  53. // Tests for jsonpb enum support
  54. Feeling ProtoEnum `json:"feeling,omitempty"`
  55. Age int `json:"age" jsonschema:"minimum=18,maximum=120,exclusiveMaximum=true,exclusiveMinimum=true"`
  56. Email string `json:"email" jsonschema:"format=email"`
  57. }
  58. var schemaGenerationTests = []struct {
  59. reflector *Reflector
  60. fixture string
  61. }{
  62. {&Reflector{}, "fixtures/defaults.json"},
  63. {&Reflector{AllowAdditionalProperties: true}, "fixtures/allow_additional_props.json"},
  64. {&Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/required_from_jsontags.json"},
  65. {&Reflector{ExpandedStruct: true}, "fixtures/defaults_expanded_toplevel.json"},
  66. }
  67. func TestSchemaGeneration(t *testing.T) {
  68. for _, tt := range schemaGenerationTests {
  69. name := strings.TrimSuffix(filepath.Base(tt.fixture), ".json")
  70. t.Run(name, func(t *testing.T) {
  71. f, err := ioutil.ReadFile(tt.fixture)
  72. if err != nil {
  73. t.Errorf("ioutil.ReadAll(%s): %s", tt.fixture, err)
  74. return
  75. }
  76. actualSchema := tt.reflector.Reflect(&TestUser{})
  77. expectedSchema := &Schema{}
  78. if err := json.Unmarshal(f, expectedSchema); err != nil {
  79. t.Errorf("json.Unmarshal(%s, %v): %s", tt.fixture, expectedSchema, err)
  80. return
  81. }
  82. if !reflect.DeepEqual(actualSchema, expectedSchema) {
  83. actualJSON, err := json.MarshalIndent(actualSchema, "", " ")
  84. if err != nil {
  85. t.Errorf("json.MarshalIndent(%v, \"\", \" \"): %v", actualSchema, err)
  86. return
  87. }
  88. t.Errorf("reflector %+v wanted schema %s, got %s", tt.reflector, f, actualJSON)
  89. }
  90. })
  91. }
  92. }