From 54d62578655932c485a6907a0fb2cc708ac0046b Mon Sep 17 00:00:00 2001 From: Alexander Rashed Date: Mon, 16 Nov 2020 14:16:53 +0100 Subject: [PATCH] Fix parameter uniqueness for form- and body-params (#7577) If a form-parameter had the same name as another (header-, query-, path-, or cookie-)parameter, a conflict could be caused (for example in the typescript generator). This fix executes the same uniqueness- check and renaming for form- and body-parameters as it is done for all other parameters. @see issue #7575 Co-authored-by: Alexander Rashed --- .../openapitools/codegen/DefaultCodegen.java | 10 ++++ .../codegen/DefaultCodegenTest.java | 14 ++++++ .../3_0/form-duplicated-parameter.yaml | 50 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/3_0/form-duplicated-parameter.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index dbc3ee854bdc..a13db5121fb6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -3856,10 +3856,20 @@ public CodegenOperation fromOperation(String path, // add form/body parameter (if any) to the end of all parameter list if (!prependFormOrBodyParameters) { for (CodegenParameter cp : formParams) { + if (ensureUniqueParams) { + if (!isParameterNameUnique(cp, allParams)) { + cp.paramName = generateNextName(cp.paramName); + } + } allParams.add(cp.copy()); } for (CodegenParameter cp : bodyParams) { + if (ensureUniqueParams) { + if (!isParameterNameUnique(cp, allParams)) { + cp.paramName = generateNextName(cp.paramName); + } + } allParams.add(cp.copy()); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 4be29a74cb99..1d3ab8c22e95 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -409,6 +409,20 @@ public void testConsistentParameterNameAfterUniquenessRename() throws Exception Assert.assertTrue(queryParamsNames.contains("myparam2")); } + @Test + public void testUniquenessRenameOfFormParameters() throws Exception { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/form-duplicated-parameter.yaml"); + DefaultCodegen codegen = new DefaultCodegen(); + codegen.setOpenAPI(openAPI); + Operation operation = openAPI.getPaths().get("/form-param-poc/{id}").getPut(); + CodegenOperation co = codegen.fromOperation("/form-param-poc/{id}", "put", operation, null); + Assert.assertEquals(co.path, "/form-param-poc/{id}"); + Assert.assertEquals(co.allParams.size(), 2); + List allParamsNames = co.allParams.stream().map(p -> p.paramName).collect(Collectors.toList()); + Assert.assertTrue(allParamsNames.contains("id")); + Assert.assertTrue(allParamsNames.contains("id2")); + } + @Test public void testGetSchemaTypeWithComposedSchemaWithOneOf() { final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/composed-oneof.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/form-duplicated-parameter.yaml b/modules/openapi-generator/src/test/resources/3_0/form-duplicated-parameter.yaml new file mode 100644 index 000000000000..86f2189eda49 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/form-duplicated-parameter.yaml @@ -0,0 +1,50 @@ +openapi: 3.0.1 +info: + title: FormData Test Api Documentation + description: Minimal OpenAPI spec file to showcase duplicated params for formData. + version: 0.0.1 +servers: + - url: /backend/rest +tags: + - name: form-param-poc + description: File storage resource for Fiscalization France standard +paths: + '/form-param-poc/{id}': + put: + tags: + - form-param-poc + summary: fullUpdate + operationId: form-param-poc_update + parameters: + - name: id + in: path + required: true + schema: + type: integer + format: int64 + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/FormParameters' + responses: + '200': + description: OK. + security: + - oAuthConfig: [] +components: + schemas: + FormParameters: + type: object + properties: + id: + type: integer + format: int64 + readOnly: true + securitySchemes: + oAuthConfig: + type: oauth2 + flows: + implicit: + authorizationUrl: ../backend/login/openid + scopes: {}