forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug][kotlin] Fix compile of reserved word in client (OpenAPITools#5221)
* [kotlin] Fix compile of reserved word in client A number of places in the client code need to be escaped for reserved words. That is, this should be: (git log formatted): `as` rather than as (markdown formatted): \`as\` rather than `as` There are only a handful of places using `{{paramName}}` which HTML encodes backticks, rather than `{{{paramName}}}` which outputs literal values. Added unit test to maintain the reserved word standard for Kotlin. * don't use kotlin-codegen-escaped parameters in parameter-map Co-authored-by: Andreas Müller <[email protected]>
- Loading branch information
Showing
9 changed files
with
626 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/bodyParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isBodyParam}}@Body {{paramName}}: {{{dataType}}}{{/isBodyParam}} | ||
{{#isBodyParam}}@Body {{{paramName}}}: {{{dataType}}}{{/isBodyParam}} |
2 changes: 1 addition & 1 deletion
2
...pi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/formParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isFormParam}}{{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{paramName}}: {{{dataType}}}{{/isFile}}{{#isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} {{paramName}}: MultipartBody.Part {{/isFile}}{{/isFormParam}} | ||
{{#isFormParam}}{{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{{paramName}}}: {{{dataType}}}{{/isFile}}{{#isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} {{{paramName}}}: MultipartBody.Part {{/isFile}}{{/isFormParam}} |
2 changes: 1 addition & 1 deletion
2
...-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/headerParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isHeaderParam}}@Header("{{baseName}}") {{paramName}}: {{{dataType}}}{{/isHeaderParam}} | ||
{{#isHeaderParam}}@Header("{{baseName}}") {{{paramName}}}: {{{dataType}}}{{/isHeaderParam}} |
2 changes: 1 addition & 1 deletion
2
...pi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/pathParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isPathParam}}@Path("{{baseName}}") {{paramName}}: {{{dataType}}}{{/isPathParam}} | ||
{{#isPathParam}}@Path("{{baseName}}") {{{paramName}}}: {{{dataType}}}{{/isPathParam}} |
2 changes: 1 addition & 1 deletion
2
...i-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/queryParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isQueryParam}}@Query("{{baseName}}") {{paramName}}: {{#collectionFormat}}{{#isCollectionFormatMulti}}{{{dataType}}}{{/isCollectionFormatMulti}}{{^isCollectionFormatMulti}}{{{collectionFormat.toUpperCase}}}Params{{/isCollectionFormatMulti}}{{/collectionFormat}}{{^collectionFormat}}{{{dataType}}}{{/collectionFormat}}{{/isQueryParam}} | ||
{{#isQueryParam}}@Query("{{baseName}}") {{{paramName}}}: {{#collectionFormat}}{{#isCollectionFormatMulti}}{{{dataType}}}{{/isCollectionFormatMulti}}{{^isCollectionFormatMulti}}{{{collectionFormat.toUpperCase}}}Params{{/isCollectionFormatMulti}}{{/collectionFormat}}{{^collectionFormat}}{{{dataType}}}{{/collectionFormat}}{{/isQueryParam}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...napi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinReservedWordsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package org.openapitools.codegen.kotlin; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.Operation; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import io.swagger.v3.oas.models.parameters.Parameter; | ||
import org.openapitools.codegen.*; | ||
import org.openapitools.codegen.languages.KotlinClientCodegen; | ||
import org.openapitools.codegen.utils.StringUtils; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.HashSet; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public class KotlinReservedWordsTest { | ||
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/kotlin/reserved_words.yaml"); | ||
|
||
@DataProvider(name = "reservedWords") | ||
static Object[][] reservedWords() { | ||
return new Object[][]{ | ||
{"as"}, | ||
{"break"}, | ||
{"class"}, | ||
{"continue"}, | ||
{"do"}, | ||
{"else"}, | ||
{"false"}, | ||
{"for"}, | ||
{"fun"}, | ||
{"if"}, | ||
{"in"}, | ||
{"interface"}, | ||
{"is"}, | ||
{"null"}, | ||
{"object"}, | ||
{"package"}, | ||
{"return"}, | ||
{"super"}, | ||
{"this"}, | ||
{"throw"}, | ||
{"true"}, | ||
{"try"}, | ||
{"typealias"}, | ||
{"typeof"}, | ||
{"val"}, | ||
{"var"}, | ||
{"when"}, | ||
{"while"} | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "reservedWords") | ||
public void testReservedWordsAsModels(String reservedWord) { | ||
final DefaultCodegen codegen = new KotlinClientCodegen(); | ||
final Schema schema = new Schema(); | ||
final String escaped = "`" + reservedWord + "`"; | ||
final String titleCased = StringUtils.camelize(reservedWord, false); | ||
|
||
codegen.setOpenAPI(openAPI); | ||
CodegenModel model = codegen.fromModel(reservedWord, schema); | ||
|
||
assertEquals(model.classname, titleCased); | ||
if ("class".equals(reservedWord)) { | ||
// this is a really weird "edge" case rename. | ||
assertEquals(model.classVarName, "propertyClass"); | ||
} else { | ||
assertEquals(model.classVarName, escaped); | ||
} | ||
assertEquals(model.name, escaped); | ||
assertEquals(model.classFilename, titleCased); | ||
} | ||
|
||
@SuppressWarnings("OptionalGetWithoutIsPresent") | ||
@Test(dataProvider = "reservedWords") | ||
public void testReservedWordsAsParameters(String reservedWord) { | ||
final DefaultCodegen codegen = new KotlinClientCodegen(); | ||
final String escaped = "`" + reservedWord + "`"; | ||
codegen.setOpenAPI(openAPI); | ||
Operation operation = openAPI.getPaths().get("/ping").getGet(); | ||
|
||
Parameter current = operation.getParameters().stream().filter(x -> reservedWord.equals(x.getName())).findFirst().get(); | ||
CodegenParameter codegenParameter = codegen.fromParameter(current, new HashSet<>()); | ||
|
||
assertEquals(current.getName(), reservedWord); | ||
if ("class".equals(reservedWord)) { | ||
assertEquals(codegenParameter.paramName, "propertyClass"); | ||
} else { | ||
assertEquals(codegenParameter.paramName, escaped); | ||
} | ||
} | ||
|
||
@Test(dataProvider = "reservedWords") | ||
public void testReservedWordsAsProperties(String reservedWord) { | ||
final DefaultCodegen codegen = new KotlinClientCodegen(); | ||
|
||
final String escaped = "`" + reservedWord + "`"; | ||
final String titleCased = StringUtils.camelize(reservedWord, false); | ||
|
||
Schema linked = openAPI.getComponents().getSchemas().get("Linked"); | ||
|
||
CodegenProperty property = codegen.fromProperty(reservedWord, (Schema) linked.getProperties().get(reservedWord)); | ||
|
||
if ("object".equals(reservedWord)) { | ||
assertEquals(property.complexType, "kotlin.Any"); | ||
assertEquals(property.dataType, "kotlin.Any"); | ||
assertEquals(property.datatypeWithEnum, "kotlin.Any"); | ||
assertEquals(property.baseType, "kotlin.Any"); | ||
} else { | ||
assertEquals(property.complexType, titleCased); | ||
assertEquals(property.dataType, titleCased); | ||
assertEquals(property.datatypeWithEnum, titleCased); | ||
assertEquals(property.baseType, titleCased); | ||
} | ||
|
||
if ("class".equals(reservedWord)) { | ||
// this is a really weird "edge" case rename. | ||
assertEquals(property.name, "propertyClass"); | ||
} else { | ||
assertEquals(property.name, escaped); | ||
} | ||
|
||
assertEquals(property.baseName, reservedWord); | ||
} | ||
|
||
} |
Oops, something went wrong.