Improving current methods for setting OpenAPI response descriptions with Minimal APIs #58724
Labels
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
design-proposal
This issue represents a design proposal for a different issue, linked in the description
feature-openapi
Summary
The purpose of this design proposal is to allow developers that use Minimal API's to set OpenAPI response descriptions in a more ergonomic way. I created (and implemented) this for controllers with #55656. Some preparations were already done for this in the PR related to the controller's issue (#58193), and this design proposal aims to finish that work.
Motivation and goals
Being able to set a description for each possible response from an endpoint is very useful as it allows a developer to not only know WHAT responses to expect, but also WHEN to expect them, at least in a broad sense. This can improve error handling in client applications and improve understanding of an API.
My proposal contains quite some info, so I want to start with a usage example of the proposal, which extends the
Produces
extension method provided by theMicrosoft.AspNetCore.OpenApi
package with a new parameter for the response description:This would also work for ProducesProblem, etc...
I created an issue for the subject of this API proposal already: #57963 . I decided to recreate this issue with an design proposal to get a discussion started more quickly and with more context.
Setting response descriptions with the MVC approach
Before we go in-depth about this API proposal, I want to create some context about what I mean with the controller support from #55656. This feature will only be released with .NET 10, so keep that in mind when reading the following code, which wouldn't compile with .NET 8 or 9.
To set a response description in a controller, a developer could write the following:
Click here to see the code
which could result in the following OpenAPI document (Parts like problem details and other irrelevant code has been removed for brevity):
In all the projects I've seen and used, response descriptions aren't used often in ASP.NET Core projects. I believe this to be case because setting them used to be quite verbose, needing XML comments or OpenAPI transformers from Swashbuckle/NSwag.
With .NET 10 releasing in November 2025, this is no longer the case with controllers as this is now much easier to do. However, I believe that the support for response descriptions with Minimal API's isn't as smooth.
The current problem of setting response descriptions with Minimal API's
As mentioned before, this design proposal aims to improve the current ways of setting response descriptions. It is already possible, but I am not satisfied with the current options:
Using transformers
The documentation mentions that descriptions can be set using Document Transformers or Operation Transformers.
Let's take an Operation Transformer as an example. From the docs:
This would do the job. However:
Using ProducesResponseType attribute (.NET 10+)
Based on the docs, this functionality could already be implemented using the
ProducesResponseType
family of attributes, which I've implemented Description support for (#55656) and will be released in .NET 10.However, I do not like this approach in the context of minimal API's:
In scope
Enrich the Produces() family of extension methods provided by the
Microsoft.AspNetCore.OpenApi
package with a new parameter for the response description. This would allow developers to set response descriptions in a more ergonomic way.Out of scope
Nothing comes to mind right now.
Risks / unknowns
One risk is that adding a Description parameter to the Produces() family of extension methods would clash with the current
contentType
andadditionalContentTypes
parameters:I'd like to add Description AFTER
statusCode
and/orresponseType
, but this is currently already taken bycontentType
, which is also a string, so this would be a source incompatible change.I do think this is worth it because of the of the following reasons:
contentType
andadditionalContentTypes
overloads. I don't have exact numbers on the following claim, but I'd like to think that developers that use Minimal API's are more likely to use JSON, which means that thecontentType
argument wouldn't be used because the default isapplication/json
anyway.So, perhaps this proposal should start with a discussion about this source compatibility problem and what we want (and don't want) to do to get this response description feature implemented for Minimal API's.
Usage Examples
I believe that extending the OpenAPI extension methods like
Produces()
provided by theMicrosoft.AspNetCore.OpenApi
package are the way to go. They are already used to enrich endpoints with OpenAPI information, and I believe that they should be used to set response descriptions as well:As mentioned in the code comment, this introduces a source incompatible change. This is addressed further in the "Alternative Designs" and "Risks" sections.
Alternative Designs
These alternative designs aim to avoid the source-incompatibility problem mentioned above.
Create new extension methods to avoid source incompatibility problems
We could create new extension methods like
ProducesWithDescription
instead of altering the existingProduces
extension methods (and others..) to avoid the source incompatibility problem.However, I do not like this much as it feels a little "bolted-on". It might also just add confusion for developers because of a larger number of available methods.
Extend the TypedResults methods with Description information
This might be a bit of a stretch, but perhaps we could extend the TypedResults methods with a Description parameter. This way, the description would be set in the same place as where the response is created:
I'm not sure if this is a good idea, though:
Introduce unclear breaking change by making description the first argument of the extension methods, causing a clearer breaking change
Another way to avoid source incompatibility problems is to create a breaking change and make the description the first argument of the OpenAPI Produces extension methods. This way there isn't any confusion about the string being an additional content type.
However, I do not think this is very pretty either, and I believe that creating a breaking change for this is not worth it.
The text was updated successfully, but these errors were encountered: