Skip to content

Commit

Permalink
Adding anyof_ref tag, moving oneof_ref out of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Sep 6, 2023
1 parent 88a348b commit 4c3020b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 51 deletions.
25 changes: 0 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type TestUser struct {
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
IPAddress interface{} `json:"ip_address,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses []interface{} `json:"ip_addresses,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
}
```
Expand Down Expand Up @@ -106,29 +104,6 @@ jsonschema.Reflect(&TestUser{})
}
]
},
"ip_address": {
"oneOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"ip_addresses": {
"items": {
"oneOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"type": "array"
},
"fav_color": {
"type": "string",
"enum": ["red", "green", "blue"]
Expand Down
25 changes: 0 additions & 25 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type SampleUser struct {
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
IPAddress interface{} `json:"ip_address,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses []interface{} `json:"ip_addresses,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
}

Expand Down Expand Up @@ -95,29 +93,6 @@ func ExampleReflect() {
// }
// ]
// },
// "ip_address": {
// "oneOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "ip_addresses": {
// "items": {
// "oneOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "type": "array"
// },
// "fav_color": {
// "type": "string",
// "enum": [
Expand Down
59 changes: 59 additions & 0 deletions fixtures/oneof_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/server",
"$ref": "#/$defs/Server",
"$defs": {
"Server": {
"properties": {
"ip_address": {
"oneOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"ip_addresses": {
"items": {
"oneOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"type": "array"
},
"ip_address_any": {
"anyOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"ip_addresses_any": {
"items": {
"anyOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"type": "array"
}
},
"additionalProperties": false,
"type": "object"
}
}
}
2 changes: 1 addition & 1 deletion fixtures/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
]
}
}
}
}
15 changes: 15 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,21 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
Type: ty,
})
}
case "anyof_ref":
subSchema := t
if t.Items != nil {
subSchema = t.Items
}
if subSchema.AnyOf == nil {
subSchema.AnyOf = make([]*Schema, 0, 1)
}
subSchema.Ref = ""
refs := strings.Split(nameValue[1], ";")
for _, r := range refs {
subSchema.AnyOf = append(subSchema.AnyOf, &Schema{
Ref: r,
})
}
case "anyof_type":
if t.AnyOf == nil {
t.AnyOf = make([]*Schema, 0, 1)
Expand Down
12 changes: 12 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,15 @@ func TestFieldNameTag(t *testing.T) {
}
compareSchemaOutput(t, "fixtures/test_config.json", &r, &Config{})
}

func TestFieldOneOfRef(t *testing.T) {
type Server struct {
IPAddress interface{} `json:"ip_address,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses []interface{} `json:"ip_addresses,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddressAny interface{} `json:"ip_address_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddressesAny []interface{} `json:"ip_addresses_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
}

r := &Reflector{}
compareSchemaOutput(t, "fixtures/oneof_ref.json", r, &Server{})
}

0 comments on commit 4c3020b

Please sign in to comment.