Skip to content

Commit aa9a5c3

Browse files
authored
Return a more specific error when more than oneOf schemas match (#292)
1 parent be070be commit aa9a5c3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

openapi3/schema.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var (
2525

2626
errSchema = errors.New("Input does not match the schema")
2727

28+
// ErrOneOfConflict is the SchemaError Origin when data matches more than one oneOf schema
29+
ErrOneOfConflict = errors.New("input matches more than one oneOf schemas")
30+
2831
// ErrSchemaInputNaN may be returned when validating a number
2932
ErrSchemaInputNaN = errors.New("NaN is not allowed")
3033
// ErrSchemaInputInf may be returned when validating a number
@@ -851,11 +854,15 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
851854
if settings.failfast {
852855
return errSchema
853856
}
854-
return &SchemaError{
857+
e := &SchemaError{
855858
Value: value,
856859
Schema: schema,
857860
SchemaField: "oneOf",
858861
}
862+
if ok > 1 {
863+
e.Origin = ErrOneOfConflict
864+
}
865+
return e
859866
}
860867
}
861868

openapi3/schema_issue289_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package openapi3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIssue289(t *testing.T) {
10+
spec := []byte(`components:
11+
schemas:
12+
Server:
13+
properties:
14+
address:
15+
oneOf:
16+
- $ref: "#/components/schemas/ip-address"
17+
- $ref: "#/components/schemas/domain-name"
18+
name:
19+
type: string
20+
type: object
21+
domain-name:
22+
maxLength: 10
23+
minLength: 5
24+
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]\\.?)|\\."
25+
type: string
26+
ip-address:
27+
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])$"
28+
type: string
29+
openapi: "3.0.1"
30+
`)
31+
32+
s, err := NewSwaggerLoader().LoadSwaggerFromData(spec)
33+
require.NoError(t, err)
34+
err = s.Components.Schemas["Server"].Value.VisitJSON(map[string]interface{}{
35+
"name": "kin-openapi",
36+
"address": "127.0.0.1",
37+
})
38+
require.EqualError(t, err, ErrOneOfConflict.Error())
39+
}

0 commit comments

Comments
 (0)