Skip to content

Commit

Permalink
Fix generated method toString with inheritance.
Browse files Browse the repository at this point in the history
Add no args constructor for POJOs, when use jackson-databind
  • Loading branch information
altro3 committed Nov 9, 2023
1 parent 2298761 commit bbbbdd0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,9 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
var hasParent = model.getParentModel() != null;
var requiredVarsWithoutDiscriminator = new ArrayList<CodegenProperty>();
var requiredParentVarsWithoutDiscriminator = new ArrayList<CodegenProperty>();
var allVars = new ArrayList<CodegenProperty>();

processParentModel(model, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator);
processParentModel(model, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, allVars);

var optionalVars = new ArrayList<CodegenProperty>();
var requiredVars = new ArrayList<CodegenProperty>();
Expand All @@ -899,6 +900,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
if (!requiredVarsWithoutDiscriminator.isEmpty()) {
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", requiredVarsWithoutDiscriminator);
}
model.vendorExtensions.put("allVars", allVars);
model.vendorExtensions.put("requiredVars", requiredVars);
model.vendorExtensions.put("optionalVars", optionalVars);
model.vendorExtensions.put("areRequiredVarsAndReadOnlyVars", !requiredVarsWithoutDiscriminator.isEmpty() && !model.readOnlyVars.isEmpty());
Expand Down Expand Up @@ -942,10 +944,14 @@ public boolean isGenerateHardNullable() {
return false;
}

private void processParentModel(CodegenModel model, List<CodegenProperty> requiredVarsWithoutDiscriminator, List<CodegenProperty> requiredParentVarsWithoutDiscriminator) {
private void processParentModel(CodegenModel model, List<CodegenProperty> requiredVarsWithoutDiscriminator,
List<CodegenProperty> requiredParentVarsWithoutDiscriminator,
List<CodegenProperty> allVars) {
var parent = model.getParentModel();
var hasParent = parent != null;

allVars.addAll(model.vars);

for (var v : model.requiredVars) {
boolean isDiscriminator = isDiscriminator(v, model);
if (!isDiscriminator(v, model) && !containsProp(v, requiredVarsWithoutDiscriminator)) {
Expand All @@ -958,7 +964,7 @@ private void processParentModel(CodegenModel model, List<CodegenProperty> requir
model.parentVars = parent.allVars;
}
if (hasParent) {
processParentModel(parent, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator);
processParentModel(parent, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, allVars);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
{{/vars}}

{{#requiredPropertiesInConstructor}}
{{^micronaut_serde_jackson}}
public {{classname}}() {
}

{{/micronaut_serde_jackson}}
{{#formatNoEmptyLines}}
{{#vendorExtensions.withRequiredVars}}
public {{classname}}({{#vendorExtensions.requiredVarsWithoutDiscriminator}}{{#isReadOnly}}{{#vendorExtensions.isServer}}{{^-first}}, {{/-first}}{{{datatypeWithEnum}}} {{name}}{{/vendorExtensions.isServer}}{{/isReadOnly}}{{^isReadOnly}}{{^-first}}, {{/-first}}{{{datatypeWithEnum}}} {{name}}{{/isReadOnly}}{{/vendorExtensions.requiredVarsWithoutDiscriminator}}) {
Expand Down Expand Up @@ -382,9 +387,9 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
@Override
public String toString() {
return "{{classname}}("
{{#allVars}}
{{#vendorExtensions.allVars}}
+ "{{name}}: " + {{{getter}}}(){{^-last}} + ", "{{/-last}}
{{/allVars}}
{{/vendorExtensions.allVars}}
+ ")";
}
{{/lombok}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
}

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

Expand Down
38 changes: 19 additions & 19 deletions test-suite-java-server-generator/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ paths:
schema:
$ref: '#/components/schemas/SendPrimitivesResponse'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendValidatedPrimitives:
get:
operationId: sendValidatedPrimitives
Expand Down Expand Up @@ -86,7 +86,7 @@ paths:
schema:
type: string
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendDates:
get:
operationId: sendDates
Expand All @@ -111,7 +111,7 @@ paths:
schema:
$ref: '#/components/schemas/SendDatesResponse'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendParameterEnum:
get:
operationId: sendParameterEnum
Expand Down Expand Up @@ -234,7 +234,7 @@ paths:
schema:
$ref: '#/components/schemas/SimpleModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendListOfSimpleModels:
post:
operationId: sendListOfSimpleModels
Expand Down Expand Up @@ -276,7 +276,7 @@ paths:
schema:
$ref: '#/components/schemas/ModelWithRequiredProperties'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendDateModel:
post:
operationId: sendDateModel
Expand All @@ -295,7 +295,7 @@ paths:
schema:
$ref: '#/components/schemas/DateModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendEnum:
post:
operationId: sendEnum
Expand All @@ -315,7 +315,7 @@ paths:
schema:
$ref: '#/components/schemas/ColorEnum'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendEnumList:
post:
operationId: sendEnumList
Expand All @@ -339,7 +339,7 @@ paths:
items:
$ref: '#/components/schemas/ColorEnum'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendModelWithMapProperty:
post:
operationId: sendModelWithMapProperty
Expand Down Expand Up @@ -391,7 +391,7 @@ paths:
schema:
$ref: '#/components/schemas/NestedModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendModelWithInnerEnum:
post:
operationId: sendModelWithInnerEnum
Expand All @@ -411,7 +411,7 @@ paths:
schema:
$ref: '#/components/schemas/ModelWithInnerEnum'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendModelWithEnumList:
post:
operationId: sendModelWithEnumList
Expand All @@ -431,7 +431,7 @@ paths:
schema:
$ref: '#/components/schemas/ModelWithEnumList'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendModelWithDiscriminator:
put:
operationId: sendModelWithDiscriminator
Expand Down Expand Up @@ -474,7 +474,7 @@ paths:
type: string
format: byte
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/sendBytes:
put:
operationId: sendBytes
Expand Down Expand Up @@ -507,7 +507,7 @@ paths:
schema:
$ref: '#/components/schemas/SimpleModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getDateTime:
get:
operationId: getDateTime
Expand All @@ -522,7 +522,7 @@ paths:
type: string
format: date-time
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getDateModel:
get:
operationId: getDateModel
Expand All @@ -536,7 +536,7 @@ paths:
schema:
$ref: '#/components/schemas/DateModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getSimpleModelWithNonStandardStatus:
get:
operationId: getSimpleModelWithNonStandardStatus
Expand All @@ -550,7 +550,7 @@ paths:
schema:
$ref: '#/components/schemas/SimpleModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getModelWithValidatedList:
get:
operationId: getModelWithValidatedList
Expand All @@ -564,7 +564,7 @@ paths:
schema:
$ref: '#/components/schemas/ModelWithValidatedListProperty'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getPaginatedSimpleModel:
get:
operationId: getPaginatedSimpleModel
Expand Down Expand Up @@ -623,7 +623,7 @@ paths:
schema:
$ref: '#/components/schemas/SimpleModel'
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'
/getDatedSimpleModelWithNonMappedHeader:
get:
operationId: getDatedSimpleModelWithNonMappedHeader
Expand Down Expand Up @@ -669,7 +669,7 @@ paths:
type: string
format: binary
default:
$ref: '#/responses/Error'
$ref: '#/components/responses/Error'

components:
schemas:
Expand Down

0 comments on commit bbbbdd0

Please sign in to comment.