我有一个WebAPI端点,它实现了两个不同版本的API(旧版和新版)。旧端点使用特定的串行器,该序列化器将所有对象序列化为带有下划线的小写字,v2端点使用驼峰式属性名称。例如,V1 =" document_type"和V2 =" documentType"
目前使用控制器特定属性来定义序列化,如下所示:
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());
}
}
当通过REST从客户端调用时,这一切都正常,但Swagger生成的文档始终使用旧的序列化设置显示属性名称。有关配置swashbuckle以正确序列化每个版本的任何建议吗?
答案 0 :(得分:0)
据我所知,swagger使用可以找到的第一个Formatters
设置。所以,如果你使用它:
controllerSettings.Formatters.Insert(0, new JsonMediaTypeFormatter { SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() } });
Swagger生成的文档没问题。 swagger是一个非常好的图书馆,我希望他们能尽快解决这个问题。