我正在使用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)
{
//...
我将此用于我的响应模型:
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; }
}
}
发布于 2020-02-02 08:56:45
首先尝试将这些类分离到不同的文件中。第二,尝试在你的类之前添加以下注释:
/// <summary>
/// Description for Your class
/// </summary>
[KnownType(typeof(InProgressExecuteMoveResponse))]
public class InProgressExecuteMoveResponse : ValidExecuteMoveResponse
{
...
}
在某些情况下,要显示摘要,必须在swashbuckle配置中启用它。也可以尝试这样配置Swashbuckle:
app.UseSwaggerUi3WithApiExplorer(settings =>
{
settings.GeneratorSettings.DefaultEnumHandling = EnumHandling.String;
settings.GeneratorSettings.AllowReferencesWithProperties = true;
...
https://stackoverflow.com/questions/60022353
复制相似问题