Skip to content

Commit

Permalink
Add default private constructor only when we have required args const…
Browse files Browse the repository at this point in the history
…ructor (#1343)
  • Loading branch information
altro3 authored Dec 7, 2023
1 parent 797f59e commit 665ad2d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE

{{#requiredPropertiesInConstructor}}
{{^micronaut_serde_jackson}}
{{#vendorExtensions.withRequiredVars}}
private {{classname}}() {
}

{{/vendorExtensions.withRequiredVars}}
{{/micronaut_serde_jackson}}
{{#formatNoEmptyLines}}
{{#vendorExtensions.withRequiredVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ void doGenerateForTestSpock() {
void doGenerateRequiredPropertiesInConstructor() {
var codegen = new JavaMicronautServerCodegen();
codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR, "true");
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, SerializationLibraryKind.JACKSON.name());
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.MODELS, CodegenConstants.APIS);

// Constructor should have properties
String modelPath = outputPath + "src/main/java/org/openapitools/model/";
assertFileContains(modelPath + "Pet.java", "public Pet(String name, List<String> photoUrls)");
assertFileNotContains(modelPath + "Pet.java", "public Pet()");
assertFileContains(modelPath + "Pet.java", "private Pet()");
}

@Test
Expand All @@ -155,8 +156,11 @@ void doNotGenerateRequiredPropertiesInConstructor() {
// Constructor should have properties
String modelPath = outputPath + "src/main/java/org/openapitools/model/";
assertFileNotContainsRegex(modelPath + "Pet.java", "public Pet\\([^)]+\\)");
assertFileNotContains(modelPath + "Pet.java", "private Pet()");
assertFileNotContainsRegex(modelPath + "User.java", "public User\\([^)]+\\)");
assertFileNotContains(modelPath + "User.java", "private User()");
assertFileNotContainsRegex(modelPath + "Order.java", "public Order\\([^)]+\\)");
assertFileNotContains(modelPath + "Order.java", "private Order()");
}

@Test
Expand Down

0 comments on commit 665ad2d

Please sign in to comment.