Skip to content

Commit

Permalink
Merge pull request #69 from gmlewis/i68-oneof-ref
Browse files Browse the repository at this point in the history
Add support for oneof_ref
  • Loading branch information
samlown committed Sep 6, 2023
2 parents aadda03 + 28cc7fb commit 88a348b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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 All @@ -49,7 +51,8 @@ jsonschema.Reflect(&TestUser{})

```json
{
"$schema": "http://json-schema.org/draft/2020-12/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema_test/sample-user",
"$ref": "#/$defs/SampleUser",
"$defs": {
"SampleUser": {
Expand Down Expand Up @@ -103,6 +106,29 @@ 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: 25 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ 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 @@ -93,6 +95,29 @@ 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
13 changes: 13 additions & 0 deletions fixtures/oneof.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@
},
"child": {
"$ref": "#/$defs/ChildOneOf"
},
"field6": {
"oneOf": [
{
"$ref": "Outer"
},
{
"$ref": "OuterNamed"
},
{
"$ref": "OuterPtr"
}
]
}
},
"additionalProperties": false,
Expand Down
15 changes: 15 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,21 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
parent.AnyOf = append(parent.AnyOf, typeFound)
}
typeFound.Required = append(typeFound.Required, propertyName)
case "oneof_ref":
subSchema := t
if t.Items != nil {
subSchema = t.Items
}
if subSchema.OneOf == nil {
subSchema.OneOf = make([]*Schema, 0, 1)
}
subSchema.Ref = ""
refs := strings.Split(nameValue[1], ";")
for _, r := range refs {
subSchema.OneOf = append(subSchema.OneOf, &Schema{
Ref: r,
})
}
case "oneof_type":
if t.OneOf == nil {
t.OneOf = make([]*Schema, 0, 1)
Expand Down
1 change: 1 addition & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type RootOneOf struct {
Field3 interface{} `json:"field3" jsonschema:"oneof_type=string;array"`
Field4 string `json:"field4" jsonschema:"oneof_required=group1"`
Field5 ChildOneOf `json:"child"`
Field6 interface{} `json:"field6" jsonschema:"oneof_ref=Outer;OuterNamed;OuterPtr"`
}

type ChildOneOf struct {
Expand Down

0 comments on commit 88a348b

Please sign in to comment.