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 3 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:"ipAddress2,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses2 []interface{} `json:"ipAddresses2,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{})
}
]
},
"ipAddress2": {
"anyOf": [
{
"$ref": "#/$defs/ipv4"
},
{
"$ref": "#/$defs/ipv6"
}
]
},
"ipAddresses2": {
"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:"ipAddress2,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"`
IPAddresses2 []interface{} `json:"ipAddresses2,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() {
// }
// ]
// },
// "ipAddress2": {
// "anyOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "ipAddresses2": {
// "items": {
// "anyOf": [
// {
// "$ref": "#/$defs/ipv4"
// },
// {
// "$ref": "#/$defs/ipv6"
// }
// ]
// },
// "type": "array"
// },
// "fav_color": {
// "type": "string",
// "enum": [
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