rob*_*más 5 c# swagger swashbuckle asp.net-core
我正在使用 Swashbuckle.AspNetCore 并将此模式部分开箱即用:
我的响应模式看起来是空的,但实际上我返回了子类。我怎样才能将它们添加到此部分?
我有这个请求模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using static Assignment_1.AppGlobals;
namespace Assignment_1
{
public class ExecuteMoveRequest: IValidatableObject, ICloneable
{
public int? Move { get; set; }
[Required]
public BoardSymbol AzurePlayerSymbol { get; set; }
[Required]
public BoardSymbol HumanPlayerSymbol { get; set; }
[MinLength(9)]
[MaxLength(9)]
public BoardSymbol[] GameBoard { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//...
Run Code Online (Sandbox Code Playgroud)
我将其用于我的响应模型:
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using static Assignment_1.AppGlobals;
namespace Assignment_1
{
public interface ExecuteMoveResponse
{
}
[KnownType(typeof(ValidExecuteMoveResponse))]
public class ValidExecuteMoveResponse: ExecuteMoveRequest, ExecuteMoveResponse
{
public string type { get; set; }
}
[KnownType(typeof(InProgressExecuteMoveResponse))]
public class InProgressExecuteMoveResponse : ValidExecuteMoveResponse
{
}
[KnownType(typeof(WonExecuteMoveResponse))]
public class WonExecuteMoveResponse : ValidExecuteMoveResponse
{
[Required]
public BoardSymbol Winner { get; set; }
[Required]
public int[] WinPositions { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
首先尝试将这些类分离到不同的文件中。第二次尝试在您的课程之前添加以下评论:
/// <summary>
/// Description for Your class
/// </summary>
[KnownType(typeof(InProgressExecuteMoveResponse))]
public class InProgressExecuteMoveResponse : ValidExecuteMoveResponse
{
...
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,要显示摘要,您必须在 swashbuckle 配置中启用它。还尝试以这种方式配置 Swashbuckle:
app.UseSwaggerUi3WithApiExplorer(settings =>
{
settings.GeneratorSettings.DefaultEnumHandling = EnumHandling.String;
settings.GeneratorSettings.AllowReferencesWithProperties = true;
...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4988 次 |
最近记录: |