Skip to content

Commit

Permalink
fix: error path is lost (#681) (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
micronull authored Nov 28, 2022
1 parent 8a66010 commit 83dd2ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
14 changes: 10 additions & 4 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1722,11 +1722,8 @@ func (err *SchemaError) Error() string {
}
}

if err.Origin != nil {
return err.Origin.Error()
}

buf := bytes.NewBuffer(make([]byte, 0, 256))

if len(err.reversePath) > 0 {
buf.WriteString(`Error at "`)
reversePath := err.reversePath
Expand All @@ -1736,6 +1733,13 @@ func (err *SchemaError) Error() string {
}
buf.WriteString(`": `)
}

if err.Origin != nil {
buf.WriteString(err.Origin.Error())

return buf.String()
}

reason := err.Reason
if reason == "" {
buf.WriteString(`Doesn't match schema "`)
Expand All @@ -1744,6 +1748,7 @@ func (err *SchemaError) Error() string {
} else {
buf.WriteString(reason)
}

if !SchemaErrorDetailsDisabled {
buf.WriteString("\nSchema:\n ")
encoder := json.NewEncoder(buf)
Expand All @@ -1756,6 +1761,7 @@ func (err *SchemaError) Error() string {
panic(err)
}
}

return buf.String()
}

Expand Down
30 changes: 30 additions & 0 deletions openapi3/schema_formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,33 @@ func TestFormatCallback_WrapError(t *testing.T) {

delete(SchemaStringFormats, "foobar")
}

func TestReversePathInMessageSchemaError(t *testing.T) {
DefineIPv4Format()

SchemaErrorDetailsDisabled = true

const spc = `
components:
schemas:
Something:
type: object
properties:
ip:
type: string
format: ipv4
`
l := NewLoader()

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

err = doc.Components.Schemas["Something"].Value.VisitJSON(map[string]interface{}{
`ip`: `123.0.0.11111`,
})

require.EqualError(t, err, `Error at "/ip": Not an IP address`)

delete(SchemaStringFormats, "ipv4")
SchemaErrorDetailsDisabled = false
}
2 changes: 1 addition & 1 deletion openapi3/schema_issue289_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ openapi: "3.0.1"
"name": "kin-openapi",
"address": "127.0.0.1",
})
require.EqualError(t, err, ErrOneOfConflict.Error())
require.ErrorIs(t, err, ErrOneOfConflict)
}
2 changes: 1 addition & 1 deletion openapi3filter/issue625_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ paths:
name: "failed allof object array",
spec: allOfArraySpec,
req: `/items?test=1.2,3.1`,
errStr: `parameter "test" in query has an error: Value must be an integer`,
errStr: `parameter "test" in query has an error: Error at "/0": Value must be an integer`,
},
{
name: "success oneof object array",
Expand Down

0 comments on commit 83dd2ff

Please sign in to comment.