Configuring a custom Swagger / Swashbuckle serializer IControllerConfiguration ASP.NET WebAPI

I have a WebAPI endpoint that implements two different versions of the API (legacy and new). Inherited endpoints use a specific Serializer that has all objects serialized as lowercase words with underscores, endpoint v2 uses camel shell property names. For example, V1 = "document_type" and V2 = "documentType"

Currently, this is achieved using special controller attributes to determine serialization, for example:

public class CamelCaseControllerConfiguration : Attribute, IControllerConfiguration { public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { controllerSettings.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); controllerSettings.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter()); } } 

All this works fine when invoking a client via REST, but the documentation created by Swagger always shows property names using outdated serializer options. Any suggestions on tweaking swashbuckle to properly serialize each version?

+7
c # asp.net-web-api swagger swashbuckle
source share
1 answer

as far as I know, swagger uses the first Formatters settings that can be found. so if you use this:

 controllerSettings.Formatters.Insert(0, new JsonMediaTypeFormatter { SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() } }); 

your documentation created by Swagger will be wonderful. swagger is a very good library, and I hope they can solve this problem soon.

0
source share

All Articles