From ded765bcb9856cb68e8f30c9c7f0f1e5e462584c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Mart=C3=ADnez?= Date: Mon, 15 Oct 2018 07:13:05 +0200 Subject: [PATCH] [C#] Fixed issue #1126 with invalid generation of C# children models in allOf clauses (#1127) * Run ./bin/utils/ensure-up-to-date to re-generate samples run in the CI. * Fixed issue #1126. DefaultCodegen now sets the hasChildren property of CodegenModel when children models are added to the model. Changed the modelGeneric.mustache template to decide whether to include a base validation model (for children to inherit) based on the hasChildren property, and not the discriminator property. * Run the ./bin/utils/ensure-up-to-date script after fixing the issue #1126 * Reverted modification in go samples, performed by ./bin/utils/ensure-up-to-date, that are failing in CI. This partially reverts commit 2168df0f82d987ff8d50963d34efb269b015ef98. --- .../java/org/openapitools/codegen/DefaultCodegen.java | 1 + .../src/main/resources/csharp/modelGeneric.mustache | 10 +++++----- .../OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs | 2 +- .../OpenAPIClient/src/Org.OpenAPITools/Model/Dog.cs | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index bbeb2e8f87f..e8312cb6780 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -239,6 +239,7 @@ public class DefaultCodegen implements CodegenConfig { parent.setChildren(new ArrayList()); } parent.getChildren().add(cm); + parent.hasChildren = true; if (parent.getDiscriminator() == null) { parent = allModels.get(parent.getParent()); } else { diff --git a/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache index 89d35b73ec0..c547d137e18 100644 --- a/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache @@ -217,7 +217,7 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}; {{/generatePropertyChanged}} {{#validatable}} -{{#discriminator}} +{{#hasChildren}} /// /// To validate all properties of the instance /// @@ -235,8 +235,8 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}; /// Validation Result protected IEnumerable BaseValidate(ValidationContext validationContext) { -{{/discriminator}} -{{^discriminator}} +{{/hasChildren}} +{{^hasChildren}} /// /// To validate all properties of the instance /// @@ -244,11 +244,11 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}; /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { -{{/discriminator}} +{{/hasChildren}} {{#parent}} {{^isArrayModel}} {{^isMapModel}} - foreach(var x in BaseValidate(validationContext)) yield return x; + foreach(var x in base.BaseValidate(validationContext)) yield return x; {{/isMapModel}} {{/isArrayModel}} {{/parent}} diff --git a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs index b9444fb3607..1504638f6f2 100644 --- a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs +++ b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs @@ -123,7 +123,7 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - foreach(var x in BaseValidate(validationContext)) yield return x; + foreach(var x in base.BaseValidate(validationContext)) yield return x; yield break; } } diff --git a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Dog.cs b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Dog.cs index 90c181c11b6..ecd67f40b6e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Dog.cs +++ b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/Dog.cs @@ -123,7 +123,7 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - foreach(var x in BaseValidate(validationContext)) yield return x; + foreach(var x in base.BaseValidate(validationContext)) yield return x; yield break; } }