diff --git a/README.md b/README.md index fe0e2b8..8a2c0d1 100644 --- a/README.md +++ b/README.md @@ -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"` } ``` @@ -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"] diff --git a/examples_test.go b/examples_test.go index 871491e..82cae05 100644 --- a/examples_test.go +++ b/examples_test.go @@ -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"` } @@ -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": [ diff --git a/fixtures/oneof_ref.json b/fixtures/oneof_ref.json new file mode 100644 index 0000000..ca69413 --- /dev/null +++ b/fixtures/oneof_ref.json @@ -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" + } + } +} \ No newline at end of file diff --git a/fixtures/test_config.json b/fixtures/test_config.json index 97149b8..d879a53 100644 --- a/fixtures/test_config.json +++ b/fixtures/test_config.json @@ -20,4 +20,4 @@ ] } } -} +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 5fc1dba..94f4067 100644 --- a/reflect.go +++ b/reflect.go @@ -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) diff --git a/reflect_test.go b/reflect_test.go index 76b2a2f..e727281 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -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{}) +}