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

Expose JsonSerializerSettings in ApiClient #7582

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 @@ -160,6 +160,23 @@ namespace {{packageName}}.Client
{
private readonly String _baseUrl;

/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
/// These settings can be adjusted to accomodate custom serialization rules.
/// </summary>
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings
davidyee marked this conversation as resolved.
Show resolved Hide resolved
{
// OpenAPI generated types generally hide default constructors.
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy
{
OverrideSpecifiedNames = false
}
}
};

/// <summary>
/// Allows for extending request processing for <see cref="ApiClient"/> generated code.
/// </summary>
Expand Down Expand Up @@ -258,7 +275,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)
Expand Down Expand Up @@ -406,7 +423,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);
Expand Down Expand Up @@ -435,7 +452,7 @@ namespace {{packageName}}.Client
InterceptRequest(req);

IRestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null)
if (RetryConfiguration.RetryPolicy != null)
{
var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
Expand Down Expand Up @@ -513,7 +530,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace {{packageName}}.Client
/// </summary>
{{>visibility}} partial class ApiClient
{
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
public JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Class for testing ApiClient
/// </summary>
public class ApiClientTests
{
public ApiClientTests()
{
}

/// <summary>
/// Test GetSerializerSettingsTest
/// </summary>
[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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient
{
private readonly String _baseUrl;

/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
/// These settings can be adjusted to accomodate custom serialization rules.
/// </summary>
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings
{
// OpenAPI generated types generally hide default constructors.
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy
{
OverrideSpecifiedNames = false
}
}
};

/// <summary>
/// Allows for extending request processing for <see cref="ApiClient"/> generated code.
/// </summary>
Expand Down Expand Up @@ -262,7 +279,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)
Expand Down Expand Up @@ -410,7 +427,7 @@ private ApiResponse<T> Exec<T>(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);
Expand Down Expand Up @@ -439,7 +456,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
InterceptRequest(req);

IRestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null)
if (RetryConfiguration.RetryPolicy != null)
{
var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
Expand Down Expand Up @@ -516,7 +533,7 @@ private ApiResponse<T> Exec<T>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ public partial class ApiClient : ISynchronousClient, IAsynchronousClient
{
private readonly String _baseUrl;

/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
/// These settings can be adjusted to accomodate custom serialization rules.
/// </summary>
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings
{
// OpenAPI generated types generally hide default constructors.
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy
{
OverrideSpecifiedNames = false
}
}
};

/// <summary>
/// Allows for extending request processing for <see cref="ApiClient"/> generated code.
/// </summary>
Expand Down Expand Up @@ -263,7 +280,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)
Expand Down Expand Up @@ -411,7 +428,7 @@ private ApiResponse<T> Exec<T>(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);
Expand Down Expand Up @@ -440,7 +457,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
InterceptRequest(req);

IRestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null)
if (RetryConfiguration.RetryPolicy != null)
{
var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
Expand Down Expand Up @@ -517,7 +534,7 @@ private ApiResponse<T> Exec<T>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Client
/// </summary>
public partial class ApiClient
{
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
public JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Client
/// </summary>
public partial class ApiClient
{
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
public JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Expand Down