swagger忽略ComplexItems属性。我需要为我的客户端应用程序生成axios模型。
public class DummyModel
{
[Key]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
// This is your complex list
public List<ComplexItem> ComplexItems { get; set; } = new List<ComplexItem>();
}
public class ComplexItem
{
[Key]
public Guid Id { get; set; }
public int Property1 { get; set; }
public string Property2 { get; set; }
public DateTime Property3 { get; set; }
}
在Api上下文中,我添加了关系。
modelBuilder.Entity<DummyModel>()
.HasMany(dm => dm.ComplexItems)
.WithOne() // or WithMany() based on your needs
.HasForeignKey("ForeignKeyColumnIfAny");
在我添加的控制器中包括:
return await _context.DummyModels
.Include(dm => dm.ComplexItems)
.ToListAsync();
但在没有列表属性的情况下,模型看起来是这样的:
export interface DummyModel {
/**
*
* @type {string}
* @memberof DummyModel
*/
id?: string;
/**
*
* @type {string}
* @memberof DummyModel
*/
name: string;
}
使现代化
程序.cs
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
....
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.SchemaFilter<ExcludeNullableSchemaFilter>();
});
DummyController
[HttpPost("DummyModelTest")]
public List<DummyModel> Post(DummyModel dummyModel)
{
List<DummyModel> list = new List<DummyModel> { dummyModel };
return list;
}