From abdca6cb88a70ffa6faee4cd9fd8a0f830b45e51 Mon Sep 17 00:00:00 2001 From: Ori Shalom Date: Sun, 22 Jan 2023 21:09:51 +0200 Subject: [PATCH] fix additional properties false not validated (#747) --- openapi3/issue746_test.go | 26 ++++++++++++++++++++++++++ openapi3/schema.go | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 openapi3/issue746_test.go diff --git a/openapi3/issue746_test.go b/openapi3/issue746_test.go new file mode 100644 index 000000000..390a34848 --- /dev/null +++ b/openapi3/issue746_test.go @@ -0,0 +1,26 @@ +package openapi3 + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIssue746(t *testing.T) { + schema := &Schema{} + err := schema.UnmarshalJSON([]byte(`{"additionalProperties": false}`)) + require.NoError(t, err) + + var value interface{} + err = json.Unmarshal([]byte(`{"foo": "bar"}`), &value) + require.NoError(t, err) + + err = schema.VisitJSON(value) + require.Error(t, err) + + schemaErr := &SchemaError{} + require.ErrorAs(t, err, &schemaErr) + require.Equal(t, "properties", schemaErr.SchemaField) + require.Equal(t, `property "foo" is unsupported`, schemaErr.Reason) +} diff --git a/openapi3/schema.go b/openapi3/schema.go index 892f32541..fbf1900db 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -810,7 +810,7 @@ func (schema *Schema) IsEmpty() bool { if ap := schema.AdditionalProperties.Schema; ap != nil && !ap.Value.IsEmpty() { return false } - if apa := schema.AdditionalProperties.Has; apa != nil && *apa { + if apa := schema.AdditionalProperties.Has; apa != nil && !*apa { return false } if items := schema.Items; items != nil && !items.Value.IsEmpty() {