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

Fix additionalProperties loading #338

Merged
merged 1 commit into from
Jul 7, 2022
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
3 changes: 2 additions & 1 deletion spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
return err
}

additionalPropertiesValue := rawFields["additionalProperties"]

for _, supportedField := range supportedSchemaFields {
delete(rawFields, supportedField)
}
Expand All @@ -153,7 +155,6 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
}
*s = Schema(inner)

additionalPropertiesValue := rawFields["additional_properties"]
additionalPropertiesBool, ok := additionalPropertiesValue.(bool)

// AdditionalProperties can be a `false` or `Schema` object for convenience turn
Expand Down
46 changes: 46 additions & 0 deletions spec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,52 @@ func TestUnmarshal_Simple(t *testing.T) {
assert.Equal(t, "string", schema.Type)
}

func TestUnmarshal_ObjectWithAdditionalProperties(t *testing.T) {
data := []byte(`{
"type": "object",
"additionalProperties": {
"properties": {
"prop": {
"type": "number"
}
},
"type": "object"
}
}`)
var schema Schema
err := json.Unmarshal(data, &schema)
assert.NoError(t, err)
assert.Equal(t, "object", schema.Type)
assert.True(t, schema.AdditionalPropertiesAllowed)
assert.Equal(t, "object", schema.AdditionalProperties.Type)
assert.Equal(t, 1, len(schema.AdditionalProperties.Properties))
}

func TestUnmarshal_ObjectWithAdditionalPropertiesFalse(t *testing.T) {
data := []byte(`{
"type": "object",
"additionalProperties": false
}`)
var schema Schema
err := json.Unmarshal(data, &schema)
assert.NoError(t, err)
assert.Equal(t, "object", schema.Type)
assert.False(t, schema.AdditionalPropertiesAllowed)
assert.Nil(t, schema.AdditionalProperties)
}

func TestUnmarshal_ObjectWithAdditionalPropertiesDefault(t *testing.T) {
data := []byte(`{
"type": "object"
}`)
var schema Schema
err := json.Unmarshal(data, &schema)
assert.NoError(t, err)
assert.Equal(t, "object", schema.Type)
assert.True(t, schema.AdditionalPropertiesAllowed)
assert.Nil(t, schema.AdditionalProperties)
}

func TestUnmarshal_UnsupportedField(t *testing.T) {
// We don't support 'const'
data := []byte(`{const: "hello"}`)
Expand Down