Skip to content

Commit fc3f90d

Browse files
Prevent a panic in the error encoder (#262)
1 parent 25cec2f commit fc3f90d

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

openapi3filter/fixtures/petstore.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@
6767
],
6868
"requestBody": {
6969
"$ref": "#/components/requestBodies/PetWithRequired"
70-
}
70+
},
71+
"parameters": [
72+
{
73+
"schema": {
74+
"type": "string",
75+
"enum": [
76+
"demo",
77+
"prod"
78+
]
79+
},
80+
"in": "header",
81+
"name": "x-environment",
82+
"description": "Where to send the data for processing"
83+
}
84+
]
7185
},
7286
"patch": {
7387
"tags": [
@@ -1136,7 +1150,7 @@
11361150
},
11371151
"name": {
11381152
"type": "string",
1139-
"example": "doggie",
1153+
"example": "doggie"
11401154
},
11411155
"photoUrls": {
11421156
"type": "array",
@@ -1196,7 +1210,7 @@
11961210
},
11971211
"name": {
11981212
"type": "string",
1199-
"example": "doggie",
1213+
"example": "doggie"
12001214
},
12011215
"photoUrls": {
12021216
"type": "array",
@@ -1330,4 +1344,4 @@
13301344
}
13311345
}
13321346
}
1333-
}
1347+
}

openapi3filter/validation_error_encoder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func convertSchemaError(e *RequestError, innerErr *openapi3.SchemaError) *Valida
148148
}
149149

150150
// Add error source
151-
if e.Parameter != nil && e.Parameter.In == "query" {
151+
if e.Parameter != nil {
152152
// We have a JSONPointer in the query param too so need to
153153
// make sure 'Parameter' check takes priority over 'Pointer'
154154
cErr.Source = &ValidationErrorSource{
@@ -172,7 +172,8 @@ func convertSchemaError(e *RequestError, innerErr *openapi3.SchemaError) *Valida
172172
cErr.Detail = fmt.Sprintf("Value '%v' at %s must be one of: %s",
173173
innerErr.Value, toJSONPointer(innerErr.JSONPointer()), strings.Join(enums, ", "))
174174
value := fmt.Sprintf("%v", innerErr.Value)
175-
if (e.Parameter.Explode == nil || *e.Parameter.Explode == true) &&
175+
if e.Parameter != nil &&
176+
(e.Parameter.Explode == nil || *e.Parameter.Explode == true) &&
176177
(e.Parameter.Style == "" || e.Parameter.Style == "form") &&
177178
strings.Contains(value, ",") {
178179
parts := strings.Split(value, ",")

openapi3filter/validation_error_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func getValidationTests(t *testing.T) []*validationTest {
7474
unsupportedContentType := newPetstoreRequest(t, http.MethodPost, "/pet", bytes.NewBufferString(`{}`))
7575
unsupportedContentType.Header.Set("Content-Type", "text/plain")
7676

77+
unsupportedHeaderValue := newPetstoreRequest(t, http.MethodPost, "/pet", bytes.NewBufferString(`{}`))
78+
unsupportedHeaderValue.Header.Set("x-environment", "watdis")
79+
7780
return []*validationTest{
7881
//
7982
// Basics
@@ -270,10 +273,43 @@ func getValidationTests(t *testing.T) []*validationTest {
270273
},
271274
},
272275

276+
//
277+
// Request header params
278+
//
279+
{
280+
name: "error - invalid enum value for header string parameter",
281+
args: validationArgs{
282+
r: unsupportedHeaderValue,
283+
},
284+
wantErrParam: "x-environment",
285+
wantErrParamIn: "header",
286+
wantErrSchemaReason: "JSON value is not one of the allowed values",
287+
wantErrSchemaPath: "/",
288+
wantErrSchemaValue: "watdis",
289+
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
290+
Title: "JSON value is not one of the allowed values",
291+
Detail: "Value 'watdis' at / must be one of: demo, prod",
292+
Source: &ValidationErrorSource{Parameter: "x-environment"}},
293+
},
294+
273295
//
274296
// Request bodies
275297
//
276298

299+
{
300+
name: "error - invalid enum value for header object attribute",
301+
args: validationArgs{
302+
r: newPetstoreRequest(t, http.MethodPost, "/pet", bytes.NewBufferString(`{"status":"watdis"}`)),
303+
},
304+
wantErrReason: "doesn't match the schema",
305+
wantErrSchemaReason: "JSON value is not one of the allowed values",
306+
wantErrSchemaValue: "watdis",
307+
wantErrSchemaPath: "/status",
308+
wantErrResponse: &ValidationError{Status: http.StatusUnprocessableEntity,
309+
Title: "JSON value is not one of the allowed values",
310+
Detail: "Value 'watdis' at /status must be one of: available, pending, sold",
311+
Source: &ValidationErrorSource{Pointer: "/status"}},
312+
},
277313
{
278314
name: "error - missing required object attribute",
279315
args: validationArgs{

0 commit comments

Comments
 (0)