Skip to content

Commit 54d6257

Browse files
alexrashedAlexander Rashed
andauthored
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>
1 parent aca6927 commit 54d6257

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,10 +3856,20 @@ public CodegenOperation fromOperation(String path,
38563856
// add form/body parameter (if any) to the end of all parameter list
38573857
if (!prependFormOrBodyParameters) {
38583858
for (CodegenParameter cp : formParams) {
3859+
if (ensureUniqueParams) {
3860+
if (!isParameterNameUnique(cp, allParams)) {
3861+
cp.paramName = generateNextName(cp.paramName);
3862+
}
3863+
}
38593864
allParams.add(cp.copy());
38603865
}
38613866

38623867
for (CodegenParameter cp : bodyParams) {
3868+
if (ensureUniqueParams) {
3869+
if (!isParameterNameUnique(cp, allParams)) {
3870+
cp.paramName = generateNextName(cp.paramName);
3871+
}
3872+
}
38633873
allParams.add(cp.copy());
38643874
}
38653875
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ public void testConsistentParameterNameAfterUniquenessRename() throws Exception
409409
Assert.assertTrue(queryParamsNames.contains("myparam2"));
410410
}
411411

412+
@Test
413+
public void testUniquenessRenameOfFormParameters() throws Exception {
414+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/form-duplicated-parameter.yaml");
415+
DefaultCodegen codegen = new DefaultCodegen();
416+
codegen.setOpenAPI(openAPI);
417+
Operation operation = openAPI.getPaths().get("/form-param-poc/{id}").getPut();
418+
CodegenOperation co = codegen.fromOperation("/form-param-poc/{id}", "put", operation, null);
419+
Assert.assertEquals(co.path, "/form-param-poc/{id}");
420+
Assert.assertEquals(co.allParams.size(), 2);
421+
List<String> allParamsNames = co.allParams.stream().map(p -> p.paramName).collect(Collectors.toList());
422+
Assert.assertTrue(allParamsNames.contains("id"));
423+
Assert.assertTrue(allParamsNames.contains("id2"));
424+
}
425+
412426
@Test
413427
public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
414428
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/composed-oneof.yaml");
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.0.1
2+
info:
3+
title: FormData Test Api Documentation
4+
description: Minimal OpenAPI spec file to showcase duplicated params for formData.
5+
version: 0.0.1
6+
servers:
7+
- url: /backend/rest
8+
tags:
9+
- name: form-param-poc
10+
description: File storage resource for Fiscalization France standard
11+
paths:
12+
'/form-param-poc/{id}':
13+
put:
14+
tags:
15+
- form-param-poc
16+
summary: fullUpdate
17+
operationId: form-param-poc_update
18+
parameters:
19+
- name: id
20+
in: path
21+
required: true
22+
schema:
23+
type: integer
24+
format: int64
25+
requestBody:
26+
content:
27+
multipart/form-data:
28+
schema:
29+
$ref: '#/components/schemas/FormParameters'
30+
responses:
31+
'200':
32+
description: OK.
33+
security:
34+
- oAuthConfig: []
35+
components:
36+
schemas:
37+
FormParameters:
38+
type: object
39+
properties:
40+
id:
41+
type: integer
42+
format: int64
43+
readOnly: true
44+
securitySchemes:
45+
oAuthConfig:
46+
type: oauth2
47+
flows:
48+
implicit:
49+
authorizationUrl: ../backend/login/openid
50+
scopes: {}

0 commit comments

Comments
 (0)