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: errors in oneOf not contain path (#676) #677

Merged
merged 1 commit into from Nov 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions openapi3/schema.go
Expand Up @@ -1650,6 +1650,12 @@ type SchemaError struct {
var _ interface{ Unwrap() error } = SchemaError{}

func markSchemaErrorKey(err error, key string) error {
var me multiErrorForOneOf

if errors.As(err, &me) {
err = me.Unwrap()
}

if v, ok := err.(*SchemaError); ok {
v.reversePath = append(v.reversePath, key)
return v
Expand All @@ -1664,17 +1670,7 @@ func markSchemaErrorKey(err error, key string) error {
}

func markSchemaErrorIndex(err error, index int) error {
if v, ok := err.(*SchemaError); ok {
v.reversePath = append(v.reversePath, strconv.FormatInt(int64(index), 10))
return v
}
if v, ok := err.(MultiError); ok {
for _, e := range v {
_ = markSchemaErrorIndex(e, index)
}
return v
}
return err
return markSchemaErrorKey(err, strconv.FormatInt(int64(index), 10))
}

func (err *SchemaError) JSONPointer() []string {
Expand Down
48 changes: 48 additions & 0 deletions openapi3/schema_oneOf_test.go
Expand Up @@ -3,6 +3,7 @@ package openapi3
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -134,3 +135,50 @@ func TestVisitJSON_OneOf_BadDescriminatorType(t *testing.T) {
})
require.EqualError(t, err, "descriminator value is not a string")
}

func TestVisitJSON_OneOf_Path(t *testing.T) {
t.Parallel()

loader := NewLoader()
spc := `
components:
schemas:
Something:
type: object
properties:
first:
type: object
properties:
second:
type: object
properties:
third:
oneOf:
- title: First rule
type: string
minLength: 5
maxLength: 5
- title: Second rule
type: string
minLength: 10
maxLength: 10
`[1:]

doc, err := loader.LoadFromData([]byte(spc))
require.NoError(t, err)

err = doc.Components.Schemas["Something"].Value.VisitJSON(map[string]interface{}{
"first": map[string]interface{}{
"second": map[string]interface{}{
"third": "123456789",
},
},
})

assert.Contains(t, err.Error(), `Error at "/first/second/third"`)

var sErr *SchemaError

assert.ErrorAs(t, err, &sErr)
assert.Equal(t, []string{"first", "second", "third"}, sErr.JSONPointer())
}