![]() |
|
ASP.NET Core 2.20-preview1: Open API Analyzers & Conventions - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic & .NET (https://sickgaming.net/forum-79.html) +--- Thread: ASP.NET Core 2.20-preview1: Open API Analyzers & Conventions (/thread-86165.html) |
ASP.NET Core 2.20-preview1: Open API Analyzers & Conventions - xSicKxBot - 08-23-2018 ASP.NET Core 2.20-preview1: Open API Analyzers & Conventions What is it? Open API (alternatively known as Swagger) is a language-agnostic specification for describing REST APIs. The Open API ecosystem has tools that allows for discovering, testing and producing client code using the specification. Support for generating and visualizing Open API documents in ASP.NET Core MVC is provided via community driven projects such as NSwag, and Swashbuckle.AspNetCore. Visit https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.1 to learn more about Open API Swagger and for details on configuring your applications to use it. For 2.2, we’re investing in tooling and runtime experiences to to allow developers to produce better Open API documents. This work ties in with ongoing work to perform client code SDK generation during build. How to use it? Analyzer For 2.2, we’re introducing a new API-specific analyzers NuGet package – Code: Microsoft.AspNetCore.Mvc.Api.AnalyzersCode: ApiControllerCode: <PackageReference Include="Microsoft.AspNetCore.Mvc.Api.Analyzers"
Version="2.2.0-preview1-35029"
PrivateAssets="All" />Open API documents contain each status code and response type an operation may return. In MVC, you use attributes such as Code: ProducesResponseTypeCode: ProducesCode: ApiControllerLet’s look at the analyzer in action: The analyzer identified that the action returned a Code: 404Code: ProducesResponseTypeAttributeConventions If your controllers follows some common patterns, e.g. they are all primarily CRUD endpoints, and you aren’t already using Code: ProducesResponseTypeCode: ProducesCode: ProducesResponseTypeBy default, ASP.NET Core MVC 2.2 ships with a set of default conventions – Code: DefaultApiConventionsAt runtime, ApiExplorer understand conventions. ApiExplorer is MVC’s abstraction to communicate with Open API document generators. Attributes from the applied convention get associated with an action and will be included in action’s Swagger documentation. API analyzers also understand conventions. If your action is unconventional i.e. it returns a status code that is not documented by the applied convention, it will produce a warning, encouraging you to document it. There are 3 ways to apply a convention to a controller action:
Code: [assembly: ApiConvention(typeof(DefaultApiConventions))]
Code: [ApiConvention(typeof(DefaultApiConventions))]
[ApiController]
[Route("/api/[controller]")]
public class PetsController : ControllerBase
{ ...
}
Code: // PUT: api/Pets/5
[ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Put))]
[HttpPut("{id}")]
public async Task<ActionResult<Pet>> PutPet(long id, Pet pet)
{ ...
}Like many other features in MVC, more specific attributes will supersede less specific ones. An API metadata attribute such as Code: ProducesResponseTypeCode: ProducesCode: ApiConventionMethodCode: ApiConventionTypeCode: ApiConventionTypeAuthoring conventions A convention is a static type with methods. These methods are annotated with Code: ProducesResponseTypeCode: ProducesDefaultResponseTypeCode: public static class MyAppConventions
{ [ProducesResponseType(200)] [ProducesResponseType(404)] public static void Find(int id) {
}
}Applying this convention to an assembly would result in the convention method applying to any action with the name Code: FindCode: idIn addition to Code: ProducesResponseTypeCode: ProducesDefaultResponseTypeCode: ApiConventionNameMatchCode: ApiConventionTypeMatchCode: [ProducesResponseType(200)]
[ProducesResponseType(404)]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Find( [ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)] int id)
{ }The Code: ApiConventionNameMatchBehavior.PrefixCode: FindCode: FindPetCode: FindByIdCode: ApiConventionNameMatchBehavior.SuffixCode: idCode: idCode: petIdCode: ApiConventionTypeMatchCode: params[]An easy way to get started authoring a custom convention is to start by copying the body of Code: DefaultApiConventionsFeedback This is one on our earliest forays in trying to use tooling to enhance runtime experiences. We’re interested in any thoughts you have about this as well as your experiences using this in your applications. The best place to provide feedback is by opening issues at https://github.com/aspnet/Mvc. Additional help |