Skip to content

Commit

Permalink
Return a more specific error when more than oneOf schemas match (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Jan 29, 2021
1 parent be070be commit aa9a5c3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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())
}

0 comments on commit aa9a5c3

Please sign in to comment.