diff --git a/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json b/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json index 0896e8c..70e34c6 100644 --- a/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json +++ b/openapi2kong/oas3_testfiles/13-request-validator-plugin.expected.json @@ -44,6 +44,89 @@ "OAS3file_13-request-validator-plugin.yaml" ] }, + { + "id": "ab63ee84-0324-5832-840a-d51118308654", + "methods": [ + "POST" + ], + "name": "example_body-single-content-type-with-charset_post", + "paths": [ + "~/body-single-content-type-with-charset$" + ], + "plugins": [ + { + "config": { + "allowed_content_types": [ + "application/json; charset=UTF-8" + ], + "body_schema": "{\"$ref\":\"#/definitions/jsonSchema\",\"definitions\":{\"jsonSchema\":{\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}},\"type\":\"object\"}}}", + "version": "draft4" + }, + "id": "c7ee68c7-96e8-56ca-98f6-cbe44cf3c9ce", + "name": "request-validator", + "tags": [ + "OAS3_import", + "OAS3file_13-request-validator-plugin.yaml" + ] + } + ], + "regex_priority": 200, + "strip_path": false, + "tags": [ + "OAS3_import", + "OAS3file_13-request-validator-plugin.yaml" + ] + }, + { + "id": "38af9752-0698-5a31-b825-90a83fee4225", + "methods": [ + "POST" + ], + "name": "example_body-single-content-type-with-structured-syntax-suffix_post", + "paths": [ + "~/body-single-content-type-with-structured-syntax-suffix$" + ], + "plugins": [ + { + "config": { + "allowed_content_types": [ + "application/merge-patch+json" + ], + "body_schema": "{\"$ref\":\"#/definitions/jsonSchema\",\"definitions\":{\"jsonSchema\":{\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}},\"type\":\"object\"}}}", + "version": "draft4" + }, + "id": "fc80da5e-c48a-59c6-b879-ab8ff331746d", + "name": "request-validator", + "tags": [ + "OAS3_import", + "OAS3file_13-request-validator-plugin.yaml" + ] + } + ], + "regex_priority": 200, + "strip_path": false, + "tags": [ + "OAS3_import", + "OAS3file_13-request-validator-plugin.yaml" + ] + }, + { + "id": "fea43c61-09c5-5baf-879f-8542b485f631", + "methods": [ + "POST" + ], + "name": "example_body-single-content-xml_post", + "paths": [ + "~/body-single-content-xml$" + ], + "plugins": [], + "regex_priority": 200, + "strip_path": false, + "tags": [ + "OAS3_import", + "OAS3file_13-request-validator-plugin.yaml" + ] + }, { "id": "6d59c2fe-e723-5238-a847-d87d8e8bb7fc", "methods": [ @@ -150,4 +233,4 @@ } ], "upstreams": [] -} \ No newline at end of file +} diff --git a/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml b/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml index 669b58f..1fd7270 100644 --- a/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml +++ b/openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml @@ -66,6 +66,38 @@ paths: responses: "200": description: OK + /body-single-content-xml: + post: + requestBody: + content: + application/xml: + schema: + $ref: '#/components/schemas/xmlSchema' + responses: + "200": + description: OK + + /body-single-content-type-with-charset: + post: + requestBody: + content: + application/json; charset=UTF-8: + schema: + $ref: '#/components/schemas/jsonSchema' + responses: + "200": + description: OK + + /body-single-content-type-with-structured-syntax-suffix: + post: + requestBody: + content: + application/merge-patch+json: + schema: + $ref: '#/components/schemas/jsonSchema' + responses: + "200": + description: OK components: schemas: diff --git a/openapi2kong/validator.go b/openapi2kong/validator.go index 0202995..8476007 100644 --- a/openapi2kong/validator.go +++ b/openapi2kong/validator.go @@ -2,6 +2,7 @@ package openapi2kong import ( "encoding/json" + "mime" "sort" "strings" @@ -79,6 +80,15 @@ func generateParameterSchema(operation *openapi3.Operation, insoCompat bool) []m return result } +func parseMediaType(mediaType string) (string, string, error) { + parsedMediaType, _, err := mime.ParseMediaType(mediaType) + if err != nil { + return "", "", err + } + parts := strings.Split(parsedMediaType, "/") + return parts[0], parts[1], nil +} + // generateBodySchema returns the given schema if there is one, a generated // schema if it was specified, or "" if there is none. func generateBodySchema(operation *openapi3.Operation) string { @@ -98,7 +108,12 @@ func generateBodySchema(operation *openapi3.Operation) string { } for contentType, content := range content { - if strings.Contains(strings.ToLower(contentType), "application/json") { + typ, subtype, err := parseMediaType(contentType) + if err != nil { + logbasics.Info("invalid MediaType '" + contentType + "' will be ignored") + return "" + } + if typ == "application" && (subtype == "json" || strings.HasSuffix(subtype, "+json")) { return extractSchema((*content).Schema) } }