From 20900537f0b979960db863f7f150ebcf6a4ad6cb Mon Sep 17 00:00:00 2001 From: Anatoly Rugalev Date: Wed, 3 Jul 2024 23:37:04 +0200 Subject: [PATCH 1/2] test: add a test for additionalProperties: false validation --- openapi3/issue82_test.go | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 openapi3/issue82_test.go diff --git a/openapi3/issue82_test.go b/openapi3/issue82_test.go new file mode 100644 index 00000000..165fb997 --- /dev/null +++ b/openapi3/issue82_test.go @@ -0,0 +1,102 @@ +package openapi3 + +import ( + "encoding/json" + "github.com/stretchr/testify/require" + "testing" +) + +func TestIssue82(t *testing.T) { + payload := map[string]interface{}{ + "prop1": "val", + "prop3": "val", + } + + schemas := []string{` +{ + "type": "object", + "additionalProperties": false, + "required": ["prop1"], + "properties": { + "prop1": { + "type": "string" + } + } +}`, `{ + "anyOf": [ + { + "type": "object", + "additionalProperties": false, + "required": ["prop1"], + "properties": { + "prop1": { + "type": "string" + } + } + }, + { + "type": "object", + "additionalProperties": false, + "properties": { + "prop2": { + "type": "string" + } + } + } + ] +}`, `{ + "oneOf": [ + { + "type": "object", + "additionalProperties": false, + "required": ["prop1"], + "properties": { + "prop1": { + "type": "string" + } + } + }, + { + "type": "object", + "additionalProperties": false, + "properties": { + "prop2": { + "type": "string" + } + } + } + ] +}`, `{ + "allOf": [ + { + "type": "object", + "additionalProperties": false, + "required": ["prop1"], + "properties": { + "prop1": { + "type": "string" + } + } + }, + { + "type": "object", + "additionalProperties": false, + "properties": { + "prop2": { + "type": "string" + } + } + } + ] + } +`} + + for _, jsonSchema := range schemas { + var dataSchema Schema + err := json.Unmarshal([]byte(jsonSchema), &dataSchema) + require.NoError(t, err) + + err = dataSchema.VisitJSON(payload) + require.Error(t, err) + } +} From 39a4972e676a311bdca1604d9914f9072739340c Mon Sep 17 00:00:00 2001 From: Anatoly Rugalev Date: Thu, 4 Jul 2024 01:06:19 +0200 Subject: [PATCH 2/2] goimports --- openapi3/issue82_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openapi3/issue82_test.go b/openapi3/issue82_test.go index 165fb997..3bf242bc 100644 --- a/openapi3/issue82_test.go +++ b/openapi3/issue82_test.go @@ -2,8 +2,9 @@ package openapi3 import ( "encoding/json" - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestIssue82(t *testing.T) {