Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bin/configs/kotlin-server-ktor-delegate-pattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: kotlin-server
outputDir: samples/server/petstore/kotlin-server/ktor-delegate-pattern/generated
Comment thread
wing328 marked this conversation as resolved.
library: ktor
inputSpec: modules/openapi-generator/src/test/resources/3_1/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-server
additionalProperties:
hideGenerationTimestamp: "true"
serializableModel: "true"
delegatePattern: "true"
1 change: 1 addition & 0 deletions docs/generators/kotlin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|apiSuffix|suffix for api classes| |Api|
|artifactId|Generated artifact id (name of jar).| |kotlin-server|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|delegatePattern|Whether to generate the server files using the delegate pattern. This option is currently supported only when using ktor library.| |true|
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |original|
|featureAutoHead|Automatically provide responses to HEAD requests for existing routes that have the GET verb defined.| |true|
|featureCORS|Ktor by default provides an interceptor for implementing proper support for Cross-Origin Resource Sharing (CORS). See enable-cors.org.| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
@Getter
@Setter
private boolean fixJacksonJsonTypeInfoInheritance = true;
@Getter
@Setter
private Boolean delegatePatternEnabled = false;

// This is here to potentially warn the user when an option is not supported by the target framework.
private Map<String, List<String>> optionsSupportedPerFramework = new ImmutableMap.Builder<String, List<String>>()
Expand Down Expand Up @@ -176,6 +179,7 @@ public KotlinServerCodegen() {
addSwitch(Constants.OMIT_GRADLE_WRAPPER, Constants.OMIT_GRADLE_WRAPPER_DESC, omitGradleWrapper);
addSwitch(USE_JAKARTA_EE, Constants.USE_JAKARTA_EE_DESC, useJakartaEe);
addSwitch(Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE, Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE_DESC, fixJacksonJsonTypeInfoInheritance);
addSwitch(Constants.DELEGATE_PATTERN, Constants.DELEGATE_PATTERN_DESC, getAutoHeadFeatureEnabled());
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
}

@Override
Expand Down Expand Up @@ -305,6 +309,17 @@ public void processOpts() {
} else {
additionalProperties.put(Constants.METRICS, getMetricsFeatureEnabled());
}
if(isKtor()){
if (additionalProperties.containsKey(Constants.DELEGATE_PATTERN)) {
setDelegatePatternEnabled(convertPropertyToBooleanAndWriteBack(Constants.DELEGATE_PATTERN));
} else {
additionalProperties.put(Constants.DELEGATE_PATTERN, getDelegatePatternEnabled());
}
if (delegatePatternEnabled) {
typeMapping.put("file", "io.ktor.http.content.PartData.FileItem");
importMapping.put("io.ktor.http.content.PartData.FileItem", "io.ktor.http.content.PartData.FileItem");
}
}

boolean generateApis = additionalProperties.containsKey(CodegenConstants.GENERATE_APIS) && (Boolean) additionalProperties.get(CodegenConstants.GENERATE_APIS);
String packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
Expand All @@ -328,9 +343,10 @@ public void processOpts() {
if (isKtor2Or3()) {
additionalProperties.put(Constants.IS_KTOR, true);

supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt"));

if(!delegatePatternEnabled) {
supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt"));
}
if (generateApis && resourcesFeatureEnabled) {
supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt"));
}
Expand All @@ -345,6 +361,16 @@ public void processOpts() {
if (!getOmitGradleWrapper()) {
supportingFiles.add(new SupportingFile("gradle-wrapper.properties", "gradle" + File.separator + "wrapper", "gradle-wrapper.properties"));
}
if(isKtor()){
supportingFiles.add(new SupportingFile("AllApis.kt.mustache", packageFolder, "AllApis.kt"));
if(delegatePatternEnabled){
apiTemplateFiles.put("apiDelegate.mustache", "Delegate.kt");
supportingFiles.add(new SupportingFile("Delegates.kt.mustache", infrastructureFolder, "Delegates.kt"));
supportingFiles.add(new SupportingFile("AppDelegates.kt.mustache", infrastructureFolder, "AppDelegates.kt"));
supportingFiles.add(new SupportingFile("BadParameterException.kt.mustache", infrastructureFolder, "BadParameterException.kt"));
supportingFiles.add(new SupportingFile("APINotImplementedException.kt.mustache", infrastructureFolder, "APINotImplementedException.kt"));
}
}

} else if (isJavalin()) {
supportingFiles.add(new SupportingFile("Main.kt.mustache", packageFolder, "Main.kt"));
Expand Down Expand Up @@ -398,6 +424,8 @@ public static class Constants {
public static final String IS_KTOR = "isKtor";
public static final String FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE = "fixJacksonJsonTypeInfoInheritance";
public static final String FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE_DESC = "When true (default), ensures Jackson polymorphism works correctly by: (1) always setting visible=true on @JsonTypeInfo, and (2) adding the discriminator property to child models with appropriate default values. When false, visible is only set to true if all children already define the discriminator property.";
public static final String DELEGATE_PATTERN = "delegatePattern";
public static final String DELEGATE_PATTERN_DESC = "Whether to generate the server files using the delegate pattern. This option is currently supported only when using ktor library.";
}

@Override
Expand Down
Loading