From 8eed03317a2f108a9fa04834edc6453c636b67ad Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Fri, 29 Jan 2021 09:31:57 +0100 Subject: [PATCH 1/2] Return a more specific error when more than oneOf schemas match Signed-off-by: Pierre Fenoll --- openapi3/schema.go | 9 +++++++- openapi3/schema_issue289_test.go | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 openapi3/schema_issue289_test.go diff --git a/openapi3/schema.go b/openapi3/schema.go index 4fcc864ac..4968feca0 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -25,6 +25,9 @@ var ( errSchema = errors.New("Input does not match the schema") + // ErrOneOfConflict is the SchemaError Origin when data matches more than one oneOf schema + ErrOneOfConflict = errors.New("input matches more than one oneOf schemas") + // ErrSchemaInputNaN may be returned when validating a number ErrSchemaInputNaN = errors.New("NaN is not allowed") // ErrSchemaInputInf may be returned when validating a number @@ -851,11 +854,15 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val if settings.failfast { return errSchema } - return &SchemaError{ + e := &SchemaError{ Value: value, Schema: schema, SchemaField: "oneOf", } + if ok > 1 { + e.Origin = ErrOneOfConflict + } + return e } } diff --git a/openapi3/schema_issue289_test.go b/openapi3/schema_issue289_test.go new file mode 100644 index 000000000..a167ff91f --- /dev/null +++ b/openapi3/schema_issue289_test.go @@ -0,0 +1,39 @@ +package openapi3 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIssue289(t *testing.T) { + spec := []byte(`components: + schemas: + Server: + properties: + address: + oneOf: + - $ref: "#/components/schemas/ip-address" + - $ref: "#/components/schemas/domain-name" + name: + type: string + type: object + domain-name: + maxLength: 10 + minLength: 5 + pattern: "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)|\\." + type: string + ip-address: + pattern: "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$" + type: string +openapi: "3.0.1" +`) + + s, err := NewSwaggerLoader().LoadSwaggerFromData(spec) + require.NoError(t, err) + err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{ + "name": "kin-openapi", + "address": "127.0.0.1", + }) + require.EqualError(t, err, ErrOneOfConflict.Error()) +} From 34c6a125ff19f3fc867bcde7e2f250554245ffe5 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Fri, 29 Jan 2021 09:35:18 +0100 Subject: [PATCH 2/2] fmt Signed-off-by: Pierre Fenoll --- openapi3/schema_issue289_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/openapi3/schema_issue289_test.go b/openapi3/schema_issue289_test.go index a167ff91f..e4e4aad36 100644 --- a/openapi3/schema_issue289_test.go +++ b/openapi3/schema_issue289_test.go @@ -1,13 +1,13 @@ package openapi3 import ( - "testing" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestIssue289(t *testing.T) { - spec := []byte(`components: + spec := []byte(`components: schemas: Server: properties: @@ -29,11 +29,11 @@ func TestIssue289(t *testing.T) { openapi: "3.0.1" `) - s, err := NewSwaggerLoader().LoadSwaggerFromData(spec) - require.NoError(t, err) - err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{ - "name": "kin-openapi", - "address": "127.0.0.1", - }) - require.EqualError(t, err, ErrOneOfConflict.Error()) + s, err := NewSwaggerLoader().LoadSwaggerFromData(spec) + require.NoError(t, err) + err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{ + "name": "kin-openapi", + "address": "127.0.0.1", + }) + require.EqualError(t, err, ErrOneOfConflict.Error()) }