Skip to content

Commit

Permalink
Fix parameter uniqueness for form- and body-params (OpenAPITools#7577)
Browse files Browse the repository at this point in the history
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 OpenAPITools#7575

Co-authored-by: Alexander Rashed <[email protected]>
  • Loading branch information
alexrashed and Alexander Rashed authored Nov 16, 2020
1 parent aca6927 commit 54d6257
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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: {}

0 comments on commit 54d6257

Please sign in to comment.