[kotlin-server] Add polymorphism, oneOf and allOf support (#22610)

* [kotlin-server] Add polymorphism support

* Fix CI triggers

* Fix FILES

* Fix samples and related triggers

* Fix FILES

* Add discriminator property to sealed class

* Fix double nullability issue

* Update samples
This commit is contained in:
Dennis Ameling
2026-02-04 10:02:02 +01:00
committed by GitHub
parent 3ecb49060e
commit 48b7c85cd4
67 changed files with 1831 additions and 11 deletions

View File

@@ -25,6 +25,7 @@ import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.templating.mustache.CamelCaseLambda;
@@ -68,6 +69,9 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
private boolean returnResponse = false;
@Setter
private boolean omitGradleWrapper = false;
@Getter
@Setter
private boolean fixJacksonJsonTypeInfoInheritance = true;
// 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>>()
@@ -106,6 +110,9 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
public KotlinServerCodegen() {
super();
// Enable proper oneOf/anyOf discriminator handling for polymorphism
legacyDiscriminatorBehavior = false;
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML))
@@ -120,7 +127,9 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
GlobalFeature.LinkObjects,
GlobalFeature.ParameterStyling
)
.excludeSchemaSupportFeatures(
.includeSchemaSupportFeatures(
SchemaSupportFeature.allOf,
SchemaSupportFeature.oneOf,
SchemaSupportFeature.Polymorphism
)
.excludeParameterFeatures(
@@ -166,6 +175,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
addSwitch(Constants.RETURN_RESPONSE, Constants.RETURN_RESPONSE_DESC, returnResponse);
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);
}
@Override
@@ -235,6 +245,11 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
setOmitGradleWrapper(Boolean.parseBoolean(additionalProperties.get(Constants.OMIT_GRADLE_WRAPPER).toString()));
}
if (additionalProperties.containsKey(Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE)) {
setFixJacksonJsonTypeInfoInheritance(Boolean.parseBoolean(additionalProperties.get(Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE).toString()));
}
additionalProperties.put(Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE, fixJacksonJsonTypeInfoInheritance);
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
// set default library to "ktor"
@@ -381,6 +396,291 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
public static final String OMIT_GRADLE_WRAPPER = "omitGradleWrapper";
public static final String OMIT_GRADLE_WRAPPER_DESC = "Whether to omit Gradle wrapper for creating a sub project.";
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.";
}
@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
objs = super.postProcessAllModels(objs);
// For libraries that use Jackson, set up parent-child relationships for discriminator children
// This enables proper polymorphism support with @JsonTypeInfo and @JsonSubTypes annotations
if (usesJacksonSerialization()) {
// Build a map of model name -> model for easy lookup
Map<String, CodegenModel> allModelsMap = new HashMap<>();
for (ModelsMap modelsMap : objs.values()) {
for (ModelMap modelMap : modelsMap.getModels()) {
CodegenModel model = modelMap.getModel();
allModelsMap.put(model.getClassname(), model);
}
}
// First pass: collect all discriminator parent -> children mappings
// Also identify the "true" discriminator owners (not inherited via allOf)
Map<String, String> childToParentMap = new HashMap<>();
Set<String> trueDiscriminatorOwners = new HashSet<>();
for (ModelsMap modelsMap : objs.values()) {
for (ModelMap modelMap : modelsMap.getModels()) {
CodegenModel model = modelMap.getModel();
if (model.getDiscriminator() != null && model.getDiscriminator().getMappedModels() != null
&& !model.getDiscriminator().getMappedModels().isEmpty()) {
String discriminatorPropBaseName = model.getDiscriminator().getPropertyBaseName();
for (CodegenDiscriminator.MappedModel mappedModel : model.getDiscriminator().getMappedModels()) {
childToParentMap.put(mappedModel.getModelName(), model.getClassname());
// If the mapping name equals the model name, check if we can derive
// a better mapping name from the child's discriminator property enum value
if (mappedModel.getMappingName().equals(mappedModel.getModelName())) {
CodegenModel childModel = allModelsMap.get(mappedModel.getModelName());
if (childModel != null) {
// Find the discriminator property in the child model
for (CodegenProperty prop : childModel.getAllVars()) {
if (prop.getBaseName().equals(discriminatorPropBaseName) && prop.isEnum) {
// If it's an enum with exactly one value, use that as the mapping name
Map<String, Object> allowableValues = prop.getAllowableValues();
if (allowableValues != null && allowableValues.containsKey("values")) {
@SuppressWarnings("unchecked")
List<Object> values = (List<Object>) allowableValues.get("values");
if (values != null && values.size() == 1) {
mappedModel.setMappingName(String.valueOf(values.get(0)));
}
}
}
}
}
}
}
// This model owns its discriminator (has mapped models)
trueDiscriminatorOwners.add(model.getClassname());
}
}
}
// Second pass: process child models
for (ModelsMap modelsMap : objs.values()) {
for (ModelMap modelMap : modelsMap.getModels()) {
CodegenModel model = modelMap.getModel();
String parentName = childToParentMap.get(model.getClassname());
if (parentName != null) {
// This model is a child of a discriminator parent
CodegenModel parentModel = allModelsMap.get(parentName);
// Set parent if not already set
if (model.getParent() == null) {
model.setParent(parentName);
}
// If this child has a discriminator but it's inherited (not a true owner),
// remove it - only the parent should have the discriminator annotations
if (model.getDiscriminator() != null && !trueDiscriminatorOwners.contains(model.getClassname())) {
model.setDiscriminator(null);
}
// For allOf pattern: if parent has properties, mark child's inherited properties
// Skip this for oneOf/anyOf patterns where parent properties are merged from children
boolean parentIsOneOfOrAnyOf = parentModel != null
&& ((parentModel.oneOf != null && !parentModel.oneOf.isEmpty())
|| (parentModel.anyOf != null && !parentModel.anyOf.isEmpty()));
if (parentModel != null && parentModel.getHasVars() && !parentIsOneOfOrAnyOf) {
Set<String> parentPropNames = new HashSet<>();
List<String> inheritedPropNamesList = new ArrayList<>();
for (CodegenProperty parentProp : parentModel.getAllVars()) {
parentPropNames.add(parentProp.getBaseName());
inheritedPropNamesList.add(parentProp.getName());
}
// Mark properties inherited from parent
for (CodegenProperty prop : model.getAllVars()) {
if (parentPropNames.contains(prop.getBaseName())) {
prop.isInherited = true;
}
}
for (CodegenProperty prop : model.getVars()) {
if (parentPropNames.contains(prop.getBaseName())) {
prop.isInherited = true;
}
}
for (CodegenProperty prop : model.getRequiredVars()) {
if (parentPropNames.contains(prop.getBaseName())) {
prop.isInherited = true;
}
}
for (CodegenProperty prop : model.getOptionalVars()) {
if (parentPropNames.contains(prop.getBaseName())) {
prop.isInherited = true;
}
}
// Set vendor extension for parent constructor call with inherited properties
if (!inheritedPropNamesList.isEmpty()) {
String parentCtorArgs = String.join(", ", inheritedPropNamesList.stream()
.map(name -> name + " = " + name)
.toArray(String[]::new));
model.getVendorExtensions().put("x-parent-ctor-args", parentCtorArgs);
}
}
}
}
}
// Third pass: set vendor extension for discriminator style and handle fixJacksonJsonTypeInfoInheritance
for (String ownerName : trueDiscriminatorOwners) {
CodegenModel owner = allModelsMap.get(ownerName);
if (owner != null && owner.getDiscriminator() != null) {
String discriminatorPropBaseName = owner.getDiscriminator().getPropertyBaseName();
boolean isOneOfOrAnyOfPattern = (owner.oneOf != null && !owner.oneOf.isEmpty())
|| (owner.anyOf != null && !owner.anyOf.isEmpty());
// hasParentProperties controls whether the sealed class has properties in its constructor
// This should be false for oneOf/anyOf patterns (parent is a type union, no direct properties)
// and true for allOf patterns (parent has properties that children inherit)
boolean hasParentProperties = !isOneOfOrAnyOfPattern;
// visibleTrue controls whether visible=true is set on @JsonTypeInfo
// When fixJacksonJsonTypeInfoInheritance is true, we always set visible=true
// When false, we only set visible=true if the parent has properties (allOf pattern)
boolean visibleTrue;
if (fixJacksonJsonTypeInfoInheritance) {
// When fixJacksonJsonTypeInfoInheritance is true:
// 1. Always set visible=true so Jackson can read the discriminator
// 2. For oneOf/anyOf patterns: add discriminator property to parent and children
visibleTrue = true;
// For oneOf/anyOf patterns, add the discriminator property to the parent sealed class
// This allows accessing the discriminator value from the parent type directly
if (isOneOfOrAnyOfPattern) {
String discriminatorVarName = toVarName(discriminatorPropBaseName);
// Clear all merged properties from the oneOf parent - they belong to children only
// We'll add back just the discriminator property
owner.getVars().clear();
owner.getRequiredVars().clear();
owner.getOptionalVars().clear();
owner.getAllVars().clear();
// Add discriminator property to parent
CodegenProperty parentDiscriminatorProp = new CodegenProperty();
parentDiscriminatorProp.baseName = discriminatorPropBaseName;
parentDiscriminatorProp.name = discriminatorVarName;
parentDiscriminatorProp.dataType = "kotlin.String";
parentDiscriminatorProp.datatypeWithEnum = "kotlin.String";
parentDiscriminatorProp.required = true;
parentDiscriminatorProp.isNullable = false;
parentDiscriminatorProp.isReadOnly = false;
owner.getVars().add(parentDiscriminatorProp);
owner.getRequiredVars().add(parentDiscriminatorProp);
owner.getAllVars().add(parentDiscriminatorProp);
// Parent now has properties (just the discriminator)
hasParentProperties = true;
// Process children: mark discriminator as inherited and set default values
for (CodegenDiscriminator.MappedModel mappedModel : owner.getDiscriminator().getMappedModels()) {
CodegenModel childModel = allModelsMap.get(mappedModel.getModelName());
if (childModel != null) {
boolean hasDiscriminatorProp = false;
String discriminatorDefault = "\"" + mappedModel.getMappingName() + "\"";
// Update existing discriminator property in all lists - mark as inherited
for (CodegenProperty prop : childModel.getVars()) {
if (prop.getBaseName().equals(discriminatorPropBaseName)) {
hasDiscriminatorProp = true;
prop.defaultValue = discriminatorDefault;
prop.dataType = "kotlin.String";
prop.datatypeWithEnum = "kotlin.String";
prop.required = true;
prop.isNullable = false;
prop.isInherited = true;
}
}
for (CodegenProperty prop : childModel.getAllVars()) {
if (prop.getBaseName().equals(discriminatorPropBaseName)) {
prop.defaultValue = discriminatorDefault;
prop.dataType = "kotlin.String";
prop.datatypeWithEnum = "kotlin.String";
prop.required = true;
prop.isNullable = false;
prop.isInherited = true;
}
}
// Move discriminator from optionalVars to requiredVars if needed
CodegenProperty propToMove = null;
for (CodegenProperty prop : childModel.getOptionalVars()) {
if (prop.getBaseName().equals(discriminatorPropBaseName)) {
prop.defaultValue = discriminatorDefault;
prop.dataType = "kotlin.String";
prop.datatypeWithEnum = "kotlin.String";
prop.required = true;
prop.isNullable = false;
prop.isInherited = true;
propToMove = prop;
break;
}
}
if (propToMove != null) {
childModel.getOptionalVars().remove(propToMove);
childModel.getRequiredVars().add(propToMove);
}
// Also update if it's already in requiredVars
for (CodegenProperty prop : childModel.getRequiredVars()) {
if (prop.getBaseName().equals(discriminatorPropBaseName)) {
prop.defaultValue = discriminatorDefault;
prop.dataType = "kotlin.String";
prop.datatypeWithEnum = "kotlin.String";
prop.isNullable = false;
prop.isInherited = true;
}
}
// If child doesn't have the discriminator property, add it as required and inherited
if (!hasDiscriminatorProp) {
CodegenProperty discriminatorProp = new CodegenProperty();
discriminatorProp.baseName = discriminatorPropBaseName;
discriminatorProp.name = discriminatorVarName;
discriminatorProp.dataType = "kotlin.String";
discriminatorProp.datatypeWithEnum = "kotlin.String";
discriminatorProp.defaultValue = discriminatorDefault;
discriminatorProp.required = true;
discriminatorProp.isNullable = false;
discriminatorProp.isReadOnly = false;
discriminatorProp.isInherited = true;
childModel.getVars().add(discriminatorProp);
childModel.getRequiredVars().add(discriminatorProp);
childModel.getAllVars().add(discriminatorProp);
}
// Set parent constructor args for the discriminator property
childModel.getVendorExtensions().put("x-parent-ctor-args",
discriminatorVarName + " = " + discriminatorVarName);
}
}
}
} else {
// When fixJacksonJsonTypeInfoInheritance is false:
// visible=true only for allOf pattern (parent has properties)
visibleTrue = hasParentProperties;
}
// Set on both model and discriminator so it's accessible in different template contexts
owner.getVendorExtensions().put("x-discriminator-has-parent-properties", hasParentProperties);
owner.getDiscriminator().getVendorExtensions().put("x-discriminator-has-parent-properties", hasParentProperties);
owner.getVendorExtensions().put("x-discriminator-visible-true", visibleTrue);
owner.getDiscriminator().getVendorExtensions().put("x-discriminator-visible-true", visibleTrue);
}
}
}
return objs;
}
@Override
@@ -462,6 +762,16 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
return Constants.JAVALIN5.equals(library) || Constants.JAVALIN6.equals(library);
}
/**
* Returns true if the current library uses Jackson for JSON serialization.
* This is used to determine if Jackson-specific features like polymorphism annotations should be enabled.
*/
private boolean usesJacksonSerialization() {
return Constants.JAVALIN5.equals(library) ||
Constants.JAVALIN6.equals(library) ||
Constants.JAXRS_SPEC.equals(library);
}
private boolean isKtor2Or3() {
return Constants.KTOR.equals(library) || Constants.KTOR2.equals(library);
}

View File

@@ -12,23 +12,38 @@ import java.io.Serializable
{{/isKtor}}
/**
* {{{description}}}
{{#isKtor}}
{{#vars}}
* @param {{{name}}} {{{description}}}
{{/vars}}
{{/isKtor}}
{{^isKtor}}
{{^discriminator}}
{{#vars}}
* @param {{{name}}} {{{description}}}
{{/vars}}
{{/discriminator}}
{{#discriminator}}
{{#vendorExtensions.x-discriminator-has-parent-properties}}
{{#vars}}
* @param {{{name}}} {{{description}}}
{{/vars}}
{{/vendorExtensions.x-discriminator-has-parent-properties}}
{{/discriminator}}
{{/isKtor}}
*/
{{#parcelizeModels}}
@Parcelize
{{/parcelizeModels}}
{{#isKtor}}
@Serializable
{{/isKtor}}
{{#hasVars}}data {{/hasVars}}class {{classname}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
){{^isKtor}}{{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}{{/isKtor}}
)
{{#vendorExtensions.x-has-data-class-body}}
{
{{/vendorExtensions.x-has-data-class-body}}
@@ -52,3 +67,53 @@ import java.io.Serializable
{{#vendorExtensions.x-has-data-class-body}}
}
{{/vendorExtensions.x-has-data-class-body}}
{{/isKtor}}
{{^isKtor}}
{{#discriminator}}
{{>typeInfoAnnotation}}
{{#vendorExtensions.x-discriminator-has-parent-properties}}
sealed class {{classname}}(
{{#requiredVars}}
{{>data_class_sealed_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_sealed_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
)
{{/vendorExtensions.x-discriminator-has-parent-properties}}
{{^vendorExtensions.x-discriminator-has-parent-properties}}
sealed class {{classname}}
{{/vendorExtensions.x-discriminator-has-parent-properties}}
{{/discriminator}}
{{^discriminator}}
{{#hasVars}}data {{/hasVars}}class {{classname}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
){{#parent}} : {{{.}}}({{#vendorExtensions.x-parent-ctor-args}}{{{.}}}{{/vendorExtensions.x-parent-ctor-args}}){{/parent}}{{^parent}}{{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}{{/parent}}
{{#vendorExtensions.x-has-data-class-body}}
{
{{/vendorExtensions.x-has-data-class-body}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
/**
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{{nameInPascalCase}}}(val value: {{{dataType}}}){
{{#allowableValues}}
{{#enumVars}}
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/vars}}
{{/hasEnums}}
{{#vendorExtensions.x-has-data-class-body}}
}
{{/vendorExtensions.x-has-data-class-body}}
{{/discriminator}}
{{/isKtor}}

View File

@@ -0,0 +1,4 @@
{{#description}}
/* {{{.}}} */
{{/description}}
open val {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{^isNullable}}{{^required}}?{{/required}}{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}{{^defaultValue}}{{^required}} = null{{/required}}{{/defaultValue}}

View File

@@ -0,0 +1,6 @@
{{#description}}
/* {{{.}}} */
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
open {{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

View File

@@ -3,4 +3,4 @@
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}

View File

@@ -3,4 +3,4 @@
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

View File

@@ -0,0 +1,6 @@
{{#description}}
/* {{{.}}} */
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
open {{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

View File

@@ -0,0 +1,5 @@
{{#description}}
/* {{{.}}} */
{{/description}}
@get:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
open val {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{^isNullable}}{{^required}}?{{/required}}{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}{{^defaultValue}}{{^required}} = null{{/required}}{{/defaultValue}}

View File

@@ -3,4 +3,4 @@
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}

View File

@@ -3,4 +3,4 @@
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

View File

@@ -0,0 +1,6 @@
{{#description}}
/* {{{.}}} */
{{/description}}
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}
@field:com.fasterxml.jackson.annotation.JsonProperty("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}")
open {{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

View File

@@ -0,0 +1,6 @@
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = {{#vendorExtensions.x-discriminator-visible-true}}true{{/vendorExtensions.x-discriminator-visible-true}}{{^vendorExtensions.x-discriminator-visible-true}}false{{/vendorExtensions.x-discriminator-visible-true}})
@com.fasterxml.jackson.annotation.JsonSubTypes(
{{#discriminator.mappedModels}}
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = {{modelName}}::class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{^-last}},{{/-last}}
{{/discriminator.mappedModels}}
)

View File

@@ -35,7 +35,6 @@ import static org.openapitools.codegen.languages.features.BeanValidationFeatures
public class KotlinServerCodegenTest {
@Test
public void enumDescription() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
@@ -311,4 +310,203 @@ public class KotlinServerCodegenTest {
Assert.assertTrue(syntaxErrorListener.getSyntaxErrorCount() == 0);
Assert.assertTrue(customKotlinParseListener.getStringReferenceCount() == 0);
}
// ==================== Polymorphism and Discriminator Tests ====================
@Test
public void oneOfWithDiscriminator_generatesSealedClassWithDiscriminatorProperty() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism-and-discriminator.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petModel = Paths.get(outputPath + "/models/Pet.kt");
// Pet should be a sealed class with Jackson polymorphism annotations and discriminator property
assertFileContains(
petModel,
"sealed class Pet(",
"open val petType: kotlin.String",
"@com.fasterxml.jackson.annotation.JsonTypeInfo",
"property = \"petType\"",
"visible = true",
"@com.fasterxml.jackson.annotation.JsonSubTypes"
);
}
@Test
public void oneOfWithDiscriminator_generatesChildrenWithOverrideDiscriminatorProperty() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism-and-discriminator.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
// Cat should have petType as overridden non-nullable String with default value
Path catModel = Paths.get(outputPath + "/models/Cat.kt");
assertFileContains(
catModel,
"data class Cat(",
"override val petType: kotlin.String = \"cat\"",
") : Pet(petType = petType)"
);
// Should NOT be nullable
assertFileNotContains(
catModel,
"petType: kotlin.String?",
"petType: kotlin.Any"
);
// Dog should have petType as overridden non-nullable String with default value
Path dogModel = Paths.get(outputPath + "/models/Dog.kt");
assertFileContains(
dogModel,
"data class Dog(",
"override val petType: kotlin.String = \"dog\"",
") : Pet(petType = petType)"
);
// Should NOT be nullable
assertFileNotContains(
dogModel,
"petType: kotlin.String?",
"petType: kotlin.Any"
);
}
@Test
public void allOfWithDiscriminator_generatesSealedClassWithProperties() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism-allof-and-discriminator.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petModel = Paths.get(outputPath + "/models/Pet.kt");
// Pet should be a sealed class WITH properties (allOf pattern)
assertFileContains(
petModel,
"sealed class Pet(",
"open val name: kotlin.String",
"open val petType: kotlin.String",
"@com.fasterxml.jackson.annotation.JsonTypeInfo",
"visible = true"
);
}
@Test
public void allOfWithDiscriminator_generatesChildrenWithOverrideProperties() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism-allof-and-discriminator.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
// Cat should use override for inherited properties and pass them to parent constructor
Path catModel = Paths.get(outputPath + "/models/Cat.kt");
assertFileContains(
catModel,
"data class Cat(",
"override val name: kotlin.String",
"override val petType: kotlin.String",
") : Pet(name = name, petType = petType)"
);
// Dog should use override for inherited properties and pass them to parent constructor
Path dogModel = Paths.get(outputPath + "/models/Dog.kt");
assertFileContains(
dogModel,
"data class Dog(",
"override val name: kotlin.String",
"override val petType: kotlin.String",
") : Pet(name = name, petType = petType)"
);
}
@Test
public void polymorphismWithoutDiscriminator_generatesRegularDataClass() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petModel = Paths.get(outputPath + "/models/Pet.kt");
// Without discriminator, Pet should be a regular data class (not sealed)
assertFileContains(
petModel,
"data class Pet("
);
assertFileNotContains(
petModel,
"sealed class",
"@com.fasterxml.jackson.annotation.JsonTypeInfo",
"@com.fasterxml.jackson.annotation.JsonSubTypes"
);
}
@Test
public void fixJacksonJsonTypeInfoInheritance_canBeDisabled() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
codegen.additionalProperties().put(KotlinServerCodegen.Constants.FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE, false);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/polymorphism-and-discriminator.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petModel = Paths.get(outputPath + "/models/Pet.kt");
// When fixJacksonJsonTypeInfoInheritance is false and parent has no properties,
// visible should be false for oneOf pattern
assertFileContains(
petModel,
"visible = false"
);
}
}

View File

@@ -0,0 +1,52 @@
# https://spec.openapis.org/oas/v3.2.0.html#models-with-polymorphism-support-using-allof-and-a-discriminator-object
# 4.24.8.9 Models with Polymorphism Support using allOf and a Discriminator Object
# It is also possible to describe polymorphic models using allOf. The following example uses allOf with a Discriminator Object to describe a polymorphic Pet model.
openapi: 3.1.0
info:
title: Polymorphism example with allOf and discriminator
version: "1.0"
paths: {}
components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
properties:
name:
type: string
petType:
type: string
required:
- name
- petType
Cat: # "Cat" will be used as the discriminating value
description: A representation of a cat
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog: # "Dog" will be used as the discriminating value
description: A representation of a dog
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize

View File

@@ -0,0 +1,57 @@
# https://spec.openapis.org/oas/v3.2.0.html#models-with-polymorphism-support-and-a-discriminator-object
# 4.24.8.8 Models with Polymorphism Support and a Discriminator Object
# The following example extends the example of the previous section by adding a Discriminator Object to the Pet schema. Note that the Discriminator Object is only a hint to the consumer of the API and does not change the validation outcome of the schema.
openapi: 3.1.0
info:
title: Basic polymorphism example with discriminator
version: "1.0"
paths: {}
components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
mapping:
cat: '#/components/schemas/Cat'
dog: '#/components/schemas/Dog'
properties:
name:
type: string
required:
- name
- petType
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
Cat:
description: A pet cat
type: object
properties:
petType:
const: 'cat'
huntingSkill:
type: string
description: The measured skill for hunting
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog:
description: A pet dog
type: object
properties:
petType:
const: 'dog'
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- petType
- packSize

View File

@@ -0,0 +1,50 @@
# https://spec.openapis.org/oas/v3.2.0.html#models-with-polymorphism-support
# 4.24.8.7 Models with Polymorphism Support
# The following example describes a Pet model that can represent either a cat or a dog, as distinguished by the petType property. Each type of pet has other properties beyond those of the base Pet model. An instance without a petType property, or with a petType property that does not match either cat or dog, is invalid.
openapi: 3.1.0
info:
title: Basic polymorphism example without discriminator
version: "1.0"
components:
schemas:
Pet:
type: object
properties:
name:
type: string
required:
- name
- petType
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
Cat:
description: A pet cat
type: object
properties:
petType:
const: 'cat'
huntingSkill:
type: string
description: The measured skill for hunting
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog:
description: A pet dog
type: object
properties:
petType:
const: 'dog'
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize