Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generated method toString with inheritance. #1298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@ -1014,8 +1014,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, false);
processParentModel(model, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, allVars, false);

var withInheritance = model.hasChildren || model.parent != null;
model.vendorExtensions.put("withInheritance", withInheritance);
Expand Down Expand Up @@ -1043,6 +1044,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
if (!requiredVarsWithoutDiscriminator.isEmpty()) {
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", requiredVarsWithoutDiscriminator);
}
model.allVars = allVars;
model.vendorExtensions.put("requiredVars", requiredVars);
model.vendorExtensions.put("withRequiredOrOptionalVars", !requiredVarsWithoutDiscriminator.isEmpty() || !optionalVars.isEmpty());
model.vendorExtensions.put("optionalVars", optionalVars);
Expand Down Expand Up @@ -1083,10 +1085,15 @@ private void processProperty(CodegenProperty property, boolean isServer, Codegen
property.required, property.isReadOnly, true);
}

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

allVars.addAll(model.vars);

if (!processParentModel) {
processVar(model, model.vars, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, processParentModel);
}
Expand All @@ -1098,7 +1105,7 @@ private void processParentModel(CodegenModel model, List<CodegenProperty> requir
model.parentVars = parent.allVars;
}
if (hasParent) {
processParentModel(parent, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, true);
processParentModel(parent, requiredVarsWithoutDiscriminator, requiredParentVarsWithoutDiscriminator, allVars, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import android.os.Parcel;
import {{javaxPackage}}.validation.constraints.*;
import {{javaxPackage}}.validation.Valid;
{{/useBeanValidation}}
{{^micronaut_serde_jackson}}
import io.micronaut.core.annotation.Introspected;
{{/micronaut_serde_jackson}}
{{^generateHardNullable}}
import io.micronaut.core.annotation.Nullable;
{{/generateHardNullable}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
{{/discriminator}}
{{>common/model/xmlAnnotation}}
{{#useBeanValidation}}
{{^micronaut_serde_jackson}}
@Introspected
{{/micronaut_serde_jackson}}
{{/useBeanValidation}}
{{#vendorExtensions.x-class-extra-annotation}}
{{{vendorExtensions.x-class-extra-annotation}}}
Expand Down Expand Up @@ -131,6 +133,11 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
{{/vars}}

{{#requiredPropertiesInConstructor}}
{{^micronaut_serde_jackson}}
private {{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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import android.os.Parcel
import {{javaxPackage}}.validation.constraints.*
import {{javaxPackage}}.validation.Valid
{{/useBeanValidation}}
{{^micronaut_serde_jackson}}
import io.micronaut.core.annotation.Introspected
{{/micronaut_serde_jackson}}
import io.micronaut.core.annotation.Nullable
{{#generatedAnnotation}}
import {{javaxPackage}}.annotation.Generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
{{/discriminator}}
{{>common/model/xmlAnnotation}}
{{#useBeanValidation}}
{{^micronaut_serde_jackson}}
@Introspected
{{/micronaut_serde_jackson}}
{{/useBeanValidation}}
{{#vendorExtensions.x-class-extra-annotation}}
{{{vendorExtensions.x-class-extra-annotation}}}
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
Loading
Loading