Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a more specific error when more than oneOf schemas match #292

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
39 changes: 39 additions & 0 deletions openapi3/schema_issue289_test.go
Original file line number Diff line number Diff line change
@@ -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())
}