Skip to content

Commit af15071

Browse files
committed
Ensure that the parameter names in the path are valid kotlin names
1 parent 416e199 commit af15071

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/main/java/io/swagger/codegen/languages/kotlin/KotlinServerCodegen.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package io.swagger.codegen.languages.kotlin;
22

3+
import io.swagger.v3.oas.models.Operation;
4+
import io.swagger.v3.oas.models.OpenAPI;
5+
import io.swagger.v3.oas.models.media.Schema;
6+
import io.swagger.codegen.CodegenOperation;
7+
import io.swagger.v3.oas.models.parameters.Parameter;
8+
39
import io.swagger.codegen.CliOption;
410
import io.swagger.codegen.CodegenConstants;
511
import io.swagger.codegen.CodegenType;
@@ -19,6 +25,8 @@
1925
public class KotlinServerCodegen extends AbstractKotlinCodegen {
2026

2127
public static final String DEFAULT_LIBRARY = Constants.KTOR;
28+
public static final String GENERATE_APIS = "generateApis";
29+
2230
static Logger LOGGER = LoggerFactory.getLogger(KotlinServerCodegen.class);
2331
private Boolean autoHeadFeatureEnabled = true;
2432
private Boolean conditionalHeadersFeatureEnabled = false;
@@ -120,6 +128,23 @@ public CodegenType getTag() {
120128
return CodegenType.SERVER;
121129
}
122130

131+
@Override
132+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> schemas, OpenAPI openAPI) {
133+
134+
// Ensure that the parameter names in the path are valid kotlin names
135+
// they need to match the names in the generated data class, this is required by ktor Location
136+
String modifiedPath = path;
137+
if (operation.getParameters() != null) {
138+
for (Parameter param : operation.getParameters()) {
139+
String pathParamName = param.getName();
140+
String kotlinName = toVarName(pathParamName);
141+
modifiedPath = modifiedPath.replace("{" + pathParamName + "}", "{" + kotlinName + "}");
142+
}
143+
}
144+
145+
return super.fromOperation(modifiedPath, httpMethod, operation, schemas, openAPI);
146+
}
147+
123148
@Override
124149
public void processOpts() {
125150
super.processOpts();
@@ -131,8 +156,8 @@ public void processOpts() {
131156
embeddedTemplateDir = templateDir = String.format("%s/kotlin-server", DEFAULT_TEMPLATE_VERSION);
132157
}
133158

134-
if (!additionalProperties.containsKey("generateApis")) {
135-
additionalProperties.put("generateApis", true);
159+
if (!additionalProperties.containsKey(GENERATE_APIS)) {
160+
additionalProperties.put(GENERATE_APIS, true);
136161
}
137162

138163
if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
@@ -173,6 +198,8 @@ public void processOpts() {
173198

174199
String packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
175200
String resourcesFolder = "src/main/resources"; // not sure this can be user configurable.
201+
202+
Boolean generateApis = additionalProperties.containsKey(GENERATE_APIS) && (Boolean)additionalProperties.get(GENERATE_APIS);
176203

177204
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
178205
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
@@ -184,7 +211,9 @@ public void processOpts() {
184211
supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
185212
supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt"));
186213

187-
supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt"));
214+
if (generateApis) {
215+
supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt"));
216+
}
188217

189218
supportingFiles.add(new SupportingFile("application.conf.mustache", resourcesFolder, "application.conf"));
190219
supportingFiles.add(new SupportingFile("logback.xml", resourcesFolder, "logback.xml"));

0 commit comments

Comments
 (0)