Skip to content

Commit 9ee1a54

Browse files
Add operation and schema transform examples
Add examples for DI-activated operation and schema transformers.
1 parent 55956c6 commit 9ee1a54

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

src/OpenApi/sample/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
2222
});
2323
builder.Services.AddOpenApi("v2", options => {
24+
options.AddSchemaTransformer<AddExternalDocsTransformer>();
25+
options.AddOperationTransformer<AddExternalDocsTransformer>();
2426
options.AddDocumentTransformer(new AddContactTransformer());
2527
options.AddDocumentTransformer((document, context, token) => {
2628
document.Info.License = new OpenApiLicense { Name = "MIT" };
@@ -81,7 +83,8 @@
8183
v2.MapGet("/users", () => new [] { "alice", "bob" })
8284
.WithTags("users");
8385

84-
v2.MapPost("/users", () => Results.Created("/users/1", new { Id = 1, Name = "Test user" }));
86+
v2.MapPost("/users", () => Results.Created("/users/1", new { Id = 1, Name = "Test user" }))
87+
.WithName("CreateUser");
8588

8689
responses.MapGet("/200-add-xml", () => new TodoWithDueDate(1, "Test todo", false, DateTime.Now.AddDays(1), DateTime.Now))
8790
.Produces<Todo>(additionalContentTypes: "text/xml");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.OpenApi;
5+
using Microsoft.OpenApi.Models;
6+
7+
namespace Sample.Transformers;
8+
9+
public sealed class AddExternalDocsTransformer(IConfiguration configuration) : IOpenApiOperationTransformer, IOpenApiSchemaTransformer
10+
{
11+
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
12+
{
13+
if (operation.OperationId is { Length: > 0 } id &&
14+
Uri.TryCreate(configuration["DocumentationBaseUrl"], UriKind.Absolute, out var baseUri))
15+
{
16+
var url = new Uri(baseUri, $"/api/docs/operations/{Uri.EscapeDataString(id)}");
17+
18+
operation.ExternalDocs = new OpenApiExternalDocs
19+
{
20+
Description = "Documentation for this OpenAPI endpoint",
21+
Url = url
22+
};
23+
}
24+
25+
return Task.CompletedTask;
26+
}
27+
28+
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken)
29+
{
30+
if (Uri.TryCreate(configuration["DocumentationBaseUrl"], UriKind.Absolute, out var baseUri))
31+
{
32+
var url = new Uri(baseUri, $"/api/docs/schemas/{Uri.EscapeDataString(schema.Type)}");
33+
34+
schema.ExternalDocs = new OpenApiExternalDocs
35+
{
36+
Description = "Documentation for this OpenAPI schema",
37+
Url = url
38+
};
39+
}
40+
return Task.CompletedTask;
41+
}
42+
}

src/OpenApi/sample/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"DocumentationBaseUrl": "https://example.com",
23
"Logging": {
34
"LogLevel": {
45
"Default": "Information",

src/OpenApi/test/Integration/snapshots/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=v2.verified.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"tags": [
3535
"Sample"
3636
],
37+
"externalDocs": {
38+
"description": "Documentation for this OpenAPI endpoint",
39+
"url": "https://example.com/api/docs/operations/CreateUser"
40+
},
41+
"operationId": "CreateUser",
3742
"responses": {
3843
"200": {
3944
"description": "OK"
@@ -48,6 +53,10 @@
4853
"type": "array",
4954
"items": {
5055
"type": "string"
56+
},
57+
"externalDocs": {
58+
"description": "Documentation for this OpenAPI schema",
59+
"url": "https://example.com/api/docs/schemas/array"
5160
}
5261
}
5362
}

0 commit comments

Comments
 (0)