Skip to content

Fix Swagger UI configuration to use NSwag instead of Swashbuckle#36929

Merged
wadepickett merged 2 commits intodotnet:mainfrom
Astroa7m:patch-2
Mar 31, 2026
Merged

Fix Swagger UI configuration to use NSwag instead of Swashbuckle#36929
wadepickett merged 2 commits intodotnet:mainfrom
Astroa7m:patch-2

Conversation

@Astroa7m
Copy link
Copy Markdown
Contributor

The tutorial code used Swashbuckle's UseSwaggerUI() method, but the project already has NSwag.AspNetCore installed.

This causes a compilation error: "'WebApplication' does not contain a definition for 'UseSwaggerUI'". The code should use NSwag's UseSwaggerUi() instead.

The tutorial code used Swashbuckle's UseSwaggerUI() method, but the project already has NSwag.AspNetCore installed. This caused a compilation error since the method wasn't available.
Copy link
Copy Markdown

@hooseinsaid hooseinsaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix. The changes correctly replace the Swashbuckle-specific configuration with NSwag equivalents, resolving the compilation issue and aligning with the project’s existing setup.

The implementation is clean and consistent, and the Swagger UI is now working as expected.

@guardrex guardrex requested a review from wadepickett March 28, 2026 00:52
@wadepickett
Copy link
Copy Markdown
Contributor

@copilot
The Swashbuckle.AspNetCore package reference should be removed from the 9.x .csproj. Remove it.
Why:

  • The 9.x article never tells users to install Swashbuckle. The only Swagger-related package the tutorial instructs users to add is NSwag.AspNetCore (line 438 of the article).

  • The 9.x Program.cs doesn't use any Swashbuckle APIs. It uses AddOpenApi() / MapOpenApi() (built-in .NET 9) for OpenAPI document generation, and UseSwaggerUi() (NSwag) for the Swagger UI. There is no AddSwaggerGen(), UseSwagger(), or Swashbuckle's UseSwaggerUI() anywhere in the 9.x code.

  • It's a leftover from the 7.x/8.x templates and app samples, which did use Swashbuckle. It was never cleaned up when the 9.x sample was created with the new OpenAPI + NSwag approach.

@wadepickett
Copy link
Copy Markdown
Contributor

@Astroa7m, thanks so much for pointing out this issue and correcting it. Greatly appreciated!
Reviewing....

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the ASP.NET Core 9.x first-mongo-app tutorial sample to use NSwag’s Swagger UI middleware (already referenced by the sample) instead of Swashbuckle’s UseSwaggerUI, resolving the reported compilation error and aligning with the repo’s modern OpenAPI pattern.

Changes:

  • Replace UseSwaggerUI with NSwag UseSwaggerUi.
  • Update UI configuration from SwaggerEndpoint(...) to DocumentPath = "/openapi/v1.json" to match MapOpenApi() output.

Removed Swashbuckle.AspNetCore package reference since it is not used for the 9.x version of this sample.
@wadepickett
Copy link
Copy Markdown
Contributor

@Astroa7m , I can approve and merge after policy agreement addressed above. Thanks again for the fix!

Copy link
Copy Markdown
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved and will be merged when Contributor License Agreement(CLA) is signed off.

@Astroa7m
Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@Astroa7m please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

@wadepickett
Copy link
Copy Markdown
Contributor

Great work, thanks!!! Will merge to live today.

@wadepickett wadepickett merged commit ddce1bc into dotnet:main Mar 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants