Skip to content

Commit

Permalink
Support additionalProperties in root types (a-h#48)
Browse files Browse the repository at this point in the history
* Run gofmt

* Support additionalProperties in root types
  • Loading branch information
antoineco authored and a-h committed Jun 29, 2018
1 parent de24bf4 commit 9a38bbc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (g *Generator) CreateTypes() (structs map[string]Struct, aliases map[string
for _, typeKey := range getOrderedKeyNamesFromSchemaMap(types) {
v := types[typeKey]

if v.TypeValue == "object" || v.TypeValue == nil {
if (v.TypeValue == "object" || v.TypeValue == nil) && len(v.Properties) > 0 {
s, errtype := createStruct(typeKey, v, types)
if errtype != nil {
errs = append(errs, errtype...)
Expand Down
24 changes: 24 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func TestEmptyNestedStructGeneration(t *testing.T) {
root.Properties = map[string]*jsonschema.Schema{
"property1": {
TypeValue: "object",
Properties: map[string]*jsonschema.Schema{
"nestedproperty1": {TypeValue: "string"},
},
},
}

Expand Down Expand Up @@ -714,6 +717,9 @@ func TestTypeAliases(t *testing.T) {
Items: &jsonschema.Schema{
TypeValue: "object",
Title: "foo",
Properties: map[string]*jsonschema.Schema{
"nestedproperty": {TypeValue: "string"},
},
}},
structs: 1,
aliases: 1,
Expand All @@ -724,6 +730,24 @@ func TestTypeAliases(t *testing.T) {
structs: 0,
aliases: 1,
},
{
gotype: "map[string]string",
input: &jsonschema.Schema{
TypeValue: "object",
AdditionalProperties: []*jsonschema.Schema{{TypeValue: "string"}},
},
structs: 0,
aliases: 1,
},
{
gotype: "map[string]interface{}",
input: &jsonschema.Schema{
TypeValue: "object",
AdditionalProperties: []*jsonschema.Schema{{TypeValue: []interface{}{"string", "integer"}}},
},
structs: 0,
aliases: 1,
},
}

for _, test := range tests {
Expand Down
15 changes: 11 additions & 4 deletions jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type Schema struct {
// Properties, Required and AdditionalProperties describe an object's child instances.
// http:https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5
Properties map[string]*Schema

// Default value is applicable to a single sub-instance
// http:https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.10.2
Default interface{} `json:"default"`
Required []string
AdditionalProperties AdditionalProperties

// Default can be used to supply a default JSON value associated with a particular schema.
// http:https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.10.2
Default interface{}

// Reference is a URI reference to a schema.
// http:https://json-schema.org/draft-07/json-schema-core.html#rfc.section.8
Reference string `json:"$ref"`
Expand Down Expand Up @@ -130,6 +130,13 @@ func addTypeAndChildrenToMap(path string, name string, s *Schema, types map[stri
return
}

// Add root schemas composed of an object type without property.
// The resulting type depends on the presence of additionalProperties.
if (t == "object" || t == "") && len(s.Properties) == 0 && path == "#" {
types[path] = s
return
}

// Add root schemas composed only of a simple type
if !(t == "object" || t == "") && path == "#" {
types[path] = s
Expand Down

0 comments on commit 9a38bbc

Please sign in to comment.