Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
44 changes: 31 additions & 13 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math/big"
"regexp"
"strconv"
"strings"
"unicode/utf16"

"github.com/getkin/kin-openapi/jsoninfo"
Expand Down Expand Up @@ -837,6 +838,35 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
}

if v := schema.OneOf; len(v) > 0 {

if schema.Discriminator != nil {
/* Find mapped object by ref */
if valuemap, okcheck := value.(map[string]interface{}); okcheck {
pn := schema.Discriminator.PropertyName
if discriminatorVal, okcheck := valuemap[pn]; okcheck {
if len(schema.Discriminator.Mapping) > 0 {
if mapref, okcheck := schema.Discriminator.Mapping[discriminatorVal.(string)]; okcheck {
for _, item := range v {
if item.Ref == mapref {
return item.Value.visitJSON(settings, value)
}
}
}
} else {
/* Assume implicit mapping on objectType as stated in Mapping Type Names section:
``It is implied, that the property to which discriminator refers, contains the
name of the target schema. In the example above, the objectType property should
contain either simpleObject, or complexObject string.''*/
for _, oneof := range schema.OneOf {
if strings.HasSuffix(oneof.Ref, discriminatorVal.(string)) {
return oneof.Value.visitJSON(settings, value)
}
}
}
}
}
}

ok := 0
for _, item := range v {
v := item.Value
Expand All @@ -848,19 +878,7 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
err := v.visitJSON(settings, value)
settings.failfast = oldfailfast
if err == nil {
if schema.Discriminator != nil {
pn := schema.Discriminator.PropertyName
if valuemap, okcheck := value.(map[string]interface{}); okcheck {
if discriminatorVal, okcheck := valuemap[pn]; okcheck == true {
mapref, okcheck := schema.Discriminator.Mapping[discriminatorVal.(string)]
if okcheck && mapref == item.Ref {
ok++
}
}
}
} else {
ok++
}
ok++
}
}
if ok != 1 {
Expand Down
3 changes: 2 additions & 1 deletion openapi3filter/validate_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
Expand Down Expand Up @@ -63,7 +64,7 @@ components:
type: integer
`

func Example() {
func TestExample(t *testing.T) {
loader := openapi3.NewSwaggerLoader()
doc, err := loader.LoadSwaggerFromData([]byte(spec))
if err != nil {
Expand Down