Skip to content

Commit 3ddda87

Browse files
committed
fixed discriminator
1 parent 561a707 commit 3ddda87

File tree

349 files changed

+18087
-383
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

349 files changed

+18087
-383
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ private boolean codegenPropertyIsNew(CodegenModel model, CodegenProperty propert
555555
? false
556556
: model.parentModel.allVars.stream().anyMatch(p ->
557557
p.name.equals(property.name) &&
558-
(p.dataType.equals(property.dataType) == false || p.datatypeWithEnum.equals(property.datatypeWithEnum) == false || p.isDiscriminator));
558+
(p.dataType.equals(property.dataType) == false || p.datatypeWithEnum.equals(property.datatypeWithEnum) == false));
559559
}
560560

561561
/**

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen {
6464
@Setter protected boolean netCoreProjectFileFlag = false;
6565
protected boolean nullReferenceTypesFlag = false;
6666
protected boolean useSourceGeneration = false;
67+
protected boolean patchIsInheritedProperty = false;
6768

6869
protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name();
6970

@@ -720,6 +721,7 @@ private void patchPropertyVendorExtensions(CodegenProperty property) {
720721
property.vendorExtensions.put("x-is-value-type", isValueType);
721722
property.vendorExtensions.put("x-is-reference-type", !isValueType);
722723
property.vendorExtensions.put("x-is-nullable-type", this.getNullableReferencesTypes() || isValueType);
724+
property.vendorExtensions.put("x-is-base-or-new-discriminator", (property.isDiscriminator && !property.isInherited) || (property.isDiscriminator && property.isNew));
723725
}
724726

725727
protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel model, CodegenProperty property) {
@@ -735,6 +737,10 @@ protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel mo
735737
property.isPrimitiveType = true;
736738
}
737739

740+
if (this.patchIsInheritedProperty && model.parentModel != null && model.parentModel.allVars.stream().anyMatch(v -> v.baseName.equals(property.baseName))) {
741+
property.isInherited = true;
742+
}
743+
738744
patchPropertyVendorExtensions(property);
739745

740746
property.name = patchPropertyName(model, property.name);

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,16 +1592,18 @@ public String toInstantiationType(Schema schema) {
15921592

15931593
@Override
15941594
protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel model, CodegenProperty property) {
1595+
if (GENERICHOST.equals(getLibrary())) {
1596+
// the isInherited property is not always correct
1597+
// fixing it here causes a breaking change in some generators
1598+
this.patchIsInheritedProperty = true;
1599+
}
1600+
15951601
super.patchProperty(enumRefs, model, property);
15961602

15971603
if (!GENERICHOST.equals(getLibrary())) {
15981604
if (!property.isContainer && (this.getNullableTypes().contains(property.dataType) || property.isEnum)) {
15991605
property.vendorExtensions.put("x-csharp-value-type", true);
16001606
}
1601-
} else {
1602-
if (model.parentModel != null && model.parentModel.allVars.stream().anyMatch(v -> v.baseName.equals(property.baseName))) {
1603-
property.isInherited = true;
1604-
}
16051607
}
16061608
}
16071609

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/JsonConverter.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
{{#isDiscriminator}}
440440
{{^model.composedSchemas.anyOf}}
441441
{{^model.composedSchemas.oneOf}}
442-
writer.WriteString("{{baseName}}", {{^isEnum}}{{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{/isEnum}}{{#isNew}}{{#isEnum}}{{#isInnerEnum}}{{classname}}.{{{datatypeWithEnum}}}ToJsonValue{{/isInnerEnum}}{{^isInnerEnum}}{{{datatypeWithEnum}}}ValueConverter.ToJsonValue{{/isInnerEnum}}({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}.Value{{/required}}){{/isEnum}}{{/isNew}});
442+
writer.WriteString("{{baseName}}", {{^isEnum}}{{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{/isEnum}}{{#isEnum}}{{#isInnerEnum}}{{classname}}.{{{datatypeWithEnum}}}ToJsonValue{{/isInnerEnum}}{{^isInnerEnum}}{{{datatypeWithEnum}}}ValueConverter.ToJsonValue{{/isInnerEnum}}({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}.Value{{/required}}){{/isEnum}});
443443

444444
{{/model.composedSchemas.oneOf}}
445445
{{/model.composedSchemas.anyOf}}

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/modelGeneric.mustache

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
{{/isNew}}
3535
{{/isInherited}}
3636
{{/isDiscriminator}}
37+
{{#vendorExtensions.x-is-base-or-new-discriminator}}
38+
{{^model.composedSchemas.anyOf}}
39+
{{^model.composedSchemas.oneOf}}
40+
{{name}} = {{^isEnum}}this.GetType().Name{{/isEnum}}{{#isEnum}}({{datatypeWithEnum}})Enum.Parse(typeof({{datatypeWithEnum}}), this.GetType().Name){{/isEnum}};
41+
{{/model.composedSchemas.oneOf}}
42+
{{/model.composedSchemas.anyOf}}
43+
{{/vendorExtensions.x-is-base-or-new-discriminator}}
3744
{{/allVars}}
3845
OnCreated();
3946
}
@@ -71,6 +78,13 @@
7178
{{/isNew}}
7279
{{/isInherited}}
7380
{{/isDiscriminator}}
81+
{{#vendorExtensions.x-is-base-or-new-discriminator}}
82+
{{^model.composedSchemas.anyOf}}
83+
{{^model.composedSchemas.oneOf}}
84+
{{name}} = {{^isEnum}}this.GetType().Name{{/isEnum}}{{#isEnum}}({{datatypeWithEnum}})Enum.Parse(typeof({{datatypeWithEnum}}), this.GetType().Name){{/isEnum}};
85+
{{/model.composedSchemas.oneOf}}
86+
{{/model.composedSchemas.anyOf}}
87+
{{/vendorExtensions.x-is-base-or-new-discriminator}}
7488
{{/allVars}}
7589
OnCreated();
7690
}
@@ -162,20 +176,20 @@
162176
{{/vendorExtensions.x-duplicated-data-type}}
163177
{{/composedSchemas.oneOf}}
164178
{{#allVars}}
165-
{{#isDiscriminator}}
179+
{{#vendorExtensions.x-is-base-or-new-discriminator}}
166180
{{^model.composedSchemas.anyOf}}
167181
{{^model.composedSchemas.oneOf}}
168182
/// <summary>
169183
/// The discriminator
170184
/// </summary>
171185
[JsonIgnore]
172186
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
173-
public {{#isNew}}new {{/isNew}}{{datatypeWithEnum}} {{name}} { get; } = {{^isNew}}"{{classname}}"{{/isNew}}{{#isNew}}{{^isEnum}}"{{classname}}"{{/isEnum}}{{#isEnum}}({{datatypeWithEnum}})Enum.Parse(typeof({{datatypeWithEnum}}), "{{classname}}"){{/isEnum}}{{/isNew}};
187+
public {{#isNew}}new {{/isNew}}{{datatypeWithEnum}} {{name}} { get; }
174188

175189
{{/model.composedSchemas.oneOf}}
176190
{{/model.composedSchemas.anyOf}}
177-
{{/isDiscriminator}}
178-
{{^isDiscriminator}}
191+
{{/vendorExtensions.x-is-base-or-new-discriminator}}
192+
{{^vendorExtensions.x-is-base-or-new-discriminator}}
179193
{{^isEnum}}
180194
{{#isInherited}}
181195
{{#isNew}}
@@ -228,7 +242,7 @@
228242

229243
{{/isInherited}}
230244
{{/isEnum}}
231-
{{/isDiscriminator}}
245+
{{/vendorExtensions.x-is-base-or-new-discriminator}}
232246
{{/allVars}}
233247
{{#isAdditionalPropertiesTrue}}
234248
{{^parentModel}}

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,3 +2938,37 @@ components:
29382938
uuid:
29392939
type: string
29402940
format: uuid
2941+
Descendant1:
2942+
allOf:
2943+
- $ref: "#/components/schemas/TestDescendants"
2944+
- required:
2945+
- "descendantName"
2946+
type: "object"
2947+
properties:
2948+
descendantName:
2949+
type: "string"
2950+
Descendant2:
2951+
allOf:
2952+
- $ref: "#/components/schemas/TestDescendants"
2953+
- required:
2954+
- "confidentiality"
2955+
type: "object"
2956+
properties:
2957+
confidentiality:
2958+
type: "string"
2959+
TestDescendants:
2960+
required:
2961+
- "alternativeName"
2962+
- "objectType"
2963+
type: "object"
2964+
properties:
2965+
alternativeName:
2966+
type: "string"
2967+
objectType:
2968+
type: "string"
2969+
enum:
2970+
- "Descendant1"
2971+
- "Descendant2"
2972+
description: ""
2973+
discriminator:
2974+
propertyName: "objectType"

samples/client/petstore/csharp/generichost/net4.7/AllOf/src/Org.OpenAPITools/Model/Adult.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ public Adult(Option<List<Child>> children = default, Option<string> firstName =
5757
[JsonPropertyName("children")]
5858
public List<Child> Children { get { return this.ChildrenOption; } set { this.ChildrenOption = new Option<List<Child>>(value); } }
5959

60-
/// <summary>
61-
/// The discriminator
62-
/// </summary>
63-
[JsonIgnore]
64-
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
65-
public new string Type { get; } = "Adult";
66-
6760
/// <summary>
6861
/// Returns the string presentation of the object
6962
/// </summary>

samples/client/petstore/csharp/generichost/net4.7/AllOf/src/Org.OpenAPITools/Model/Child.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ public Child(Option<int?> age = default, Option<string> firstName = default, Opt
5959
[JsonPropertyName("age")]
6060
public int? Age { get { return this.AgeOption; } set { this.AgeOption = new Option<int?>(value); } }
6161

62-
/// <summary>
63-
/// The discriminator
64-
/// </summary>
65-
[JsonIgnore]
66-
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
67-
public new string Type { get; } = "Child";
68-
6962
/// <summary>
7063
/// Used to track the state of BoosterSeat
7164
/// </summary>

samples/client/petstore/csharp/generichost/net4.7/AllOf/src/Org.OpenAPITools/Model/Person.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Person(Option<string> firstName = default, Option<string> lastName = defa
3939
{
4040
FirstNameOption = firstName;
4141
LastNameOption = lastName;
42+
Type = this.GetType().Name;
4243
OnCreated();
4344
}
4445

@@ -75,7 +76,7 @@ public Person(Option<string> firstName = default, Option<string> lastName = defa
7576
/// </summary>
7677
[JsonIgnore]
7778
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
78-
public string Type { get; } = "Person";
79+
public string Type { get; }
7980

8081
/// <summary>
8182
/// Gets or Sets additional properties

samples/client/petstore/csharp/generichost/net4.7/FormModels/.openapi-generator/FILES

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ docs/models/ComplexQuadrilateral.md
3333
docs/models/DanishPig.md
3434
docs/models/DateOnlyClass.md
3535
docs/models/DeprecatedObject.md
36+
docs/models/Descendant1.md
37+
docs/models/Descendant2.md
3638
docs/models/Dog.md
3739
docs/models/Drawing.md
3840
docs/models/EnumArrays.md
@@ -114,6 +116,8 @@ docs/models/SpecialModelName.md
114116
docs/models/Tag.md
115117
docs/models/TestCollectionEndingWithWordList.md
116118
docs/models/TestCollectionEndingWithWordListObject.md
119+
docs/models/TestDescendants.md
120+
docs/models/TestDescendantsObjectType.md
117121
docs/models/TestEnumParametersEnumHeaderStringParameter.md
118122
docs/models/TestEnumParametersEnumQueryDoubleParameter.md
119123
docs/models/TestEnumParametersEnumQueryIntegerParameter.md
@@ -190,6 +194,8 @@ src/Org.OpenAPITools/Model/ComplexQuadrilateral.cs
190194
src/Org.OpenAPITools/Model/DanishPig.cs
191195
src/Org.OpenAPITools/Model/DateOnlyClass.cs
192196
src/Org.OpenAPITools/Model/DeprecatedObject.cs
197+
src/Org.OpenAPITools/Model/Descendant1.cs
198+
src/Org.OpenAPITools/Model/Descendant2.cs
193199
src/Org.OpenAPITools/Model/Dog.cs
194200
src/Org.OpenAPITools/Model/Drawing.cs
195201
src/Org.OpenAPITools/Model/EnumArrays.cs
@@ -271,6 +277,8 @@ src/Org.OpenAPITools/Model/SpecialModelName.cs
271277
src/Org.OpenAPITools/Model/Tag.cs
272278
src/Org.OpenAPITools/Model/TestCollectionEndingWithWordList.cs
273279
src/Org.OpenAPITools/Model/TestCollectionEndingWithWordListObject.cs
280+
src/Org.OpenAPITools/Model/TestDescendants.cs
281+
src/Org.OpenAPITools/Model/TestDescendantsObjectType.cs
274282
src/Org.OpenAPITools/Model/TestEnumParametersEnumHeaderStringParameter.cs
275283
src/Org.OpenAPITools/Model/TestEnumParametersEnumQueryDoubleParameter.cs
276284
src/Org.OpenAPITools/Model/TestEnumParametersEnumQueryIntegerParameter.cs

0 commit comments

Comments
 (0)