【发布时间】:2020-05-11 02:43:15
【问题描述】:
我使用来自jsonapi.org 的JSONAPI 规范,然后我使用JsonApiSerializer 来完成JSONAPI 规范,所以我的响应和请求正文如下所示:
{
"data": {
"type": "articles",
"id": "stringId",
"attributes": {
"title": "JSON:API paints my bikeshed!"
}
}
我有一个实体“文章” 它看起来像:
public class Article
{
public string Id { get; set; }
public string title { get; set; }
}
然后我尝试使用 Swashbuckle Swagger 记录我的 API,但在 Swagger UI 中,我的示例请求和响应正文如下所示:
{
"id": "string",
"title": "string"
}
我认为swagger忽略了JsonApiSerializer,有没有办法改变swagger的默认序列化器并使用我自己的序列化器?
我的 Startup.cs 看起来像:
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(
"v1",
new OpenApiInfo
{
Version = "v1",
Title = "HTT API",
Description = "HTT API provides methods to handle events",
Contact = new OpenApiContact
{
Name = "htt",
Email = "info@htt.com",
},
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddAPIDependencies(this.Configuration);
services.AddControllers().AddNewtonsoftJson(
options =>
{
var serializerSettings = new JsonApiSerializerSettings();
options.SerializerSettings.ContractResolver = serializerSettings.ContractResolver;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
services.Configure<DatabaseSettings>(
this.Configuration.GetSection(nameof(DatabaseSettings)));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "HTT API V1");
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
- 网核3.1
- Swashbuckle.AspNetCore 5.0.0
【问题讨论】:
-
您好,这个问题有更新吗?
标签: c# asp.net-core swagger-ui swashbuckle json-api