Fix bugs to ensure unique parameter names (#8762)

* fix unique parameter naming

* add tests
This commit is contained in:
William Cheng
2021-02-23 14:49:44 +08:00
committed by GitHub
parent e7f50f66a3
commit 42d3dbd5d1
3 changed files with 79 additions and 3 deletions

View File

@@ -3778,7 +3778,7 @@ public class DefaultCodegen implements CodegenConfig {
// ensure unique params
if (ensureUniqueParams) {
if (!isParameterNameUnique(p, allParams)) {
while (!isParameterNameUnique(p, allParams)) {
p.paramName = generateNextName(p.paramName);
}
}
@@ -3804,7 +3804,7 @@ public class DefaultCodegen implements CodegenConfig {
if (!prependFormOrBodyParameters) {
for (CodegenParameter cp : formParams) {
if (ensureUniqueParams) {
if (!isParameterNameUnique(cp, allParams)) {
while (!isParameterNameUnique(cp, allParams)) {
cp.paramName = generateNextName(cp.paramName);
}
}
@@ -3813,7 +3813,7 @@ public class DefaultCodegen implements CodegenConfig {
for (CodegenParameter cp : bodyParams) {
if (ensureUniqueParams) {
if (!isParameterNameUnique(cp, allParams)) {
while (!isParameterNameUnique(cp, allParams)) {
cp.paramName = generateNextName(cp.paramName);
}
}

View File

@@ -24,6 +24,7 @@ import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.GoDeprecatedClientCodegen;
import org.openapitools.codegen.languages.GoClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -71,6 +72,27 @@ public class GoClientCodegenTest {
Assert.assertFalse(bp.isPrimitiveType);
}
@Test(description = "test to ensrue the paramter names are unique")
public void ensureParameterNameUniqueTest() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/conflictingParameter.yaml");
final GoClientCodegen codegen = new GoClientCodegen();
codegen.setOpenAPI(openAPI);
final String path = "/pet/{id}";
final Operation p = openAPI.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, null);
Assert.assertEquals(op.allParams.size(), 9);
CodegenParameter cp = op.allParams.get(0);
Assert.assertEquals(cp.paramName, "id");
CodegenParameter cp2 = op.allParams.get(1);
Assert.assertEquals(cp2.paramName, "id2");
CodegenParameter cp3 = op.allParams.get(2);
Assert.assertEquals(cp3.paramName, "id3");
CodegenParameter cp4 = op.allParams.get(3);
Assert.assertEquals(cp4.paramName, "id4");
CodegenParameter cp5 = op.allParams.get(4);
Assert.assertEquals(cp5.paramName, "id5");
}
@Test
public void testFilenames() throws Exception {
final GoDeprecatedClientCodegen codegen = new GoDeprecatedClientCodegen();

View File

@@ -0,0 +1,54 @@
openapi: 3.0.0
info:
version: 1.0.0
title: OpenAPI Petstore
paths:
/pet/{id}:
post:
operationId: create
parameters:
- in: path
name: id
required: true
schema:
type: integer
format: int32
- in: header
name: id
schema:
type: integer
format: int32
- in: cookie
name: id
schema:
type: integer
format: int32
- in: query
name: id
schema:
type: integer
format: int32
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
id:
type: integer
format: int32
pathid:
type: integer
format: int32
headerId2:
type: integer
format: int32
cookieId2:
type: integer
format: int32
queryId2:
type: integer
format: int32
responses:
'400':
description: Invalid pet value