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

Fix validation of complex enum values #647

Merged
merged 1 commit into from
Oct 24, 2022
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
3 changes: 2 additions & 1 deletion openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math"
"math/big"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -870,7 +871,7 @@ func (schema *Schema) visitJSON(settings *schemaValidationSettings, value interf
func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, value interface{}) (err error) {
if enum := schema.Enum; len(enum) != 0 {
for _, v := range enum {
if value == v {
if reflect.DeepEqual(v, value) {
return
}
}
Expand Down
42 changes: 42 additions & 0 deletions openapi3/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,3 +1314,45 @@ func TestValidationFailsOnInvalidPattern(t *testing.T) {
err := schema.Validate(context.Background())
require.Error(t, err)
}

func TestIssue646(t *testing.T) {
data := []byte(`
enum:
- 42
- []
- [a]
- {}
- {b: c}
`[1:])

var schema Schema
err := yaml.Unmarshal(data, &schema)
require.NoError(t, err)

err = schema.Validate(context.Background())
require.NoError(t, err)

err = schema.VisitJSON(42)
require.NoError(t, err)

err = schema.VisitJSON(1337)
require.Error(t, err)

err = schema.VisitJSON([]interface{}{})
require.NoError(t, err)

err = schema.VisitJSON([]interface{}{"a"})
require.NoError(t, err)

err = schema.VisitJSON([]interface{}{"b"})
require.Error(t, err)

err = schema.VisitJSON(map[string]interface{}{})
require.NoError(t, err)

err = schema.VisitJSON(map[string]interface{}{"b": "c"})
require.NoError(t, err)

err = schema.VisitJSON(map[string]interface{}{"d": "e"})
require.Error(t, err)
}