forked from loafle/openapi-generator-original
Expose JsonSerializerSettings in ApiClient (#7582)
* Expose JsonSerializerSettings in ApiClient * Update generated petstore sample * Add XML comments for SerializerSettings and match new OverrideSpecifiedNames default * Add GetSerializerSettingsTest
This commit is contained in:
parent
4dc8d2a351
commit
cf185d559b
@ -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
|
||||
{
|
||||
// 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>
|
||||
@ -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)
|
||||
@ -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);
|
||||
@ -509,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);
|
||||
|
@ -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
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -164,6 +164,23 @@ namespace Org.OpenAPITools.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
|
||||
{
|
||||
// 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>
|
||||
@ -262,7 +279,7 @@ namespace Org.OpenAPITools.Client
|
||||
RestRequest request = new RestRequest(Method(method))
|
||||
{
|
||||
Resource = path,
|
||||
JsonSerializer = new CustomJsonCodec(configuration)
|
||||
JsonSerializer = new CustomJsonCodec(SerializerSettings, configuration)
|
||||
};
|
||||
|
||||
if (options.PathParameters != null)
|
||||
@ -410,7 +427,7 @@ namespace Org.OpenAPITools.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);
|
||||
@ -512,7 +529,7 @@ namespace Org.OpenAPITools.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);
|
||||
|
@ -165,6 +165,23 @@ namespace Org.OpenAPITools.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
|
||||
{
|
||||
// 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>
|
||||
@ -263,7 +280,7 @@ namespace Org.OpenAPITools.Client
|
||||
RestRequest request = new RestRequest(Method(method))
|
||||
{
|
||||
Resource = path,
|
||||
JsonSerializer = new CustomJsonCodec(configuration)
|
||||
JsonSerializer = new CustomJsonCodec(SerializerSettings, configuration)
|
||||
};
|
||||
|
||||
if (options.PathParameters != null)
|
||||
@ -411,7 +428,7 @@ namespace Org.OpenAPITools.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);
|
||||
@ -513,7 +530,7 @@ namespace Org.OpenAPITools.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);
|
||||
|
@ -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
|
||||
};
|
||||
|
@ -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
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user