Skip to content

Commit

Permalink
Merge pull request #46 from deepjyoti30/feat/add-anyof-support
Browse files Browse the repository at this point in the history
Add support for `anyof_required` and `anyof_type`
  • Loading branch information
samlown authored Nov 1, 2022
2 parents 679d2eb + ee500fb commit 55c890f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
91 changes: 91 additions & 0 deletions fixtures/anyof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/root-any-of",
"$ref": "#/$defs/RootAnyOf",
"$defs": {
"ChildAnyOf": {
"anyOf": [
{
"required": [
"child1",
"child4"
],
"title": "group1"
},
{
"required": [
"child2",
"child3"
],
"title": "group2"
}
],
"properties": {
"child1": {
"type": "string"
},
"child2": {
"type": "string"
},
"child3": {
"oneOf": [
{
"type": "string"
},
{
"type": "array"
}
]
},
"child4": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object"
},
"RootAnyOf": {
"anyOf": [
{
"required": [
"field1",
"field4"
],
"title": "group1"
},
{
"required": [
"field2"
],
"title": "group2"
}
],
"properties": {
"field1": {
"type": "string"
},
"field2": {
"type": "string"
},
"field3": {
"anyOf": [
{
"type": "string"
},
{
"type": "array"
}
]
},
"field4": {
"type": "string"
},
"child": {
"$ref": "#/$defs/ChildAnyOf"
}
},
"additionalProperties": false,
"type": "object"
}
}
}
26 changes: 26 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,21 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
parent.OneOf = append(parent.OneOf, typeFound)
}
typeFound.Required = append(typeFound.Required, propertyName)
case "anyof_required":
var typeFound *Schema
for i := range parent.AnyOf {
if parent.AnyOf[i].Title == nameValue[1] {
typeFound = parent.AnyOf[i]
}
}
if typeFound == nil {
typeFound = &Schema{
Title: nameValue[1],
Required: []string{},
}
parent.AnyOf = append(parent.AnyOf, typeFound)
}
typeFound.Required = append(typeFound.Required, propertyName)
case "oneof_type":
if t.OneOf == nil {
t.OneOf = make([]*Schema, 0, 1)
Expand All @@ -694,6 +709,17 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
Type: ty,
})
}
case "anyof_type":
if t.AnyOf == nil {
t.AnyOf = make([]*Schema, 0, 1)
}
t.Type = ""
types := strings.Split(nameValue[1], ";")
for _, ty := range types {
t.AnyOf = append(t.AnyOf, &Schema{
Type: ty,
})
}
case "enum":
switch t.Type {
case "string":
Expand Down
16 changes: 16 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ type ChildOneOf struct {
Child4 string `json:"child4" jsonschema:"oneof_required=group1"`
}

type RootAnyOf struct {
Field1 string `json:"field1" jsonschema:"anyof_required=group1"`
Field2 string `json:"field2" jsonschema:"anyof_required=group2"`
Field3 interface{} `json:"field3" jsonschema:"anyof_type=string;array"`
Field4 string `json:"field4" jsonschema:"anyof_required=group1"`
Field5 ChildAnyOf `json:"child"`
}

type ChildAnyOf struct {
Child1 string `json:"child1" jsonschema:"anyof_required=group1"`
Child2 string `json:"child2" jsonschema:"anyof_required=group2"`
Child3 interface{} `json:"child3" jsonschema:"anyof_required=group2,oneof_type=string;array"`
Child4 string `json:"child4" jsonschema:"anyof_required=group1"`
}

type Text string

type TextNamed string
Expand Down Expand Up @@ -336,6 +351,7 @@ func TestSchemaGeneration(t *testing.T) {
{&TestUser{}, &Reflector{DoNotReference: true}, "fixtures/no_reference.json"},
{&TestUser{}, &Reflector{DoNotReference: true, AssignAnchor: true}, "fixtures/no_reference_anchor.json"},
{&RootOneOf{}, &Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/oneof.json"},
{&RootAnyOf{}, &Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/anyof.json"},
{&CustomTypeField{}, &Reflector{
Mapper: func(i reflect.Type) *Schema {
if i == reflect.TypeOf(CustomTime{}) {
Expand Down

0 comments on commit 55c890f

Please sign in to comment.