Skip to content

Commit

Permalink
Refactor generating kotlin POJO's
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Nov 30, 2024
1 parent ede98e3 commit d60192d
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 138 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ private static List<String> normalizeExtraAnnotations(String prefix, Collection<
}

private static String normalizeExtraAnnotation(String prefix, String annotationStr) {
if (annotationStr.startsWith(prefix)) {
return annotationStr;
}
return prefix + (annotationStr.startsWith("@") ? annotationStr.substring(1) : annotationStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{#isContainer}}
// Is a container wrapped = {{isXmlWrapped}}
{{#items}}
// items.name = {{name}} items.baseName = {{baseName}} items.xmlName = {{xmlName}} items.xmlNamespace = {{xmlNamespace}}
// items.name = {{{name}}} items.baseName = {{baseName}} items.xmlName = {{xmlName}} items.xmlNamespace = {{xmlNamespace}}
// items.example = {{example}} items.type = {{dataType}}
@{{{vendorExtensions.fieldAnnPrefix}}}XmlElement({{#xmlNamespace}}namespace = "{{xmlNamespace}}", {{/xmlNamespace}}name = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/items}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{/vendorExtensions.x-class-extra-annotation}}
{{/formatNoEmptyLines}}
{{!Declare the class with extends and implements}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#hasChildren}}open {{/hasChildren}}{{^hasChildren}}{{#hasVars}}data {{/hasVars}}{{/hasChildren}}class {{classname}}{{#hasVars}}({{/hasVars}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#hasChildren}}open {{/hasChildren}}{{vendorExtensions.dataClass}}class {{classname}}{{#hasVars}}({{/hasVars}}
{{#vendorExtensions.requiredVarsWithoutDiscriminator}}

{{#formatNoEmptyLines}}
Expand All @@ -66,34 +66,9 @@
{{/deprecated}}
{{/description}}
{{>common/model/field_annotations}}
{{#vendorExtensions.overridden}}override {{/vendorExtensions.overridden}}{{^vendorExtensions.overridden}}{{#hasChildren}}open {{/hasChildren}}{{/vendorExtensions.overridden}}var {{{name}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}},
{{^vendorExtensions.isParentVar}}{{#vendorExtensions.overridden}}override {{/vendorExtensions.overridden}}{{^vendorExtensions.overridden}}{{#hasChildren}}open {{/hasChildren}}{{/vendorExtensions.overridden}}var {{/vendorExtensions.isParentVar}}{{{name}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}},
{{/formatNoEmptyLines}}
{{/vendorExtensions.requiredVarsWithoutDiscriminator}}
{{#vendorExtensions.withInheritance}}
{{#vendorExtensions.optionalVars}}

{{#formatNoEmptyLines}}
{{#description}}
/**
* {{description}}
{{#deprecated}}
*
* @deprecated{{#vendorExtensions.x-deprecated-message}} {{{.}}}{{/vendorExtensions.x-deprecated-message}}
{{/deprecated}}
*/
{{/description}}
{{^description}}
{{#deprecated}}
/**
* @deprecated{{#vendorExtensions.x-deprecated-message}} {{{.}}}{{/vendorExtensions.x-deprecated-message}}
*/
{{/deprecated}}
{{/description}}
{{>common/model/field_annotations}}
{{#vendorExtensions.overridden}}override {{/vendorExtensions.overridden}}{{^vendorExtensions.overridden}}{{#vendorExtensions.hasChildren}}open {{/vendorExtensions.hasChildren}}{{/vendorExtensions.overridden}}var {{{name}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} = {{#vendorExtensions.defaultValueInit}}{{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}null{{/vendorExtensions.defaultValueInit}},
{{/formatNoEmptyLines}}
{{/vendorExtensions.optionalVars}}
{{/vendorExtensions.withInheritance}}
{{#hasVars}}){{/hasVars}}{{#parent}} : {{{parent}}}({{#vendorExtensions.requiredParentVarsWithoutDiscriminator}}{{{name}}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVarsWithoutDiscriminator}}){{/parent}}{{#vendorExtensions.x-implements}}{{#parent}}, {{/parent}}{{^parent}}: {{/parent}}{{^-first}}, {{/-first}}{{{.}}}{{/vendorExtensions.x-implements}} {{openbrace}}

{{#vendorExtensions.withInheritance}}
Expand All @@ -117,7 +92,7 @@
}

override fun hashCode(): Int =
Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#vendorExtensions.hasOwnVars}}, {{/vendorExtensions.hasOwnVars}}super.hashCode(){{/parent}})
Objects.hash({{#vars}}{{^isByteArray}}{{{name}}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{{name}}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#vendorExtensions.hasOwnVars}}, {{/vendorExtensions.hasOwnVars}}super.hashCode(){{/parent}})

override fun toString(): String =
{{#hasVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ void testDiscriminatorWithoutUseOneOfInterfaces() {

var codegen = new JavaMicronautClientCodegen();
codegen.setUseOneOfInterfaces(false);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discirminator2.yml", CodegenConstants.MODELS);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discriminator2.yml", CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "model/JsonOp.java",
Expand Down Expand Up @@ -677,7 +677,7 @@ void testDiscriminatorWithUseOneOfInterfaces() {

var codegen = new JavaMicronautClientCodegen();
codegen.setUseOneOfInterfaces(true);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discirminator2.yml", CodegenConstants.MODELS);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discriminator2.yml", CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "model/JsonOp.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,27 @@ void testReadOnlyConstructorBug() {
assertFileContains(apiPath + "BookInfo.kt",
"""
open class BookInfo(
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_NAME)
open var name: String,
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_REQUIRED_READ_ONLY)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var requiredReadOnly: String?,
open var requiredReadOnly: String? = null,
@field:Nullable
@field:Size(min = 3)
@field:JsonProperty(JSON_PROPERTY_AUTHOR)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var author: String? = null,
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_OPTIONAL_READ_ONLY)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var optionalReadOnly: String? = null,
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_TYPE)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
Expand All @@ -337,19 +342,38 @@ open class BookInfo(
""");
assertFileContains(apiPath + "ExtendedBookInfo.kt",
"""
data class ExtendedBookInfo(
class ExtendedBookInfo(
@field:NotNull
@field:Pattern(regexp = "[0-9]{13}")
@field:JsonProperty(JSON_PROPERTY_ISBN)
var isbn: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_NAME)
override var name: String,
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_REQUIRED_READ_ONLY)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
override var requiredReadOnly: String?,
) : BookInfo(name, requiredReadOnly) {
@NotNull
@JsonProperty(JSON_PROPERTY_NAME)
name: String,
@Nullable
@JsonProperty(JSON_PROPERTY_REQUIRED_READ_ONLY)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
requiredReadOnly: String? = null,
@Nullable
@Size(min = 3)
@JsonProperty(JSON_PROPERTY_AUTHOR)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
author: String? = null,
@Nullable
@JsonProperty(JSON_PROPERTY_OPTIONAL_READ_ONLY)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
optionalReadOnly: String? = null,
@Nullable
@JsonProperty(JSON_PROPERTY_TYPE)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
type: BookInfoType? = null,
) : BookInfo(name, requiredReadOnly, author, optionalReadOnly, type) {
""");
}

Expand Down Expand Up @@ -397,47 +421,56 @@ void testDiscriminatorConstructorBug() {
assertFileContains(apiPath + "BookInfo.kt",
"""
open class BookInfo(
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_NAME)
open var name: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_TYPE)
open var type: BookInfoType? = null,
) {""");
) {
""");
assertFileContains(apiPath + "BasicBookInfo.kt",
"""
open class BasicBookInfo(
@field:NotNull
@field:Size(min = 3)
@field:JsonProperty(JSON_PROPERTY_AUTHOR)
open var author: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_NAME)
override var name: String,
) : BookInfo(name) {
@NotNull
@JsonProperty(JSON_PROPERTY_NAME)
name: String,
@Nullable
@JsonProperty(JSON_PROPERTY_TYPE)
type: BookInfoType? = null,
) : BookInfo(name, type) {
""");
assertFileContains(apiPath + "DetailedBookInfo.kt",
"""
data class DetailedBookInfo(
class DetailedBookInfo(
@field:NotNull
@field:Pattern(regexp = "[0-9]{13}")
@field:JsonProperty(JSON_PROPERTY_ISBN)
var isbn: String,
@field:NotNull
@field:Size(min = 3)
@field:JsonProperty(JSON_PROPERTY_AUTHOR)
override var author: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_NAME)
override var name: String,
) : BasicBookInfo(author, name)
@NotNull
@Size(min = 3)
@JsonProperty(JSON_PROPERTY_AUTHOR)
author: String,
@NotNull
@JsonProperty(JSON_PROPERTY_NAME)
name: String,
@Nullable
@JsonProperty(JSON_PROPERTY_TYPE)
type: BookInfoType? = null,
) : BasicBookInfo(author, name, type) {
""");
}

Expand Down Expand Up @@ -562,8 +595,8 @@ void testOneOf() {
String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-with-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/Subject.kt", "val typeCode: String");
assertFileContains(path + "model/Person.kt", "override var typeCode: String = \"PERS\",");
assertFileContains(path + "model/Subject.kt", "val typeCode: String?");
assertFileContains(path + "model/Person.kt", "override var typeCode: String? = \"PERS\",");
}

@Test
Expand All @@ -586,11 +619,11 @@ void testDiscriminatorCustomType() {
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/CancellationReasonTypesV2.kt", """
@field:NotNull
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_VERSION)
override var version: Int,
override var version: Int? = null,
""");
assertFileContains(path + "model/CancellationReasonTypesDTO.kt", "val version: Int");
assertFileContains(path + "model/CancellationReasonTypesDTO.kt", "val version: Int?");
}

@Test
Expand Down Expand Up @@ -675,15 +708,17 @@ void testDiscriminatorWithoutUseOneOfInterfaces() {

var codegen = new KotlinMicronautClientCodegen();
codegen.setUseOneOfInterfaces(false);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discirminator2.yml", CodegenConstants.MODELS);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discriminator2.yml", CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/JsonOp.kt",
"""
open class JsonOp(
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_PATH)
open var path: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_OP)
open var op: String,
Expand All @@ -698,18 +733,21 @@ open class JsonOp(

assertFileContains(path + "model/OpAdd.kt",
"""
data class OpAdd(
class OpAdd(
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_VALUE)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var `value`: String? = null,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_PATH)
override var path: String,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_OP)
override var op: String,
) : JsonOp(path, op) {
@NotNull
@JsonProperty(JSON_PROPERTY_PATH)
path: String,
@NotNull
@JsonProperty(JSON_PROPERTY_OP)
op: String,
) : JsonOp(path, op) {
"""
);
}
Expand All @@ -719,32 +757,32 @@ void testDiscriminatorWithUseOneOfInterfaces() {

var codegen = new KotlinMicronautClientCodegen();
codegen.setUseOneOfInterfaces(true);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discirminator2.yml", CodegenConstants.MODELS);
String outputPath = generateFiles(codegen, "src/test/resources/3_0/discriminator2.yml", CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/JsonOp.kt",
"""
interface JsonOp {
val op: String
val op: String?
}
"""
);

assertFileContains(path + "model/OpAdd.kt",
"""
data class OpAdd(
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_VALUE)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var `value`: String? = null,
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_PATH)
var path: String,
@field:NotNull
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_OP)
override var op: String,
): JsonOp {
override var op: String? = null,
"""
);
}
Expand Down
Loading

0 comments on commit d60192d

Please sign in to comment.