8000 Custom JSON serializer · Issue #609 · RicoSuter/NSwag · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Custom JSON serializer #609

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

Closed
danielreuterwall opened this issue Feb 20, 2017 · 10 comments
Closed

Custom JSON serializer #609

danielreuterwall opened this issue Feb 20, 2017 · 10 comments
Assignees

Comments

@danielreuterwall
Copy link

We have implemented a custom contract resolver for our WebAPI defined in Application_Start like this:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CustomContractResolver();

It works great for the WebAPI but it's not respected by Swagger. Looking at the SwaggerUiOwinSettings we use to inject Swagger into the project, we can only set a fixed set of contract resolvers defined in NJsonSchema.PropertyNameHandling using the config parameter DefaultPropertyNameHandling.

Is it possible to configure Swagger to use our custom contract resolver or any other way around this?

@RicoSuter
Copy link
Owner

Currently not possible, but we should refactor to use JsonContract for generation: RicoSuter/NJsonSchema#321

@RicoSuter
Copy link
Owner

What exactly did you customize in the contract resolver?

8000

@danielreuterwall
Copy link
Author

We have swedish characters in our internal models that we need to escape for the Typescript generator to work. The contract resolver takes care of that.

@danielreuterwall
Copy link
Author

Ideally we might not have Swedish characters in our models. However if the WebAPI is set up to use a custom contract resolver this should be respected by Swagger.

@RicoSuter
Copy link
Owner

Where do you have Swedish characters? In the property names?

@danielreuterwall
Copy link
Author

Yep, property and type names.

8000

@RicoSuter
Copy link
Owner

Can you give some samples, i think this is just a fix in the property name generator

@danielreuterwall
Copy link
Author
danielreuterwall commented Feb 27, 2017

Sure thing!
Let's say we have a model with swedish characters

[DataContract]
public class ModelWithÅÄÖ {
  [DataMember]
  public string PropertyWithÅÄÖ { get; set; }
}

Then we have our custom contract resolver that escapes the Swedish characters like this

protected override string ResolvePropertyName(string propertyName)
  {
    var escapedPropertyName = propertyName
      .Replace('å', 'a')
      .Replace('ä', 'a')
      .Replace('ö', 'o')
      .Replace('Å', 'A')
      .Replace('Ä', 'A')
      .Replace('Ö', 'O');

  return escapedPropertyName ;
}

For the type names we have another issue as that's not part of the contract resolver.

@RicoSuter
Copy link
Owner

v11 of NSwag has the new ContractResolver-based implementation... You can now provide your resolver so that your serialization logic is used...

@RicoSuter
Copy link
Owner

Please test with the CI artifacts and report and problems...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0