C# Api 控制器 - 如何实现 swagger oneOf

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

我必须用C#实现这种Api控制器:

       requestBody:
        description: A JSON object containing pet information
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
                - $ref: '#/components/schemas/Hamster'

其中一个文档:https://swagger.io/docs/specification/describing-request-body/ 预期输出:

我正在使用:

<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />

我已经尝试过使用动态或接口,但 swagger 生成器没有执行图中所示的 oneOf 模式。

有 C# 示例吗?

c# asp.net-core swagger asp.net-core-webapi swashbuckle
1个回答
0
投票

这是为您准备的教程,希望对您有所帮助。

1。创建一个基本的 WebApi 项目,如下所示。

2。在 Program.cs 中使用以下设置

    builder.Services.AddSwaggerGen(swaggerGenOptions =>
    {
        swaggerGenOptions.UseAllOfForInheritance();
        swaggerGenOptions.UseOneOfForPolymorphism();

        swaggerGenOptions.SelectSubTypesUsing(baseType =>
            typeof(Program).Assembly.GetTypes().Where(type => type.IsSubclassOf(baseType))
        );
    });

3.修改WeatherForecast.cs文件。

namespace SwaggerOneOf
{
    public class WeatherForecast
    {
        public DateTime Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string? Summary { get; set; }
    }

    public class WeatherForecastWithLocation : WeatherForecast
    {
        public string? Location { get; set; }
    }
}

4。修改控制器上的方法。

using Microsoft.AspNetCore.Mvc;

namespace SwaggerOneOf.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get() =>
            DateTime.Now.Minute < 30
                ? Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                })
                : Enumerable.Range(1, 5).Select(index => new WeatherForecastWithLocation
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)],
                    Location = "London"
                })
                .ToArray();
    }
}

5。测试结果

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.