diff --git a/expander_test.go b/expander_test.go index 471b567..087dfab 100644 --- a/expander_test.go +++ b/expander_test.go @@ -42,6 +42,37 @@ var ( specs = filepath.Join("fixtures", "specs") ) +func TestExpand_Issue148(t *testing.T) { + fp := filepath.Join("fixtures", "bugs", "schema-148.json") + b, err := jsonDoc(fp) + require.NoError(t, err) + + assertAdditionalProps := func(sp Swagger) func(*testing.T) { + return func(t *testing.T) { + require.Len(t, sp.Definitions, 2) + + require.Contains(t, sp.Definitions, "empty") + empty := sp.Definitions["empty"] + require.NotNil(t, empty.AdditionalProperties) + require.NotNil(t, empty.AdditionalProperties.Schema) + require.True(t, empty.AdditionalProperties.Allows) + + require.Contains(t, sp.Definitions, "false") + additionalIsFalse := sp.Definitions["false"] + require.NotNil(t, additionalIsFalse.AdditionalProperties) + require.Nil(t, additionalIsFalse.AdditionalProperties.Schema) + require.False(t, additionalIsFalse.AdditionalProperties.Allows) + } + } + + var sp Swagger + require.NoError(t, json.Unmarshal(b, &sp)) + t.Run("check additional properties", assertAdditionalProps(sp)) + + require.NoError(t, ExpandSpec(&sp, nil)) + t.Run("check additional properties after expansion", assertAdditionalProps(sp)) +} + func TestExpand_KnownRef(t *testing.T) { // json schema draft 4 meta schema is embedded by default: it resolves without setup schema := RefProperty("http://json-schema.org/draft-04/schema#") diff --git a/fixtures/bugs/schema-148.json b/fixtures/bugs/schema-148.json new file mode 100644 index 0000000..a233257 --- /dev/null +++ b/fixtures/bugs/schema-148.json @@ -0,0 +1,10 @@ +{ + "definitions": { + "empty": { + "additionalProperties": {} + }, + "false": { + "additionalProperties": false + } + } +} diff --git a/swagger.go b/swagger.go index 44722ff..1590fd1 100644 --- a/swagger.go +++ b/swagger.go @@ -253,7 +253,7 @@ func (s SchemaOrBool) MarshalJSON() ([]byte, error) { // UnmarshalJSON converts this bool or schema object from a JSON structure func (s *SchemaOrBool) UnmarshalJSON(data []byte) error { var nw SchemaOrBool - if len(data) >= 4 { + if len(data) > 0 { if data[0] == '{' { var sch Schema if err := json.Unmarshal(data, &sch); err != nil { @@ -261,7 +261,7 @@ func (s *SchemaOrBool) UnmarshalJSON(data []byte) error { } nw.Schema = &sch } - nw.Allows = !(data[0] == 'f' && data[1] == 'a' && data[2] == 'l' && data[3] == 's' && data[4] == 'e') + nw.Allows = !bytes.Equal(data, []byte("false")) } *s = nw return nil