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

rework convertError Example code to show query schema error #626

Merged
merged 1 commit into from
Oct 7, 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
6 changes: 4 additions & 2 deletions openapi3/errors.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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