Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move MapOpenApi() Inside the IsDevelopment() block for consistency wi… #34541

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aspnetcore/fundamentals/openapi/aspnetcore-openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET
ms.author: safia
monikerRange: '>= aspnetcore-6.0'
ms.custom: mvc
ms.date: 12/11/2024
ms.date: 01/23/2025
uid: fundamentals/openapi/aspnetcore-openapi
---
# Generate OpenAPI documents
Expand Down Expand Up @@ -48,7 +48,7 @@ The following code:
* Adds OpenAPI services using the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method on the app builder's service collection.
* Maps an endpoint for viewing the OpenAPI document in JSON format with the <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointRouteBuilderExtensions.MapOpenApi%2A> extension method on the app.

[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)]
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,9)]

Launch the app and navigate to `https://localhost:<port>/openapi/v1.json` to view the generated OpenAPI document.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//#define DOCUMENTtransformer1
//#define DOCUMENTtransformer2
#define DOCUMENTtransformerUse999
Copy link
Contributor

Choose a reason for hiding this comment

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

Removed dupe preprocessor directive for default.

//#define DEFAULT
//#define FIRST
//#define OPENAPIWITHSCALAR
//#define MAPOPENAPIWITHCACHING
Expand Down Expand Up @@ -80,7 +79,10 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
Copy link
Contributor

Choose a reason for hiding this comment

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

Added Development environment condition for all calls to app.MapOpenAPI() and tested each.

{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand All @@ -107,7 +109,10 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand Down Expand Up @@ -161,7 +166,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand Down Expand Up @@ -189,7 +197,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/world", () => "Hello world!")
.WithGroupName("internal");
Expand Down Expand Up @@ -253,7 +264,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => new Body { Amount = 1.1m });

Expand All @@ -279,9 +293,10 @@ public class Body {

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
Expand Down Expand Up @@ -342,8 +357,11 @@ public class Body {

app.UseOutputCache();

app.MapOpenApi()
.CacheOutput();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi()
.CacheOutput();
}

app.MapGet("/", () => "Hello world!");

Expand All @@ -365,10 +383,9 @@ public class Body {

var app = builder.Build();

app.MapOpenApi();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}

Expand All @@ -386,7 +403,10 @@ public class Body {

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand Down Expand Up @@ -419,7 +439,10 @@ public class Body {

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand Down Expand Up @@ -473,7 +496,10 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext

var app = builder.Build();

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapGet("/", () => "Hello world!");

Expand Down
Loading