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.
[kotlin] Fix the path variable escaping in kotlin client generators (O…
- Loading branch information
1 parent
52610e0
commit eb92eeb
Showing
8 changed files
with
150 additions
and
6 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
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
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
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
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
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
104 changes: 104 additions & 0 deletions
104
...i-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.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,104 @@ | ||
package org.openapitools.codegen.kotlin; | ||
|
||
import io.swagger.parser.OpenAPIParser; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.parser.core.models.ParseOptions; | ||
import lombok.Getter; | ||
import org.jetbrains.kotlin.com.intellij.openapi.util.text.Strings; | ||
import org.openapitools.codegen.ClientOptInput; | ||
import org.openapitools.codegen.CodegenConstants; | ||
import org.openapitools.codegen.DefaultGenerator; | ||
import org.openapitools.codegen.languages.KotlinClientCodegen; | ||
import org.openapitools.codegen.languages.features.CXFServerFeatures; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import static org.openapitools.codegen.TestUtils.assertFileContains; | ||
|
||
public class KotlinClientCodegenApiTest { | ||
|
||
@DataProvider(name = "pathResponses") | ||
public Object[][] pathResponses() { | ||
return new Object[][]{ | ||
{ClientLibrary.JVM_KTOR}, | ||
{ClientLibrary.JVM_OKHTTP4}, | ||
{ClientLibrary.JVM_SPRING_WEBCLIENT}, | ||
{ClientLibrary.JVM_SPRING_RESTCLIENT}, | ||
{ClientLibrary.JVM_RETROFIT2}, | ||
{ClientLibrary.MULTIPLATFORM}, | ||
{ClientLibrary.JVM_VOLLEY}, | ||
{ClientLibrary.JVM_VERTX} | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "pathResponses") | ||
void testPathVariableIsNotEscaped_19930(ClientLibrary library) throws IOException { | ||
|
||
OpenAPI openAPI = new OpenAPIParser() | ||
.readLocation("src/test/resources/3_0/kotlin/issue19930-path-escaping.json", null, new ParseOptions()).getOpenAPI(); | ||
|
||
KotlinClientCodegen codegen = createCodegen(library); | ||
|
||
String outputPath = codegen.getOutputDir().replace('\\', '/'); | ||
ClientOptInput input = new ClientOptInput(); | ||
input.openAPI(openAPI); | ||
input.config(codegen); | ||
|
||
DefaultGenerator generator = new DefaultGenerator(); | ||
|
||
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false"); | ||
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false"); | ||
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false"); | ||
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true"); | ||
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false"); | ||
|
||
generator.opts(input).generate(); | ||
|
||
System.out.println(outputPath); | ||
|
||
assertFileContains(Paths.get(outputPath + "/src/" + library.getSourceRoot() + "/org/openapitools/client/apis/ArticleApi.kt"), "article('{Id}')"); | ||
} | ||
|
||
private KotlinClientCodegen createCodegen(ClientLibrary library) throws IOException { | ||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); | ||
output.deleteOnExit(); | ||
KotlinClientCodegen codegen = new KotlinClientCodegen(); | ||
codegen.setLibrary(library.getLibraryName()); | ||
codegen.setOutputDir(output.getAbsolutePath()); | ||
codegen.setSerializationLibrary(library.getSerializationLibrary()); | ||
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); | ||
codegen.additionalProperties().put(KotlinClientCodegen.USE_SPRING_BOOT3, "true"); | ||
codegen.additionalProperties().put(KotlinClientCodegen.DATE_LIBRARY, "kotlinx-datetime"); | ||
return codegen; | ||
} | ||
|
||
@Getter | ||
private enum ClientLibrary { | ||
JVM_KTOR("main/kotlin"), | ||
JVM_OKHTTP4("main/kotlin"), | ||
JVM_SPRING_WEBCLIENT("main/kotlin"), | ||
JVM_SPRING_RESTCLIENT("main/kotlin"), | ||
JVM_RETROFIT2("main/kotlin"), | ||
MULTIPLATFORM("commonMain/kotlin"), | ||
JVM_VOLLEY("gson", "main/java"), | ||
JVM_VERTX("main/kotlin"); | ||
private final String serializationLibrary; | ||
private final String libraryName; | ||
private final String sourceRoot; | ||
|
||
ClientLibrary(String serializationLibrary, String sourceRoot) { | ||
this.serializationLibrary = serializationLibrary; | ||
this.sourceRoot = sourceRoot; | ||
this.libraryName = Strings.toLowerCase(this.name()).replace("_", "-"); | ||
} | ||
|
||
ClientLibrary(String sourceRoot) { | ||
this("jackson", sourceRoot); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
modules/openapi-generator/src/test/resources/3_0/kotlin/issue19930-path-escaping.json
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,40 @@ | ||
{ | ||
"openapi": "3.0.1", | ||
"info": { | ||
"title": "", | ||
"description": "", | ||
"version": "1.0.0" | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "https://localhost:8080" | ||
} | ||
], | ||
"paths": { | ||
"/article('{Id}')": { | ||
"get": { | ||
"tags": [ | ||
"Article" | ||
], | ||
"parameters": [ | ||
{ | ||
"name": "Id", | ||
"in": "path", | ||
"description": "key: Id of Article", | ||
"required": true, | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Retrieved entity", | ||
"content": { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |