Skip to content

Commit

Permalink
rework convertError Example code to show query schema error (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Oct 7, 2022
1 parent 2fd9aa2 commit 8588ab8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
6 changes: 4 additions & 2 deletions openapi3/errors.go
Expand Up @@ -11,9 +11,11 @@ type MultiError []error

func (me MultiError) Error() string {
buff := &bytes.Buffer{}
for _, e := range me {
for i, e := range me {
buff.WriteString(e.Error())
buff.WriteString(" | ")
if i != len(me)-1 {
buff.WriteString(" | ")
}
}
return buff.String()
}
Expand Down
6 changes: 6 additions & 0 deletions openapi3filter/testdata/petstore.yaml
Expand Up @@ -30,6 +30,12 @@ paths:
summary: "Add a new pet to the store"
description: ""
operationId: "addPet"
parameters:
- name: num
in: query
schema:
type: integer
minimum: 1
requestBody:
required: true
content:
Expand Down
53 changes: 23 additions & 30 deletions openapi3filter/unpack_errors_test.go
Expand Up @@ -66,7 +66,7 @@ func Example() {

// (note invalid type for name and invalid status)
body := strings.NewReader(`{"name": 100, "photoUrls": [], "status": "invalidStatus"}`)
req, err := http.NewRequest("POST", ts.URL+"/pet", body)
req, err := http.NewRequest("POST", ts.URL+"/pet?num=0", body)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -108,65 +108,58 @@ func Example() {
// Value:
// "invalidStatus"
//
// ===== Start New Error =====
// query.num:
// parameter "num" in query has an error: number must be at least 1
// Schema:
// {
// "minimum": 1,
// "type": "integer"
// }
//
// Value:
// 0
//
// response: 400 {}
}

const (
prefixBody = "@body"
unknown = "@unknown"
)

func convertError(me openapi3.MultiError) map[string][]string {
issues := make(map[string][]string)
for _, err := range me {
const prefixBody = "@body"
switch err := err.(type) {
case *openapi3.SchemaError:
// Can inspect schema validation errors here, e.g. err.Value
field := prefixBody
if path := err.JSONPointer(); len(path) > 0 {
field = fmt.Sprintf("%s.%s", field, strings.Join(path, "."))
}
if _, ok := issues[field]; !ok {
issues[field] = make([]string, 0, 3)
}
issues[field] = append(issues[field], err.Error())
case *openapi3filter.RequestError: // possible there were multiple issues that failed validation
if err, ok := err.Err.(openapi3.MultiError); ok {
for k, v := range convertError(err) {
if _, ok := issues[k]; !ok {
issues[k] = make([]string, 0, 3)
}
issues[k] = append(issues[k], v...)
}
continue
}

// check if invalid HTTP parameter
if err.Parameter != nil {
prefix := err.Parameter.In
name := fmt.Sprintf("%s.%s", prefix, err.Parameter.Name)
if _, ok := issues[name]; !ok {
issues[name] = make([]string, 0, 3)
}
issues[name] = append(issues[name], err.Error())
continue
}

if err, ok := err.Err.(openapi3.MultiError); ok {
for k, v := range convertError(err) {
issues[k] = append(issues[k], v...)
}
continue
}

// check if requestBody
if err.RequestBody != nil {
if _, ok := issues[prefixBody]; !ok {
issues[prefixBody] = make([]string, 0, 3)
}
issues[prefixBody] = append(issues[prefixBody], err.Error())
continue
}
default:
reasons, ok := issues[unknown]
if !ok {
reasons = make([]string, 0, 3)
}
reasons = append(reasons, err.Error())
issues[unknown] = reasons
const unknown = "@unknown"
issues[unknown] = append(issues[unknown], err.Error())
}
}
return issues
Expand Down

0 comments on commit 8588ab8

Please sign in to comment.