Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed json unmarshal for BoolOrSchema #168

Merged
merged 1 commit into from
Dec 1, 2023
Merged
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
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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allocates

}
*s = nw
return nil
Expand Down
Loading