From b67debfe3c785e0f245de6b6e4acc540216cea02 Mon Sep 17 00:00:00 2001 From: Prashansa Kulshrestha Date: Wed, 13 Nov 2024 13:00:08 +0530 Subject: [PATCH] fix: validating cookie parameter for req-validator 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: https://github.com/Kong/go-apiops/issues/223 --- .../13-request-validator-plugin.expected.json | 8 -------- .../13-request-validator-plugin.yaml | 3 +++ openapi2kong/validator.go | 19 +++++++++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json b/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json index b444ce2..94fded6 100644 --- a/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json +++ b/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json @@ -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", diff --git a/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml b/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml index 1fd7270..e256ba5 100644 --- a/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml +++ b/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml @@ -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: diff --git a/openapi2kong/validator.go b/openapi2kong/validator.go index 4f2f3f8..378764e 100644 --- a/openapi2kong/validator.go +++ b/openapi2kong/validator.go @@ -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 @@ -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 { @@ -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) {