Skip to content

Commit 70ef48e

Browse files
Move MapOpenApi() Inside the IsDevelopment() block for consistency wi… (#34541)
* Move MapOpenApi() Inside the IsDevelopment() block for consistency with templates and other docs The default template with .NET 9 has MapOpenApi() inside the IsDevelopment block. Many examples on MS Learn also have it inside due to limit information exposure. Move inside to minimize potential for confusion to devs. * Conditionally map all calls to OpenApi in development environment * Combined MapOpenAPI and MapScalar in same env condition Line 501, moved app.MapOpenAPI() to same environment condition block for app.MapScalarApiReference() * Updated code highlight --------- Co-authored-by: Wade Pickett <wpickett@microsoft.com>
1 parent cd5722e commit 70ef48e

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

aspnetcore/fundamentals/openapi/aspnetcore-openapi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET
55
ms.author: safia
66
monikerRange: '>= aspnetcore-6.0'
77
ms.custom: mvc
8-
ms.date: 12/11/2024
8+
ms.date: 01/23/2025
99
uid: fundamentals/openapi/aspnetcore-openapi
1010
---
1111
# Generate OpenAPI documents
@@ -48,7 +48,7 @@ The following code:
4848
* Adds OpenAPI services using the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method on the app builder's service collection.
4949
* 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.
5050

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

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

aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//#define DOCUMENTtransformer1
44
//#define DOCUMENTtransformer2
55
#define DOCUMENTtransformerUse999
6-
//#define DEFAULT
76
//#define FIRST
87
//#define OPENAPIWITHSCALAR
98
//#define MAPOPENAPIWITHCACHING
@@ -80,7 +79,10 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary
8079

8180
var app = builder.Build();
8281

83-
app.MapOpenApi();
82+
if (app.Environment.IsDevelopment())
83+
{
84+
app.MapOpenApi();
85+
}
8486

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

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

108110
var app = builder.Build();
109111

110-
app.MapOpenApi();
112+
if (app.Environment.IsDevelopment())
113+
{
114+
app.MapOpenApi();
115+
}
111116

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

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

162167
var app = builder.Build();
163168

164-
app.MapOpenApi();
169+
if (app.Environment.IsDevelopment())
170+
{
171+
app.MapOpenApi();
172+
}
165173

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

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

190198
var app = builder.Build();
191199

192-
app.MapOpenApi();
200+
if (app.Environment.IsDevelopment())
201+
{
202+
app.MapOpenApi();
203+
}
193204

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

254265
var app = builder.Build();
255266

256-
app.MapOpenApi();
267+
if (app.Environment.IsDevelopment())
268+
{
269+
app.MapOpenApi();
270+
}
257271

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

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

280294
var app = builder.Build();
281295

282-
app.MapOpenApi();
283296
if (app.Environment.IsDevelopment())
284297
{
298+
app.MapOpenApi();
299+
285300
app.UseSwaggerUI(options =>
286301
{
287302
options.SwaggerEndpoint("/openapi/v1.json", "v1");
@@ -342,8 +357,11 @@ public class Body {
342357

343358
app.UseOutputCache();
344359

345-
app.MapOpenApi()
346-
.CacheOutput();
360+
if (app.Environment.IsDevelopment())
361+
{
362+
app.MapOpenApi()
363+
.CacheOutput();
364+
}
347365

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

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

366384
var app = builder.Build();
367385

368-
app.MapOpenApi();
369-
370386
if (app.Environment.IsDevelopment())
371387
{
388+
app.MapOpenApi();
372389
app.MapScalarApiReference();
373390
}
374391

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

387404
var app = builder.Build();
388405

389-
app.MapOpenApi();
406+
if (app.Environment.IsDevelopment())
407+
{
408+
app.MapOpenApi();
409+
}
390410

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

@@ -419,7 +439,10 @@ public class Body {
419439

420440
var app = builder.Build();
421441

422-
app.MapOpenApi();
442+
if (app.Environment.IsDevelopment())
443+
{
444+
app.MapOpenApi();
445+
}
423446

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

@@ -473,7 +496,10 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
473496

474497
var app = builder.Build();
475498

476-
app.MapOpenApi();
499+
if (app.Environment.IsDevelopment())
500+
{
501+
app.MapOpenApi();
502+
}
477503

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

0 commit comments

Comments
 (0)