Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(o2k): request-validator support +json suffix for schemas #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -150,4 +233,4 @@
}
],
"upstreams": []
}
}
32 changes: 32 additions & 0 deletions openapi2kong/oas3_testfiles/13-request-validator-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion openapi2kong/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2kong

import (
"encoding/json"
"mime"
"sort"
"strings"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
Loading