如何手动添加到swagger的架构部分?

问题描述 投票:1回答:1

我正在使用Swashbuckle.AspNetCore并立即使用此模式部分:screenshot of swagger schemas

我的响应模式看起来是空的,但实际上我返回了子类。如何将它们添加到此部分?

我有此请求模型:

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; }
    }
}
c# asp.net-core swagger swashbuckle
1个回答
0
投票

请先尝试将这些类分隔在不同的文件中。第二次尝试在您的课程之前添加此注释:

/// <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;
...
© www.soinside.com 2019 - 2024. All rights reserved.