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

[C#] Fixed valuetype parameters and discriminator deserialization #5680

Merged
merged 9 commits into from
Apr 29, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,32 @@ private void postProcessEnumRefs(final Map<String, Object> models) {
var.isPrimitiveType = true;
}
}
for (CodegenProperty var : model.readWriteVars) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this only flagged things in the allVars array which are not actually references to the objecsts in the readWrite or readOnly arrays, so it was missing out on this decoration

if (enumRefs.containsKey(var.dataType)) {
// Handle any enum properties referred to by $ref.
// This is different in C# than most other generators, because enums in C# are compiled to integral types,
// while enums in many other languages are true objects.
CodegenModel refModel = enumRefs.get(var.dataType);
var.allowableValues = refModel.allowableValues;
var.isEnum = true;

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;
}
}
for (CodegenProperty var : model.readOnlyVars) {
if (enumRefs.containsKey(var.dataType)) {
// Handle any enum properties referred to by $ref.
// This is different in C# than most other generators, because enums in C# are compiled to integral types,
// while enums in many other languages are true objects.
CodegenModel refModel = enumRefs.get(var.dataType);
var.allowableValues = refModel.allowableValues;
var.isEnum = true;

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;
}
}

// We're looping all models here.
if (model.isEnum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,25 @@ public void postProcessParameter(CodegenParameter parameter) {
postProcessPattern(parameter.pattern, parameter.vendorExtensions);
postProcessEmitDefaultValue(parameter.vendorExtensions);
super.postProcessParameter(parameter);

if (nullableType.contains(parameter.dataType)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be noted that this code is just copied from the csharp-netcore generator and works properly there

if (!parameter.required) { //optional
parameter.dataType = parameter.dataType + "?";
} else {
parameter.vendorExtensions.put("x-csharp-value-type", true);
}
}
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
postProcessPattern(property.pattern, property.vendorExtensions);
postProcessEmitDefaultValue(property.vendorExtensions);
super.postProcessModelProperty(model, property);

if (!property.isContainer && (nullableType.contains(property.dataType) || property.isEnum)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be noted that this code is just copied from the csharp-netcore generator and works properly there

property.vendorExtensions.put("x-csharp-value-type", true);
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// </summary>
[DataContract]
{{#discriminator}}
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]{{#children}}
[JsonConverter(typeof(JsonSubtypes), "{{#lambda.camelcase_param}}{{{discriminatorName}}}{{/lambda.camelcase_param}}")]{{#children}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now if this discriminator isn't camelCased it causes things to not deserialize properly when receiving them from an API (since the json is expected to come back camelcased)

[JsonSubtypes.KnownSubType(typeof({{classname}}), "{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}")]{{/children}}
{{/discriminator}}
{{#generatePropertyChanged}}
Expand Down Expand Up @@ -31,7 +31,7 @@
{{#description}}
/// <value>{{description}}</value>
{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}{{#required}}true{{/required}}{{^required}}false{{/required}}{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
{{/isEnum}}
{{/vars}}
Expand Down Expand Up @@ -108,7 +108,7 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
/// </summary>{{#description}}
/// <value>{{description}}</value>{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]{{#isDate}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}{{#required}}true{{/required}}{{^required}}false{{/required}}{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]{{#isDate}}
[JsonConverter(typeof(OpenAPIDateConverter))]{{/isDate}}
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
{{/isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected EnumTest() { }
/// <param name="enumInteger">enumInteger.</param>
/// <param name="enumNumber">enumNumber.</param>
/// <param name="outerEnum">outerEnum.</param>
public EnumTest(EnumStringEnum? enumString = default(EnumStringEnum?), EnumStringRequiredEnum enumStringRequired = default(EnumStringRequiredEnum), EnumIntegerEnum? enumInteger = default(EnumIntegerEnum?), EnumNumberEnum? enumNumber = default(EnumNumberEnum?), OuterEnum outerEnum = default(OuterEnum))
public EnumTest(EnumStringEnum? enumString = default(EnumStringEnum?), EnumStringRequiredEnum enumStringRequired = default(EnumStringRequiredEnum), EnumIntegerEnum? enumInteger = default(EnumIntegerEnum?), EnumNumberEnum? enumNumber = default(EnumNumberEnum?), OuterEnum? outerEnum = default(OuterEnum?))
{
this.EnumStringRequired = enumStringRequired;
this.EnumString = enumString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected EnumTest() { }
/// <param name="enumInteger">enumInteger.</param>
/// <param name="enumNumber">enumNumber.</param>
/// <param name="outerEnum">outerEnum.</param>
public EnumTest(EnumStringEnum? enumString = default(EnumStringEnum?), EnumStringRequiredEnum enumStringRequired = default(EnumStringRequiredEnum), EnumIntegerEnum? enumInteger = default(EnumIntegerEnum?), EnumNumberEnum? enumNumber = default(EnumNumberEnum?), OuterEnum outerEnum = default(OuterEnum))
public EnumTest(EnumStringEnum? enumString = default(EnumStringEnum?), EnumStringRequiredEnum enumStringRequired = default(EnumStringRequiredEnum), EnumIntegerEnum? enumInteger = default(EnumIntegerEnum?), EnumNumberEnum? enumNumber = default(EnumNumberEnum?), OuterEnum? outerEnum = default(OuterEnum?))
{
this.EnumStringRequired = enumStringRequired;
this.EnumString = enumString;
Expand Down
62 changes: 31 additions & 31 deletions samples/client/petstore/csharp/OpenAPIClient/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ No authorization required

## FakeOuterBooleanSerialize

> bool FakeOuterBooleanSerialize (bool body = null)
> bool FakeOuterBooleanSerialize (bool? body = null)



Expand All @@ -120,7 +120,7 @@ namespace Example
{
Configuration.Default.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(Configuration.Default);
var body = true; // bool | Input boolean as post body (optional)
var body = true; // bool? | Input boolean as post body (optional)

try
{
Expand All @@ -143,7 +143,7 @@ namespace Example

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | **bool**| Input boolean as post body | [optional]
**body** | **bool?**| Input boolean as post body | [optional]

### Return type

Expand Down Expand Up @@ -245,7 +245,7 @@ No authorization required

## FakeOuterNumberSerialize

> decimal FakeOuterNumberSerialize (decimal body = null)
> decimal FakeOuterNumberSerialize (decimal? body = null)



Expand All @@ -268,7 +268,7 @@ namespace Example
{
Configuration.Default.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(Configuration.Default);
var body = 8.14; // decimal | Input number as post body (optional)
var body = 8.14; // decimal? | Input number as post body (optional)

try
{
Expand All @@ -291,7 +291,7 @@ namespace Example

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | **decimal**| Input number as post body | [optional]
**body** | **decimal?**| Input number as post body | [optional]

### Return type

Expand Down Expand Up @@ -614,7 +614,7 @@ No authorization required

## TestEndpointParameters

> void TestEndpointParameters (decimal number, double _double, string patternWithoutDelimiter, byte[] _byte, int integer = null, int int32 = null, long int64 = null, float _float = null, string _string = null, System.IO.Stream binary = null, DateTime date = null, DateTime dateTime = null, string password = null, string callback = null)
> void TestEndpointParameters (decimal number, double _double, string patternWithoutDelimiter, byte[] _byte, int? integer = null, int? int32 = null, long? int64 = null, float? _float = null, string _string = null, System.IO.Stream binary = null, DateTime? date = null, DateTime? dateTime = null, string password = null, string callback = null)

Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

Expand Down Expand Up @@ -645,14 +645,14 @@ namespace Example
var _double = 1.2D; // double | None
var patternWithoutDelimiter = patternWithoutDelimiter_example; // string | None
var _byte = BYTE_ARRAY_DATA_HERE; // byte[] | None
var integer = 56; // int | None (optional)
var int32 = 56; // int | None (optional)
var int64 = 789; // long | None (optional)
var _float = 3.4F; // float | None (optional)
var integer = 56; // int? | None (optional)
var int32 = 56; // int? | None (optional)
var int64 = 789; // long? | None (optional)
var _float = 3.4F; // float? | None (optional)
wing328 marked this conversation as resolved.
Show resolved Hide resolved
var _string = _string_example; // string | None (optional)
var binary = BINARY_DATA_HERE; // System.IO.Stream | None (optional)
var date = 2013-10-20; // DateTime | None (optional)
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime | None (optional)
var date = 2013-10-20; // DateTime? | None (optional)
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime? | None (optional)
var password = password_example; // string | None (optional)
var callback = callback_example; // string | None (optional)

Expand Down Expand Up @@ -681,14 +681,14 @@ Name | Type | Description | Notes
**_double** | **double**| None |
**patternWithoutDelimiter** | **string**| None |
**_byte** | **byte[]**| None |
**integer** | **int**| None | [optional]
**int32** | **int**| None | [optional]
**int64** | **long**| None | [optional]
**_float** | **float**| None | [optional]
**integer** | **int?**| None | [optional]
**int32** | **int?**| None | [optional]
**int64** | **long?**| None | [optional]
**_float** | **float?**| None | [optional]
**_string** | **string**| None | [optional]
**binary** | **System.IO.Stream**| None | [optional]
**date** | **DateTime**| None | [optional]
**dateTime** | **DateTime**| None | [optional]
**date** | **DateTime?**| None | [optional]
**dateTime** | **DateTime?**| None | [optional]
**password** | **string**| None | [optional]
**callback** | **string**| None | [optional]

Expand Down Expand Up @@ -719,7 +719,7 @@ void (empty response body)

## TestEnumParameters

> void TestEnumParameters (List<string> enumHeaderStringArray = null, string enumHeaderString = null, List<string> enumQueryStringArray = null, string enumQueryString = null, int enumQueryInteger = null, double enumQueryDouble = null, List<string> enumFormStringArray = null, string enumFormString = null)
> void TestEnumParameters (List<string> enumHeaderStringArray = null, string enumHeaderString = null, List<string> enumQueryStringArray = null, string enumQueryString = null, int? enumQueryInteger = null, double? enumQueryDouble = null, List<string> enumFormStringArray = null, string enumFormString = null)

To test enum parameters

Expand All @@ -746,8 +746,8 @@ namespace Example
var enumHeaderString = enumHeaderString_example; // string | Header parameter enum test (string) (optional) (default to -efg)
var enumQueryStringArray = enumQueryStringArray_example; // List<string> | Query parameter enum test (string array) (optional)
var enumQueryString = enumQueryString_example; // string | Query parameter enum test (string) (optional) (default to -efg)
var enumQueryInteger = 56; // int | Query parameter enum test (double) (optional)
var enumQueryDouble = 1.2D; // double | Query parameter enum test (double) (optional)
var enumQueryInteger = 56; // int? | Query parameter enum test (double) (optional)
var enumQueryDouble = 1.2D; // double? | Query parameter enum test (double) (optional)
var enumFormStringArray = new List<string>(); // List<string> | Form parameter enum test (string array) (optional) (default to $)
var enumFormString = enumFormString_example; // string | Form parameter enum test (string) (optional) (default to -efg)

Expand Down Expand Up @@ -776,8 +776,8 @@ Name | Type | Description | Notes
**enumHeaderString** | **string**| Header parameter enum test (string) | [optional] [default to -efg]
**enumQueryStringArray** | **List&lt;string&gt;**| Query parameter enum test (string array) | [optional]
**enumQueryString** | **string**| Query parameter enum test (string) | [optional] [default to -efg]
**enumQueryInteger** | **int**| Query parameter enum test (double) | [optional]
**enumQueryDouble** | **double**| Query parameter enum test (double) | [optional]
**enumQueryInteger** | **int?**| Query parameter enum test (double) | [optional]
**enumQueryDouble** | **double?**| Query parameter enum test (double) | [optional]
**enumFormStringArray** | [**List&lt;string&gt;**](string.md)| Form parameter enum test (string array) | [optional] [default to $]
**enumFormString** | **string**| Form parameter enum test (string) | [optional] [default to -efg]

Expand Down Expand Up @@ -808,7 +808,7 @@ No authorization required

## TestGroupParameters

> void TestGroupParameters (int requiredStringGroup, bool requiredBooleanGroup, long requiredInt64Group, int stringGroup = null, bool booleanGroup = null, long int64Group = null)
> void TestGroupParameters (int requiredStringGroup, bool requiredBooleanGroup, long requiredInt64Group, int? stringGroup = null, bool? booleanGroup = null, long? int64Group = null)

Fake endpoint to test group parameters (optional)

Expand All @@ -834,9 +834,9 @@ namespace Example
var requiredStringGroup = 56; // int | Required String in group parameters
var requiredBooleanGroup = true; // bool | Required Boolean in group parameters
var requiredInt64Group = 789; // long | Required Integer in group parameters
var stringGroup = 56; // int | String in group parameters (optional)
var booleanGroup = true; // bool | Boolean in group parameters (optional)
var int64Group = 789; // long | Integer in group parameters (optional)
var stringGroup = 56; // int? | String in group parameters (optional)
var booleanGroup = true; // bool? | Boolean in group parameters (optional)
var int64Group = 789; // long? | Integer in group parameters (optional)

try
{
Expand All @@ -862,9 +862,9 @@ Name | Type | Description | Notes
**requiredStringGroup** | **int**| Required String in group parameters |
**requiredBooleanGroup** | **bool**| Required Boolean in group parameters |
**requiredInt64Group** | **long**| Required Integer in group parameters |
**stringGroup** | **int**| String in group parameters | [optional]
**booleanGroup** | **bool**| Boolean in group parameters | [optional]
**int64Group** | **long**| Integer in group parameters | [optional]
**stringGroup** | **int?**| String in group parameters | [optional]
**booleanGroup** | **bool?**| Boolean in group parameters | [optional]
**int64Group** | **long?**| Integer in group parameters | [optional]

### Return type

Expand Down
Loading