Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,6 @@ samples/client/jetbrains/adyen/checkout71/http/client/Apis/http-client.private.e

# Generated by the run-in-docker.sh
\?/

# AI agent caches
.junie
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
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.| |false|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', 'original', and 'bestEffortBacktick' (like 'original' but tries to wrap values in backticks before falling back to sanitizing, e.g. `name,asc` stays `name,asc` rather than becoming nameCommaAsc; useful for sort/order enums)| |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 @@ -100,6 +100,9 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
@Getter
@Setter
private boolean fixJacksonJsonTypeInfoInheritance = true;
@Getter
@Setter
private Boolean delegatePatternEnabled = false;

/**
* Constructs an instance of `KotlinServerCodegen`.
Expand Down Expand Up @@ -174,6 +177,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, getDelegatePatternEnabled());
}

@Override
Expand Down Expand Up @@ -307,6 +311,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 @@ -330,9 +345,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 @@ -347,6 +363,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 @@ -402,6 +428,8 @@ public static class Constants {
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 USE_TAGS = "useTags";
public static final String USE_TAGS_DESC = "use tags for creating interface and controller classnames.";
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
Loading