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: fixed mixed-types array generation for req-validator plugin #231

Merged
merged 2 commits into from
Nov 25, 2024
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 @@ -47,6 +47,22 @@
"required": true,
"schema": "{\"$ref\":\"#/definitions/secondHeaderType\",\"definitions\":{\"numberType\":{\"example\":2.5,\"type\":\"number\"},\"secondHeaderType\":{\"oneOf\":[{\"$ref\":\"#/definitions/stringType\"},{\"$ref\":\"#/definitions/numberType\"}],\"type\":\"string\"},\"stringType\":{\"example\":\"10\",\"type\":\"string\"}}}",
"style": "simple"
},
{
"explode": true,
"in": "query",
"name": "testArrayOne",
"required": true,
"schema": "{\"items\":{\"oneOf\":[{\"type\":\"string\"},{\"type\":\"integer\"}]},\"type\":\"array\"}",
"style": "form"
},
{
"explode": true,
"in": "query",
"name": "testArrayTwo",
"required": true,
"schema": "{\"definitions\":{\"headerType\":{\"oneOf\":[{\"$ref\":\"#/definitions/stringType\"},{\"$ref\":\"#/definitions/numberType\"}],\"type\":\"string\"},\"newType\":{\"oneOf\":[{\"type\":\"string\"},{\"type\":\"number\"}],\"type\":\"string\"},\"numberType\":{\"example\":2.5,\"type\":\"number\"},\"stringType\":{\"example\":\"10\",\"type\":\"string\"}},\"items\":{\"oneOf\":[{\"$ref\":\"#/definitions/headerType\"},{\"$ref\":\"#/definitions/newType\"}]},\"type\":\"array\"}",
"style": "form"
}
],
"version": "draft4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ paths:
schema:
$ref: '#/components/schemas/secondHeaderType'
required: true
- in: query
name: testArrayOne
schema:
type: array
items:
oneOf:
- type: string
- type: integer
required: true
- in: query
name: testArrayTwo
schema:
type: array
items:
oneOf:
- $ref: '#/components/schemas/headerType'
- $ref: '#/components/schemas/newType'
required: true
components:
schemas:
headerType:
Expand All @@ -55,4 +73,11 @@ components:
numberType:
type: number
example: 2.5
newType:
type: string
oneOf:
- type: string
- type: number



16 changes: 15 additions & 1 deletion openapi2kong/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,21 @@ func fetchTopLevelType(schemaMap map[string]interface{}) (string, bool) {
}

// Recursively search in nested objects
for _, value := range schemaMap {
for key, value := range schemaMap {
// This implies type = array
if key == "items" {
if itemMap, ok := schemaMap["items"].(map[string]interface{}); ok {
if _, ok := itemMap["oneOf"]; ok {
// skip this item map
// we don't need a top-level type with this oneOf
// However, we need to ensure that any nested refs
// in the oneOf array have top-level types.
// Thus, continuing the loop here.
continue
}
}
}

switch v := value.(type) {
case map[string]interface{}:
if str, oneOfAnyOfFound := fetchTopLevelType(v); oneOfAnyOfFound {
Expand Down
Loading