Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for anyof_ref #72

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
IPAddress2 interface{} `json:"ip_address2,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are important enough to stick in the README. I think the best way forward here is with some custom tests inside the refect_test.go file.

IPAddresses2 []interface{} `json:"ip_addresses2,omitempty" jsonschema:"anyof_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_address2": {
"anyOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"ip_addresses2": {
"items": {
"anyOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"type": "array"
},
"fav_color": {
"type": "string",
"enum": ["red", "green", "blue"]
Expand Down
41 changes: 33 additions & 8 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
)

type SampleUser struct {
ID int `json:"id"`
Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
Tags map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
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"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
ID int `json:"id"`
Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
Tags map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
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"`
IPAddress2 interface{} `json:"ip_address2,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses2 []interface{} `json:"ip_addresses2,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
}

func ExampleReflect() {
Expand Down Expand Up @@ -93,6 +95,29 @@ func ExampleReflect() {
// }
// ]
// },
// "ip_address2": {
// "anyOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "ip_addresses2": {
// "items": {
// "anyOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "type": "array"
// },
// "fav_color": {
// "type": "string",
// "enum": [
Expand Down
13 changes: 13 additions & 0 deletions fixtures/anyof.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@
},
"child": {
"$ref": "#/$defs/ChildAnyOf"
},
"field6": {
"anyOf": [
{
"$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 @@ -720,6 +720,21 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
parent.AnyOf = append(parent.AnyOf, typeFound)
}
typeFound.Required = append(typeFound.Required, propertyName)
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 "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 @@ -153,6 +153,7 @@ type RootAnyOf struct {
Field3 interface{} `json:"field3" jsonschema:"anyof_type=string;array"`
Field4 string `json:"field4" jsonschema:"anyof_required=group1"`
Field5 ChildAnyOf `json:"child"`
Field6 interface{} `json:"field6" jsonschema:"anyof_ref=Outer;OuterNamed;OuterPtr"`
}

type ChildAnyOf struct {
Expand Down