Skip to content

Commit

Permalink
fix: validating cookie parameter for req-validator
Browse files Browse the repository at this point in the history
At the moment, we do not support cookie parameters in request-validator
plugin. However, `deck file openapi2kong` command generates the yaml file
even if it is present in the parameter schema. Thus, adding a validation
layer over it to ensure deck does not create invalid specs.

For: #223
  • Loading branch information
Prashansa-K committed Nov 13, 2024
1 parent 9bbc21c commit b67debf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,6 @@
"schema": "{\"type\":\"integer\"}",
"style": "simple"
},
{
"explode": true,
"in": "cookie",
"name": "cookieid",
"required": true,
"schema": "{\"type\":\"integer\"}",
"style": "form"
},
{
"explode": false,
"in": "path",
Expand Down
3 changes: 3 additions & 0 deletions openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ paths:
schema:
type: integer
required: true
# This would not be added to the req-validator plugin config
# as cookie type is not supported yet.
# A warning would be logged and this parameter would be ignored.
- in: cookie
name: cookieid
schema:
Expand Down
19 changes: 13 additions & 6 deletions openapi2kong/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ func generateParameterSchema(operation *v3.Operation, path *v3.PathItem,

result := make([]map[string]interface{}, len(combinedParameters))
i := 0
invalidParamCounts := 0

for _, parameter := range combinedParameters {
if parameter != nil {
if parameter.In == "cookie" {
logbasics.Info(`cookie parameters are not supported for request-validator plugin;
choose either path, query or header`)

invalidParamCounts++

continue
}

style := getDefaultParamStyle(parameter.Style, parameter.In)

var explode bool
Expand All @@ -82,11 +92,6 @@ func generateParameterSchema(operation *v3.Operation, path *v3.PathItem,
paramConf["explode"] = explode
paramConf["in"] = parameter.In

if parameter.In == "cookie" {
return nil, fmt.Errorf(`cookie parameters are not supported for request-validator plugin;
choose either path, query or header`)
}

if parameter.In == "path" {
paramConf["name"] = sanitizeRegexCapture(parameter.Name, insoCompat)
} else {
Expand Down Expand Up @@ -115,7 +120,9 @@ func generateParameterSchema(operation *v3.Operation, path *v3.PathItem,
}
}

return result, nil
// This ensures that we don't return nulls in the map, in case of invalid parameters
// indexing makes sure that order is maintained and nulls are in the end
return result[:len(result)-invalidParamCounts], nil
}

func parseMediaType(mediaType string) (string, string, error) {
Expand Down

0 comments on commit b67debf

Please sign in to comment.