diff --git a/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.expected.json b/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.expected.json index 35828e5..4f9b292 100644 --- a/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.expected.json +++ b/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.expected.json @@ -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" diff --git a/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.yaml b/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.yaml index caff31a..26fc767 100644 --- a/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.yaml +++ b/openapi2kong/oas3_testfiles/17-request-validator-plugin-oneOf-usage.yaml @@ -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: @@ -55,4 +73,11 @@ components: numberType: type: number example: 2.5 + newType: + type: string + oneOf: + - type: string + - type: number + + diff --git a/openapi2kong/validator.go b/openapi2kong/validator.go index 9a991b1..3f6e223 100644 --- a/openapi2kong/validator.go +++ b/openapi2kong/validator.go @@ -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 {