Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !7582
An error occurred while fetching the assigned milestone of the selected merge_request.

Expose JsonSerializerSettings in ApiClient

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/davidyee/feature/add-custom-json-serializer into master 4 years ago
  • Overview 0
  • Commits 4
  • Pipelines 0
  • Changes 7

Created by: davidyee

In this PR, I expose the JsonSerializerSettings in the ApiClient so that serialization customizations can be done. For example, in my specific use-case, I wanted to add additional Converters to change the way JSON was being deserialized back into the csharp classes.

Fixes #4241 (closed)

I reviewed the comments made on this related PR #4466 and incorporated the feedback into this PR.

@mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.
Compare
  • master (base)

and
  • latest version
    7cf23cd3
    4 commits, 2 years ago

7 files
+ 112
- 15

    Preferences

    File browser
    Compare changes
modules/openapi-gen‎erator/…/…/resources‎
csh‎arp‎
ApiClient‎.mustache‎ +1 -1
csharp-‎netcore‎
ApiClient‎.mustache‎ +21 -4
samples/cli‎ent/petstore‎
csh‎arp‎
OpenAPIClient‎/src/…/Client‎
ApiCli‎ent.cs‎ +1 -1
OpenAPIClientNetSt‎andard/src/…/Client‎
ApiCli‎ent.cs‎ +1 -1
csharp-‎netcore‎
OpenAPIC‎lient/src‎
Org.OpenAP‎ITools.Test‎
ApiClien‎tTests.cs‎ +46 -0
Org.OpenAPI‎Tools/Client‎
ApiCli‎ent.cs‎ +21 -4
OpenAPIClientCo‎re/src/…/Client‎
ApiCli‎ent.cs‎ +21 -4
modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache
+ 1
- 1
  • View file @ 7cf23cd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -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
};
modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache
+ 21
- 4
  • View file @ 7cf23cd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -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);
@@ -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));
@@ -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);
samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs
+ 1
- 1
  • View file @ 7cf23cd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -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
};
samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/ApiClient.cs
+ 1
- 1
  • View file @ 7cf23cd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -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
};
samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs
+ 21
- 4
  • View file @ 7cf23cd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -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);
@@ -439,7 +456,7 @@ namespace Org.OpenAPITools.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));
@@ -516,7 +533,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);
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
1
1 participant
Administrator
Reference:
Source branch: github/fork/davidyee/feature/add-custom-json-serializer

Menu

Explore Projects Groups Snippets