question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect OpenApi documentation in Minimal API when using WithOpenApi extension method

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I’m using .NET 7.0. I have created a Minimal API with the following endpoint:

app.MapDelete("api/people/{id:int}", Results<NoContent, NotFound> (int id) =>
{
    return id > 0 ? TypedResults.NoContent() : TypedResults.NotFound();
});

This produces the following page in Swagger:

image

In particular, the description of the 404 response is correctly “Not Found”.

However, if I add the WithOpenApi extension method to the endpoint, like this:

app.MapDelete("api/people/{id:int}", Results<NoContent, NotFound> (int id) =>
{
    return id > 0 ? TypedResults.NoContent() : TypedResults.NotFound();
})
.WithOpenApi(operation =>
{
    operation.Summary = "Deletes a person given the ID";
    operation.Description = "If the person does not exist, a 404 response is returned";
    return operation;
});

I obtain this:

image

So, the 404 response now has the description “Client error”, which is incorrect.

Expected Behavior

The description of 404 response should be “Not Found” even when I use the WithOpenApi extension method.

Steps To Reproduce

Minimal repro here: https://github.com/marcominerva/OpenApiIssue.

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

Visual Studio 2022 17.4.1

.NET SDK: Version: 7.0.100 Commit: e12b7af219

Ambiente di runtime: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.100\

Host: Version: 7.0.0 Architecture: x64 Commit: d099f075e4

.NET SDKs installed: 7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:14
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
captainsafiacommented, Nov 22, 2022

@marcominerva Thank you for reporting the issue and submitting a PR! I’ve opened a backport to .NET 7 so we can fix it there too. 🙇🏽

0reactions
marcominervacommented, Nov 21, 2022

Thank you for the workaround, I’ll try to make a PR with the fix as soon as possible!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use OpenAPI in Minimal API apps
Learn how to use OpenAPI (Swagger and Swashbuckle) features of minimal APIs in ASP.NET Core.
Read more >
Add Swagger description to minimal .NET 6 APIs
Currently support for Open API docs for minimal APIs is quite minimal and does not allow adding descriptions/summaries as far as I can...
Read more >
Open API support for ASP.NET Core Minimal API
This article will discuss about implementing Open API (Swagger) for ASP.NET Core 6.0 minimal API. Today I saw one video from Maria Naggaga ......
Read more >
Swagger OpenAPI Configurations in ASP.NET Core Web API
Learn Swagger OpenAPI Configurations in ASP.NET Core Web API, define request/response types, headers like authorization and accept-language.
Read more >
On build Open API generation in ASP.NET Core
In ASP.NET Core world to decorate your API methods with Open API metadata we either use: attributes or extensions methods (minimal APIs), but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found