首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何手动添加到swagger的schemas部分?

如何手动添加到swagger的schemas部分?
EN

Stack Overflow用户
提问于 2020-02-02 08:39:42
回答 1查看 1.5K关注 0票数 4

我正在使用Swashbuckle.AspNetCore,并将这个模式部分开箱即用:

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

我有这个请求模型:

代码语言:javascript
运行
复制
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)
        {
//...

我将此用于我的响应模型:

代码语言:javascript
运行
复制
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; }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-02 08:56:45

首先尝试将这些类分离到不同的文件中。第二,尝试在你的类之前添加以下注释:

代码语言:javascript
运行
复制
/// <summary>
/// Description for Your class
/// </summary>
[KnownType(typeof(InProgressExecuteMoveResponse))]
public class InProgressExecuteMoveResponse : ValidExecuteMoveResponse
{
    ...
}

在某些情况下,要显示摘要,必须在swashbuckle配置中启用它。也可以尝试这样配置Swashbuckle:

代码语言:javascript
运行
复制
  app.UseSwaggerUi3WithApiExplorer(settings =>
        {
            settings.GeneratorSettings.DefaultEnumHandling = EnumHandling.String;
            settings.GeneratorSettings.AllowReferencesWithProperties = true;
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60022353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档