Как настроить Swagger, чтобы он знает, что «класс» доступен только для чтения, и не включать его в пользовательский интерфейс для POST и PUT?
Текущая модель ClassSection:
Код: Выделить всё
public class ClassSection
{
[Key]
public int Id { get; set; }
[ForeignKey(nameof(Id))]
public int ProfessorId { get; set; }
[ForeignKey("Class")]
public int ClassId { get; set; }
[SwaggerSchema(ReadOnly = true)]
public Class Class { get; set; }
[Required]
public int Section { get; set; }
[Required]
public DateTime StartTime { get; set; }
[Required]
public DateTime EndTime { get; set; }
[Required]
public string Building { get; set; }
[Required]
public int Room { get; set; }
[Required]
public int Capacity { get; set; }
}
Код: Выделить всё
public class SwaggerFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type == typeof(ClassSection))
{
schema.Properties["class"].ReadOnly = true;
}
}
}
Код: Выделить всё
Program.csКод: Выделить всё
builder.Services.AddSwaggerGen(options =>
{
options.EnableAnnotations();
options.SchemaFilter();
});
[img]https://i .sstatic.net/6Q6zSqBM.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/790 ... -read-only
Мобильная версия