Skip to content

Commit

Permalink
Merge pull request #1799 from altro3/correct-process-novars
Browse files Browse the repository at this point in the history
Correct generating no vars classes
  • Loading branch information
altro3 authored Oct 10, 2024
2 parents 38891ef + f97b929 commit 03c82f6
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,18 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
if (model.isEnum) {
addImport(model, "Function");
}

model.hasVars = !requiredVarsWithoutDiscriminator.isEmpty() || !model.vars.isEmpty();
// just for tests
if (System.getProperty("micronaut.test.no-vars") != null) {
model.hasVars = false;
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList());
model.vendorExtensions.put("optionalVars", Collections.emptyList());
model.vendorExtensions.put("requiredVars", Collections.emptyList());
model.vendorExtensions.put("withRequiredVars", false);
model.vars = Collections.emptyList();
}

addStrValueToEnum(model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,18 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
processProperty(property, isServer, model, objs);
}
}

model.hasVars = !requiredVarsWithoutDiscriminator.isEmpty() || (withInheritance && !optionalVars.isEmpty());
// just for tests
if (System.getProperty("micronaut.test.no-vars") != null) {
model.hasVars = false;
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList());
model.vendorExtensions.put("optionalVars", Collections.emptyList());
model.vendorExtensions.put("requiredVars", Collections.emptyList());
model.vendorExtensions.put("withRequiredVars", false);
model.vars = Collections.emptyList();
}

addStrValueToEnum(model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@
@Serdeable
{{/micronaut_serde_jackson}}
{{#jackson}}
{{#vendorExtensions.withMultipleVars}}
{{#hasVars}}
{{#vendorExtensions.withMultipleVars}}
@JsonPropertyOrder({{openbrace}}
{{#vars}}
{{#vars}}
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
{{/vars}}
{{/vars}}
{{closebrace}})
{{/vendorExtensions.withMultipleVars}}
{{^vendorExtensions.withMultipleVars}}
{{/vendorExtensions.withMultipleVars}}
{{^vendorExtensions.withMultipleVars}}
@JsonPropertyOrder({{#vars}}{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{/vars}})
{{/vendorExtensions.withMultipleVars}}
{{/vendorExtensions.withMultipleVars}}
{{/hasVars}}
{{/jackson}}
{{#additionalModelTypeAnnotations}}
{{{.}}}
Expand Down Expand Up @@ -469,11 +471,16 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
@Override
public String toString() {
{{#hasVars}}
return "{{classname}}("
{{#allVars}}
+ "{{name}}: " + {{{getter}}}(){{^-last}} + ", "{{/-last}}
{{/allVars}}
+ ")";
{{/hasVars}}
{{^hasVars}}
return "{{classname}}()";
{{/hasVars}}
}
{{/lombok}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
@Serdeable
{{/micronaut_serde_jackson}}
{{#jackson}}
{{#hasVars}}
@JsonPropertyOrder(
{{#vars}}
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
{{/vars}}
)
{{/hasVars}}
{{/jackson}}
{{#additionalModelTypeAnnotations}}
{{{.}}}
Expand All @@ -43,7 +45,7 @@
{{/vendorExtensions.x-class-extra-annotation}}
{{/formatNoEmptyLines}}
{{!Declare the class with extends and implements}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#hasChildren}}open{{/hasChildren}}{{^hasChildren}}data{{/hasChildren}} class {{classname}}(
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#hasChildren}}open {{/hasChildren}}{{^hasChildren}}{{#hasVars}}data {{/hasVars}}{{/hasChildren}}class {{classname}}{{#hasVars}}({{/hasVars}}
{{#vendorExtensions.requiredVarsWithoutDiscriminator}}

{{#formatNoEmptyLines}}
Expand Down Expand Up @@ -92,7 +94,7 @@
{{/formatNoEmptyLines}}
{{/vendorExtensions.optionalVars}}
{{/vendorExtensions.withInheritance}}
){{#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}}

Expand All @@ -103,16 +105,17 @@
if (!super.equals(other)) return false
{{/parent}}
other as {{classname}}

{{#hasVars}}
{{#vars}}
if ({{{name}}} != other.{{{name}}}) return false
{{/vars}}

{{/hasVars}}
return true
}

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}}
Expand All @@ -123,11 +126,17 @@
{{/-first}}
{{/parent}}
{{/vars}}
{{/hasVars}}
return result
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,26 @@ void testCustomValidationMessages() {
"""
);
}

@Test
void testNoVars() {

System.setProperty("micronaut.test.no-vars", "true");

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

assertFileContains(path + "model/Book.java",
"""
@Serdeable
@Generated("io.micronaut.openapi.generator.JavaMicronautClientCodegen")
@Serializable
public class Book {
@Override
""");

System.clearProperty("micronaut.test.no-vars");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1280,4 +1280,30 @@ void testCustomValidationMessages() {
"""
);
}

@Test
void testNoVars() {

System.setProperty("micronaut.test.no-vars", "true");

var codegen = new KotlinMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/extra-annotations.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "model/Book.kt",
"""
@Serdeable
@Generated("io.micronaut.openapi.generator.KotlinMicronautClientCodegen")
@Serializable
class Book {
companion object {
}
}
""");

System.clearProperty("micronaut.test.no-vars");
}

}

0 comments on commit 03c82f6

Please sign in to comment.