/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/OpenApi/sample/Controllers/TestController.cs
126 строк
4 KB
Korolev Dmitry
ApiExplorer and OpenAPI unions support (#67001)
10 июн 2026, 15:04
Не верифицирован
10 июн 2026, 15:04
bd9f3b7
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing; [ApiController] [Route("[controller]")] [ApiExplorerSettings(GroupName = "controllers")] public class TestController : ControllerBase { [HttpGet] [Route("/getbyidandname/{id}/{name}")] public string GetByIdAndName(RouteParamsContainer paramsContainer) { return paramsContainer.Id + "_" + paramsContainer.Name; } [HttpGet] [Route("/gettypedresult")] public Ok<MvcTodo> GetTypedResult() { return TypedResults.Ok(new MvcTodo("Title", "Description", true)); } [HttpPost] [Route("/forms")] public IActionResult PostForm([FromForm] MvcTodo todo) { return Ok(todo); } [HttpGet] [Route("/getcultureinvariant")] public Ok<CurrentWeather> GetCurrentWeather() { return TypedResults.Ok(new CurrentWeather(1.0f)); } [Route("/nohttpmethod")] public ActionResult<CurrentWeather> NoHttpMethod() => Ok(new CurrentWeather(-100)); [HttpQuery] [Route("/query")] public ActionResult<CurrentWeather> HttpQueryMethod() => Ok(new CurrentWeather(0)); [HttpFoo] // See https://github.com/dotnet/aspnetcore/issues/60914 [Route("/unsupported")] public ActionResult<CurrentWeather> UnsupportedHttpMethod() => Ok(new CurrentWeather(100)); [HttpQuery] [Route("/query-with-body")] public ActionResult<MvcTodo> HttpQueryWithBodyMethod([FromBody] MvcTodo todo) => Ok(todo); [HttpGet] [Route("/multi-content-type")] [ProducesResponseType(typeof(MvcTodo), StatusCodes.Status200OK, "application/json")] [ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/plain")] public IActionResult GetMultiContentType() => Ok(new MvcTodo("Title", "Description", true)); [HttpGet] [Route("/any-of")] [ProducesResponseType(typeof(MvcTodo), StatusCodes.Status200OK, "application/json")] [ProducesResponseType(typeof(CurrentWeather), StatusCodes.Status200OK, "application/json")] public IActionResult GetAnyOf() => Ok(new MvcTodo("Title", "Description", true)); [HttpGet] [Route("/dup-description")] [ProducesResponseType(typeof(MvcTodo), StatusCodes.Status200OK, "application/json", Description = "Use it!")] [ProducesResponseType(typeof(MvcTodo), StatusCodes.Status200OK, Description = "Returns a Todo")] public IActionResult GetDuplicateDescription() => Ok(new MvcTodo("Title", "Description", true)); [HttpGet] [Route("/union-pet")] [ProducesResponseType(typeof(UnionPet), StatusCodes.Status200OK)] public IActionResult GetUnionPet() => Ok(new UnionPet(new Kitten("Whiskers", 9))); [HttpGet] [Route("/union-value")] [ProducesResponseType(typeof(UnionIntString), StatusCodes.Status200OK)] public IActionResult GetUnionValue() => Ok(new UnionIntString(42)); [HttpGet] [Route("/union-clinic")] [ProducesResponseType(typeof(Clinic), StatusCodes.Status200OK)] public IActionResult GetUnionClinic() => Ok(new Clinic("123 Vet Ave", new UnionPet(new Puppy("Rex", "Beagle")))); [HttpGet] [Route("/union-any-of")] [ProducesResponseType(typeof(UnionPet), StatusCodes.Status200OK, "application/json")] [ProducesResponseType(typeof(Clinic), StatusCodes.Status200OK, "application/json")] public IActionResult GetUnionAnyOf() => Ok(new UnionPet(new Kitten("Mittens", 7))); public class HttpQuery() : HttpMethodAttribute(["QUERY"]); public class HttpFoo() : HttpMethodAttribute(["FOO"]); public class RouteParamsContainer { [FromRoute(Name = "id")] public int Id { get; set; } [FromRoute(Name = "name")] [MinLength(5)] [UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "MinLengthAttribute works without reflection on string properties.")] public string? Name { get; set; } } public record MvcTodo(string Title, string Description, bool IsCompleted); public record CurrentWeather([property: Range(-100.5f, 100.5f)] float Temperature = 0.1f); }