Skip to content

Commit

Permalink
Fixed generating code without properties, but with inheritance.
Browse files Browse the repository at this point in the history
Minor fixes in templates.

Fixed micronaut-projects#1869
  • Loading branch information
altro3 committed Nov 15, 2024
1 parent 0a14aa0 commit b025add
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
}
}

model.vendorExtensions.put("hasOwnVars", !model.vars.isEmpty());
model.vendorExtensions.put("withMultipleVars", model.vars.size() > 1);
if (!requiredParentVarsWithoutDiscriminator.isEmpty()) {
model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", requiredParentVarsWithoutDiscriminator);
Expand Down Expand Up @@ -1929,6 +1930,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
// just for tests
if (System.getProperty("micronaut.test.no-vars") != null) {
model.hasVars = false;
model.vendorExtensions.put("hasOwnVars", false);
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList());
model.vendorExtensions.put("optionalVars", Collections.emptyList());
model.vendorExtensions.put("requiredVars", Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
}
}

model.vendorExtensions.put("hasOwnVars", !model.vars.isEmpty());
model.vendorExtensions.put("withMultipleVars", model.vars.size() > 1);
if (!requiredParentVarsWithoutDiscriminator.isEmpty()) {
model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", requiredParentVarsWithoutDiscriminator);
Expand Down Expand Up @@ -1857,6 +1858,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
// just for tests
if (System.getProperty("micronaut.test.no-vars") != null) {
model.hasVars = false;
model.vendorExtensions.put("hasOwnVars", false);
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList());
model.vendorExtensions.put("optionalVars", Collections.emptyList());
model.vendorExtensions.put("requiredVars", Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
// sort operations by default
protected boolean skipSortingOperations = false;

protected final static Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application/(.*)[+]?xml(;.*)?");
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?");
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
protected static final Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application/(.*)[+]?xml(;.*)?");
protected static final Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?");
protected static final Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{/enumVars}}
{{/allowableValues}}

{{#formatSingleLine}}public final static Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}}
{{#formatSingleLine}}public static final Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}}
.collect(Collectors.toMap(v -> v.value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}}, Function.identity())));

private final {{{vendorExtensions.baseType}}} value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@Serdeable
{{/micronaut_serde_jackson}}
{{#jackson}}
{{#hasVars}}
{{#vendorExtensions.hasOwnVars}}
{{#vendorExtensions.withMultipleVars}}
@JsonPropertyOrder({{openbrace}}
{{#vars}}
Expand All @@ -56,7 +56,7 @@
{{^vendorExtensions.withMultipleVars}}
@JsonPropertyOrder({{#vars}}{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{/vars}})
{{/vendorExtensions.withMultipleVars}}
{{/hasVars}}
{{/vendorExtensions.hasOwnVars}}
{{/jackson}}
{{#additionalModelTypeAnnotations}}
{{{.}}}
Expand Down Expand Up @@ -447,15 +447,15 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
if (o == null || getClass() != o.getClass()) {
return false;
}
{{#hasVars}}
{{classname}} {{classVarName}} = ({{classname}}) o;
return {{#vars}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals({{name}}, {{classVarName}}.{{name}}){{^-last}} &&
{{/-last}}{{/vars}}{{#parent}} &&
super.equals(o){{/parent}};
{{/hasVars}}
{{^hasVars}}
{{#vendorExtensions.hasOwnVars}}
var {{classVarName}} = ({{classname}}) o;
return {{#vars}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals({{name}}, {{classVarName}}.{{name}}){{^-last}}
&& {{/-last}}{{/vars}}{{#parent}}
&& super.equals(o){{/parent}};
{{/vendorExtensions.hasOwnVars}}
{{^vendorExtensions.hasOwnVars}}
return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}};
{{/hasVars}}
{{/vendorExtensions.hasOwnVars}}
{{/useReflectionEqualsHashCode}}
}
Expand All @@ -465,7 +465,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
return HashCodeBuilder.reflectionHashCode(this);
{{/useReflectionEqualsHashCode}}
{{^useReflectionEqualsHashCode}}
return Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
return Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#vendorExtensions.hasOwnVars}}, {{/vendorExtensions.hasOwnVars}}super.hashCode(){{/parent}});
{{/useReflectionEqualsHashCode}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{{{.}}}
{{/additionalEnumTypeAnnotations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#formatSingleLine}}enum class {{>common/model/enumName}}{{/formatSingleLine}}(
@get:JsonValue val value: {{{dataType}}}
@get:JsonValue val value: {{{dataType}}},
) {
{{#allowableValues}}
Expand All @@ -41,13 +41,12 @@
@Deprecated("")
{{/deprecated}}
@JsonProperty({{{strValue}}})
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{{name}}}({{{value}}}),{{#-last}}
;{{/-last}}
{{/enumVars}}
{{/allowableValues}}

override fun toString(): String {
return {{#isString}}value{{/isString}}{{^isString}}"$value"{{/isString}}
}
override fun toString(): String = {{#isString}}value{{/isString}}{{^isString}}"$value"{{/isString}}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {{javaxPackage}}.annotation.Generated
{{#generateSwagger2Annotations}}
import io.swagger.v3.oas.annotations.media.Schema
{{/generateSwagger2Annotations}}
import java.util.*
{{/formatNoEmptyLines}}
{{#formatOneEmptyLine}}
{{#models}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
{{>common/model/typeInfoAnnotation}}
interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
{{/formatNoEmptyLines}}
interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}}{{/formatNoEmptyLines}}{{#discriminator}} {
{{#discriminator}}
val {{propertyName}}: {{propertyType}}
{{/discriminator}}
}
{{/discriminator}}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
@Serdeable
{{/micronaut_serde_jackson}}
{{#jackson}}
{{#hasVars}}
{{#vendorExtensions.hasOwnVars}}
@JsonPropertyOrder(
{{#vars}}
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}},
{{/vars}}
)
{{/hasVars}}
{{/vendorExtensions.hasOwnVars}}
{{/jackson}}
{{#additionalModelTypeAnnotations}}
{{{.}}}
Expand Down Expand Up @@ -94,50 +94,38 @@
{{/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}}
{{#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}}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
{{#parent}}
if (!super.equals(other)) return false
{{/parent}}
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}
other as {{classname}}
{{#hasVars}}
{{#vars}}
if ({{{name}}} != other.{{{name}}}) return false
{{/vars}}
{{/hasVars}}
return true
{{#vendorExtensions.hasOwnVars}}
return {{#vars}}{{^-first}}
&& {{/-first}}{{{name}}} == other.{{{name}}}{{/vars}}{{#parent}}
&& super.equals(other){{/parent}}
{{/vendorExtensions.hasOwnVars}}
{{^vendorExtensions.hasOwnVars}}
return {{#parent}}super.equals(other){{/parent}}{{^parent}}true{{/parent}}
{{/vendorExtensions.hasOwnVars}}
}

override fun hashCode(): Int {
var result = {{#parent}}super.hashCode(){{/parent}}{{^parent}}{{#vars.0}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0{{/isNullable}}{{/vars.0}}{{/parent}}
{{#hasVars}}
{{#vars}}
{{#parent}}
result = 31 * result + {{#isNullable}}({{/isNullable}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0){{/isNullable}}
{{/parent}}
{{^parent}}
{{^-first}}
result = {{#-first}}{{#parent}}31 * result + {{/parent}}{{/-first}}{{^-first}}31 * result + {{/-first}}{{#isNullable}}({{/isNullable}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0){{/isNullable}}
{{/-first}}
{{/parent}}
{{/vars}}
{{/hasVars}}
return result
}
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}})

override fun toString(): String {
override fun toString(): String =
{{#hasVars}}
return "{{classname}}({{#allVars}}{{{vendorExtensions.realName}}}='${{{name}}}'{{^-last}}, {{/-last}}{{/allVars}})"
"{{classname}}({{#allVars}}{{{vendorExtensions.realName}}}='${{{name}}}'{{^-last}}, {{/-last}}{{/allVars}})"
{{/hasVars}}
{{^hasVars}}
return "{{classname}}()"
"{{classname}}()"
{{/hasVars}}
}
{{/vendorExtensions.withInheritance}}

{{#vars}}
Expand All @@ -158,6 +146,7 @@

{{/vars}}

{{#vendorExtensions.hasOwnVars}}
companion object {
{{#serializableModel}}

Expand All @@ -170,4 +159,15 @@
const val JSON_PROPERTY_{{nameInSnakeCase}} = "{{baseName}}"
{{/vars}}
}
{{/vendorExtensions.hasOwnVars}}
{{^vendorExtensions.hasOwnVars}}
{{#serializableModel}}
companion object {
@Serial
@JvmStatic
private val serialVersionUID = {{{vendorExtensions.serialId}}}L
}
{{/serializableModel}}
{{/vendorExtensions.hasOwnVars}}
{{closebrace}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{{#discriminator.propertyBaseName}}
{{#jackson}}
@JsonIgnoreProperties(
value = ["{{{discriminator.propertyBaseName}}}"], // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
value = ["{{{discriminator.propertyBaseName}}}"], // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{#vendorExtensions.hasMappedModels}}
@JsonSubTypes({{#mappedModels}}
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}}
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}}"),
{{/mappedModels}})
{{/vendorExtensions.hasMappedModels}}
{{/jackson}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void testEnumsWithNonStringTypeValue() {

assertFileContains(modelPath + "StringEnum.java", "@JsonProperty(\"starting\")", "STARTING(\"starting\"),",
"""
public final static Map<String, StringEnum> VALUE_MAPPING = Map.copyOf(Arrays.stream(values())
public static final Map<String, StringEnum> VALUE_MAPPING = Map.copyOf(Arrays.stream(values())
.collect(Collectors.toMap(v -> v.value, Function.identity())));
""",
"""
Expand Down Expand Up @@ -804,7 +804,7 @@ void testUseEnumCaseInsensitive() {

assertFileContains(path + "model/StringEnum.java",
"""
public final static Map<String, StringEnum> VALUE_MAPPING = Map.copyOf(Arrays.stream(values())
public static final Map<String, StringEnum> VALUE_MAPPING = Map.copyOf(Arrays.stream(values())
.collect(Collectors.toMap(v -> v.value.toLowerCase(), Function.identity())));
""",
"""
Expand Down Expand Up @@ -1363,4 +1363,32 @@ void testSwaggerAnnotations() {
);
""");
}

@Test
void testEquals() {

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

assertFileNotContains(path + "model/SalesInvoiceCreateDto.java", "@JsonPropertyOrder");
assertFileContains(path + "model/SalesInvoiceCreateDto.java", """
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode());
}
""");
}
}
Loading

0 comments on commit b025add

Please sign in to comment.