diff --git a/spec/spec.go b/spec/spec.go index e1a7fae5..40591324 100644 --- a/spec/spec.go +++ b/spec/spec.go @@ -133,6 +133,8 @@ func (s *Schema) UnmarshalJSON(data []byte) error { return err } + additionalPropertiesValue := rawFields["additionalProperties"] + for _, supportedField := range supportedSchemaFields { delete(rawFields, supportedField) } @@ -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 diff --git a/spec/spec_test.go b/spec/spec_test.go index 5691be32..da6e297f 100644 --- a/spec/spec_test.go +++ b/spec/spec_test.go @@ -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"}`)