Skip to content

Commit

Permalink
Fix additionalProperties loading (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym-stripe authored Jul 7, 2022
1 parent 3efcf47 commit a059f8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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

0 comments on commit a059f8e

Please sign in to comment.