Skip to content

Commit

Permalink
Merge pull request #168 from fredbi/fix-148
Browse files Browse the repository at this point in the history
fixed json unmarshal for BoolOrSchema
  • Loading branch information
casualjim committed Dec 1, 2023
2 parents 8b1afa6 + 32e224f commit 95bb41d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
31 changes: 31 additions & 0 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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#")
Expand Down
10 changes: 10 additions & 0 deletions fixtures/bugs/schema-148.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"definitions": {
"empty": {
"additionalProperties": {}
},
"false": {
"additionalProperties": false
}
}
}
4 changes: 2 additions & 2 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ 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 {
return err
}
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
Expand Down

0 comments on commit 95bb41d

Please sign in to comment.