Как вручную добавить в раздел схемы swagger? - PullRequest
2 голосов
/ 02 февраля 2020

Я использую 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; }
    }
}

1 Ответ

0 голосов
/ 02 февраля 2020

Попробуйте сначала разделить эти классы в разных файлах. Вторая попытка добавить перед вашими классами следующие комментарии:

/// <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;
...
...