Skip to content
Open
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
27 changes: 27 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, p
}
extras := strings.Split(f.Tag.Get("jsonschema_extras"), ",")
t.extraKeywords(extras)

allOfIfs := splitOnUnescapedCommas(f.Tag.Get("jsonschema_allof_type"))
parent.addAllOfConditions(allOfIfs, propertyName)
}

// read struct tags for generic keyworks
Expand Down Expand Up @@ -853,6 +856,30 @@ func (t *Schema) arrayKeywords(tags []string) {
}
}

func (t *Schema) addAllOfConditions(conditions []string, propertyName string) {
if len(t.AllOf) == 0 {
t.AllOf = make([]*Schema, 0)
}
// tag Format: <field_name>:<test_value>=/path/my/type
for _, condition := range conditions {
parts := strings.Split(condition, ":")
if len(parts) != 2 {
continue
}
conditionPart := strings.Split(strings.TrimSpace(parts[0]), "=")
if len(conditionPart) != 2 {
continue
}
ruleSchema := Schema{
If: &Schema{Properties: orderedmap.New()},
Then: &Schema{Properties: orderedmap.New()},
}
ruleSchema.If.Properties.Set(conditionPart[0], Schema{Const: conditionPart[1]})
ruleSchema.Then.Properties.Set(propertyName, Schema{Ref: parts[1]})
t.AllOf = append(t.AllOf, &ruleSchema)
}
}

func (t *Schema) extraKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
Expand Down
30 changes: 30 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,33 @@ func TestArrayFormat(t *testing.T) {
pt := p.Items.Format
require.Equal(t, pt, "uri")
}

func TestAllOfType(t *testing.T) {
type Application struct {
Name string `json:"name"`
Kind string `json:"kind"`
Specification json.RawMessage `json:"spec" jsonschema_allof_type:"kind=docker:#/$defs/dockerApp, kind=vm:#/$defs/vmApp"`
}

r := new(Reflector)
appSchema := r.Reflect(&Application{})

if appSchema.Definitions == nil {
appSchema.Definitions = make(map[string]*Schema, 0)
}

require.Len(t, appSchema.Definitions, 1)
require.NotNil(t, appSchema.Definitions["Application"])
require.Len(t, appSchema.Definitions["Application"].AllOf, 2)

// Check IF part
allOfDocker := appSchema.Definitions["Application"].AllOf[0]
jsonDocker, _ := allOfDocker.MarshalJSON()
dockerIfMustBe := `{"if":{"properties":{"kind":{"const":"docker"}}},"then":{"properties":{"spec":{"$ref":"#/$defs/dockerApp"}}}}`
require.Equal(t, dockerIfMustBe, string(jsonDocker))

allOfVM := appSchema.Definitions["Application"].AllOf[1]
jsonVM, _ := allOfVM.MarshalJSON()
vmIfMustBe := `{"if":{"properties":{"kind":{"const":"vm"}}},"then":{"properties":{"spec":{"$ref":"#/$defs/vmApp"}}}}`
require.Equal(t, vmIfMustBe, string(jsonVM))
}