From 669acda78690f585fa4cc2dded052cc5eac2d9b2 Mon Sep 17 00:00:00 2001 From: David Yee Date: Fri, 2 Oct 2020 12:29:12 -0600 Subject: [PATCH 1/4] Expose JsonSerializerSettings in ApiClient --- .../csharp-netcore/ApiClient.mustache | 21 +++++++++++++++---- .../main/resources/csharp/ApiClient.mustache | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache index 5221740208c8..dd41b63f1607 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache @@ -160,6 +160,19 @@ namespace {{packageName}}.Client { private readonly String _baseUrl; + public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = true + } + } + }; + /// /// Allows for extending request processing for generated code. /// @@ -258,7 +271,7 @@ namespace {{packageName}}.Client RestRequest request = new RestRequest(Method(method)) { Resource = path, - JsonSerializer = new CustomJsonCodec(configuration) + JsonSerializer = new CustomJsonCodec(SerializerSettings, configuration) }; if (options.PathParameters != null) @@ -406,7 +419,7 @@ namespace {{packageName}}.Client } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); @@ -435,7 +448,7 @@ namespace {{packageName}}.Client InterceptRequest(req); IRestResponse response; - if (RetryConfiguration.RetryPolicy != null) + if (RetryConfiguration.RetryPolicy != null) { var policy = RetryConfiguration.RetryPolicy; var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); @@ -513,7 +526,7 @@ namespace {{packageName}}.Client } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); diff --git a/modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache index 8495fb2e8a0e..6d202d290550 100644 --- a/modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache @@ -30,7 +30,7 @@ namespace {{packageName}}.Client /// {{>visibility}} partial class ApiClient { - private JsonSerializerSettings serializerSettings = new JsonSerializerSettings + public JsonSerializerSettings serializerSettings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }; From 3275ea4712ea07a5a76ab3db053b2f8f8795e0ab Mon Sep 17 00:00:00 2001 From: David Yee Date: Fri, 2 Oct 2020 12:29:48 -0600 Subject: [PATCH 2/4] Update generated petstore sample --- .../src/Org.OpenAPITools/Client/ApiClient.cs | 21 +++++++++++++++---- .../src/Org.OpenAPITools/Client/ApiClient.cs | 21 +++++++++++++++---- .../src/Org.OpenAPITools/Client/ApiClient.cs | 2 +- .../src/Org.OpenAPITools/Client/ApiClient.cs | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index 40870dcffc69..9e375c28d3aa 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -164,6 +164,19 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { private readonly String _baseUrl; + public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = true + } + } + }; + /// /// Allows for extending request processing for generated code. /// @@ -262,7 +275,7 @@ private RestRequest NewRequest( RestRequest request = new RestRequest(Method(method)) { Resource = path, - JsonSerializer = new CustomJsonCodec(configuration) + JsonSerializer = new CustomJsonCodec(SerializerSettings, configuration) }; if (options.PathParameters != null) @@ -410,7 +423,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); @@ -439,7 +452,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura InterceptRequest(req); IRestResponse response; - if (RetryConfiguration.RetryPolicy != null) + if (RetryConfiguration.RetryPolicy != null) { var policy = RetryConfiguration.RetryPolicy; var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); @@ -516,7 +529,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs index 3b0ae496fc59..297ebc057d52 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -165,6 +165,19 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { private readonly String _baseUrl; + public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = true + } + } + }; + /// /// Allows for extending request processing for generated code. /// @@ -263,7 +276,7 @@ private RestRequest NewRequest( RestRequest request = new RestRequest(Method(method)) { Resource = path, - JsonSerializer = new CustomJsonCodec(configuration) + JsonSerializer = new CustomJsonCodec(SerializerSettings, configuration) }; if (options.PathParameters != null) @@ -411,7 +424,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); @@ -440,7 +453,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura InterceptRequest(req); IRestResponse response; - if (RetryConfiguration.RetryPolicy != null) + if (RetryConfiguration.RetryPolicy != null) { var policy = RetryConfiguration.RetryPolicy; var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); @@ -517,7 +530,7 @@ private ApiResponse Exec(RestRequest req, IReadableConfiguration configura } else { - var customDeserializer = new CustomJsonCodec(configuration); + var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); diff --git a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index 21a140ed84e1..26b1205ce633 100644 --- a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Client /// public partial class ApiClient { - private JsonSerializerSettings serializerSettings = new JsonSerializerSettings + public JsonSerializerSettings serializerSettings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }; diff --git a/samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/ApiClient.cs index 3367117774f1..33758aa1883d 100644 --- a/samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/ApiClient.cs @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Client /// public partial class ApiClient { - private JsonSerializerSettings serializerSettings = new JsonSerializerSettings + public JsonSerializerSettings serializerSettings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }; From 7457fdbff0e3fddf9ff7704e53f0924e8687aa19 Mon Sep 17 00:00:00 2001 From: David Yee Date: Sun, 11 Oct 2020 17:39:36 -0600 Subject: [PATCH 3/4] Add XML comments for SerializerSettings and match new OverrideSpecifiedNames default --- .../src/main/resources/csharp-netcore/ApiClient.mustache | 6 +++++- .../OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs | 6 +++++- .../src/Org.OpenAPITools/Client/ApiClient.cs | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache index dd41b63f1607..3f05aaf63807 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache @@ -160,6 +160,10 @@ namespace {{packageName}}.Client { private readonly String _baseUrl; + /// + /// Specifies the settings on a object. + /// These settings can be adjusted to accomodate custom serialization rules. + /// public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings { // OpenAPI generated types generally hide default constructors. @@ -168,7 +172,7 @@ namespace {{packageName}}.Client { NamingStrategy = new CamelCaseNamingStrategy { - OverrideSpecifiedNames = true + OverrideSpecifiedNames = false } } }; diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index 9e375c28d3aa..67080e92706e 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -164,6 +164,10 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { private readonly String _baseUrl; + /// + /// Specifies the settings on a object. + /// These settings can be adjusted to accomodate custom serialization rules. + /// public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings { // OpenAPI generated types generally hide default constructors. @@ -172,7 +176,7 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { NamingStrategy = new CamelCaseNamingStrategy { - OverrideSpecifiedNames = true + OverrideSpecifiedNames = false } } }; diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs index 297ebc057d52..02db6d6d5541 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -165,6 +165,10 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { private readonly String _baseUrl; + /// + /// Specifies the settings on a object. + /// These settings can be adjusted to accomodate custom serialization rules. + /// public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings { // OpenAPI generated types generally hide default constructors. @@ -173,7 +177,7 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient { NamingStrategy = new CamelCaseNamingStrategy { - OverrideSpecifiedNames = true + OverrideSpecifiedNames = false } } }; From 7cf23cd38d7612891207f8fa63609c182f3329aa Mon Sep 17 00:00:00 2001 From: David Yee Date: Sun, 11 Oct 2020 17:40:22 -0600 Subject: [PATCH 4/4] Add GetSerializerSettingsTest --- .../Org.OpenAPITools.Test/ApiClientTests.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ApiClientTests.cs diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ApiClientTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ApiClientTests.cs new file mode 100644 index 000000000000..9a001a63e0b2 --- /dev/null +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ApiClientTests.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reflection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using RestSharp; +using Xunit; + +using Org.OpenAPITools.Client; +using Org.OpenAPITools.Api; +using Org.OpenAPITools.Model; + +namespace Org.OpenAPITools.Test +{ + /// + /// Class for testing ApiClient + /// + public class ApiClientTests + { + public ApiClientTests() + { + } + + /// + /// Test GetSerializerSettingsTest + /// + [Fact] + public void GetSerializerSettingsTest() + { + ApiClient apiClient = new ApiClient(); + + var serializerSettingsPropertyInfo = typeof(ApiClient).GetProperty(nameof(ApiClient.SerializerSettings)); + + // Validate that we can the set the SerializerSettings (public visibility) + Assert.NotNull(serializerSettingsPropertyInfo?.GetSetMethod()); + + // Validate default serializer settings + Assert.NotNull(apiClient.SerializerSettings); + Assert.Equal(ConstructorHandling.AllowNonPublicDefaultConstructor, apiClient.SerializerSettings.ConstructorHandling); + Assert.False(((DefaultContractResolver)apiClient.SerializerSettings.ContractResolver).NamingStrategy.OverrideSpecifiedNames); + } + } +}