Skip to content

Commit 562f8a8

Browse files
committed
added fixes for pb33f/wiretap#83
Signed-off-by: Dave Shanley <[email protected]>
1 parent 3e15bf5 commit 562f8a8

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

parameters/query_parameters_test.go

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ paths:
18271827
valid, errors := v.ValidateQueryParams(request)
18281828
assert.False(t, valid)
18291829

1830-
assert.Len(t, errors, 1)
1830+
assert.Len(t, errors, 2)
18311831
assert.Equal(t, "The query parameter 'fishy' has the 'deepObject' style defined, "+
18321832
"There are multiple values (2) supplied, instead of a single value", errors[0].Reason)
18331833
}
@@ -2451,3 +2451,98 @@ paths:
24512451
assert.Len(t, errors, 1)
24522452
assert.Equal(t, "expected string, but got number", errors[0].SchemaValidationErrors[0].Reason)
24532453
}
2454+
2455+
// https://github.com/pb33f/wiretap/issues/83
2456+
func TestNewValidator_QueryParamValidateStyle_BadSchemaDeepObject(t *testing.T) {
2457+
spec := `openapi: 3.1.0
2458+
info:
2459+
title: Test
2460+
version: 0.1.0
2461+
security:
2462+
- apiKeyAuth: []
2463+
paths:
2464+
/anything/queryParams/deepObject/obj:
2465+
get:
2466+
operationId: deepObjectQueryParamsObject
2467+
parameters:
2468+
- name: objParam
2469+
in: query
2470+
style: deepObject
2471+
schema:
2472+
$ref: "components.yaml#/components/schemas/simpleObject"
2473+
required: true
2474+
responses:
2475+
"200":
2476+
description: OK
2477+
components:
2478+
securitySchemes:
2479+
apiKeyAuth:
2480+
type: apiKey
2481+
in: header
2482+
name: Authorization
2483+
description: Authenticate using an API Key generated via our platform.`
2484+
2485+
doc, _ := libopenapi.NewDocument([]byte(spec))
2486+
2487+
m, err := doc.BuildV3Model()
2488+
assert.Len(t, err, 1) // path build will fail because of missing schema.
2489+
2490+
v := NewParameterValidator(&m.Model)
2491+
2492+
request, _ := http.NewRequest(http.MethodGet,
2493+
"http://localhost:9090/anything/queryParams/deepObject/obj?objParam=blahdedahdedah", nil)
2494+
2495+
valid, errors := v.ValidateQueryParams(request)
2496+
assert.True(t, valid)
2497+
assert.Len(t, errors, 0)
2498+
}
2499+
2500+
// https://github.com/pb33f/wiretap/issues/83
2501+
func TestNewValidator_QueryParamValidateStyle_BadSchemaDeepObject_Inline(t *testing.T) {
2502+
spec := `openapi: 3.1.0
2503+
info:
2504+
title: Test
2505+
version: 0.1.0
2506+
security:
2507+
- apiKeyAuth: []
2508+
paths:
2509+
/anything/queryParams/deepObject/obj:
2510+
get:
2511+
operationId: deepObjectQueryParamsObject
2512+
parameters:
2513+
- name: objParam
2514+
in: query
2515+
style: deepObject
2516+
schema:
2517+
type: object
2518+
properties:
2519+
cake:
2520+
type: string
2521+
required: true
2522+
responses:
2523+
"200":
2524+
description: OK
2525+
components:
2526+
securitySchemes:
2527+
apiKeyAuth:
2528+
type: apiKey
2529+
in: header
2530+
name: Authorization
2531+
description: Authenticate using an API Key generated via our platform.`
2532+
2533+
doc, _ := libopenapi.NewDocument([]byte(spec))
2534+
2535+
m, err := doc.BuildV3Model()
2536+
assert.Len(t, err, 0) //no patch build here
2537+
2538+
v := NewParameterValidator(&m.Model)
2539+
2540+
request, _ := http.NewRequest(http.MethodGet,
2541+
"http://localhost:9090/anything/queryParams/deepObject/obj?objParam=blahdedahdedah", nil)
2542+
2543+
valid, errors := v.ValidateQueryParams(request)
2544+
assert.False(t, valid)
2545+
assert.Len(t, errors, 1)
2546+
assert.Equal(t, "The query parameter 'objParam' is defined as an object,"+
2547+
" however it failed to pass a schema validation", errors[0].Reason)
2548+
}

parameters/validate_parameter.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,31 @@ func ValidateParameterSchema(
8181
}
8282
}
8383
if p != nil {
84-
scErrs = jsch.Validate(p)
84+
85+
// check if any of the items have an empty key
86+
skip := false
87+
if rawIsMap {
88+
for k := range p.(map[string]interface{}) {
89+
if k == "" {
90+
validationErrors = append(validationErrors, &errors.ValidationError{
91+
ValidationType: validationType,
92+
ValidationSubType: subValType,
93+
Message: fmt.Sprintf("%s '%s' failed to validate", entity, name),
94+
Reason: fmt.Sprintf("%s '%s' is defined as an object, "+
95+
"however it failed to pass a schema validation", reasonEntity, name),
96+
SpecLine: schema.GoLow().Type.KeyNode.Line,
97+
SpecCol: schema.GoLow().Type.KeyNode.Column,
98+
SchemaValidationErrors: nil,
99+
HowToFix: errors.HowToFixInvalidSchema,
100+
})
101+
skip = true
102+
break
103+
}
104+
}
105+
}
106+
if !skip {
107+
scErrs = jsch.Validate(p)
108+
}
85109
}
86110
}
87111
if scErrs != nil {

0 commit comments

Comments
 (0)