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 <alexander.rashed@ntsretail.com>
This commit is contained in:
Alexander Rashed 2020-11-16 14:16:53 +01:00 committed by GitHub
parent aca6927ce0
commit 54d6257865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 0 deletions

View File

@ -3856,10 +3856,20 @@ public class DefaultCodegen implements CodegenConfig {
// add form/body parameter (if any) to the end of all parameter list // add form/body parameter (if any) to the end of all parameter list
if (!prependFormOrBodyParameters) { if (!prependFormOrBodyParameters) {
for (CodegenParameter cp : formParams) { for (CodegenParameter cp : formParams) {
if (ensureUniqueParams) {
if (!isParameterNameUnique(cp, allParams)) {
cp.paramName = generateNextName(cp.paramName);
}
}
allParams.add(cp.copy()); allParams.add(cp.copy());
} }
for (CodegenParameter cp : bodyParams) { for (CodegenParameter cp : bodyParams) {
if (ensureUniqueParams) {
if (!isParameterNameUnique(cp, allParams)) {
cp.paramName = generateNextName(cp.paramName);
}
}
allParams.add(cp.copy()); allParams.add(cp.copy());
} }
} }

View File

@ -409,6 +409,20 @@ public class DefaultCodegenTest {
Assert.assertTrue(queryParamsNames.contains("myparam2")); 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<String> allParamsNames = co.allParams.stream().map(p -> p.paramName).collect(Collectors.toList());
Assert.assertTrue(allParamsNames.contains("id"));
Assert.assertTrue(allParamsNames.contains("id2"));
}
@Test @Test
public void testGetSchemaTypeWithComposedSchemaWithOneOf() { public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/composed-oneof.yaml"); final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/composed-oneof.yaml");

View File

@ -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: {}