-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Open API: Add fluent builder for registering custom backoffice OpenAPI documents #22774
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
Merged
lauraneto
merged 12 commits into
release/18.0
from
v18/feature/add-back-office-open-api-document-extension
May 12, 2026
+1,648
−436
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
36274a2
Add helper for registering custom backoffice OpenAPI documents
lauraneto 9ebc2ba
Refactor backoffice OpenAPI helper into a fluent builder
lauraneto e54e9fe
Rename WithHttpJsonOptions to WithJsonOptions
lauraneto 8a759c2
Merge branch 'release/18.0' into v18/feature/add-back-office-open-api…
lauraneto 4a7914a
Add WithJsonOptions(string) overload for named HTTP JsonOptions
lauraneto efc902c
Migrate Management API OpenAPI registration to AddBackOfficeOpenApiDo…
lauraneto 1331c3c
Cleanup unused usings
lauraneto 14910cf
Address PR review feedback on AddBackOfficeOpenApiDocument
lauraneto bdddc40
Remove redundant operation-id override from extension template
lauraneto 37062cc
Narrow MimeTypesTransformer to JSON-equivalent variants and register …
lauraneto ee1bd55
Register RequireNonNullablePropertiesSchemaTransformer in AddBackOffi…
lauraneto 0f030a4
Apply review notes
lauraneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
src/Umbraco.Cms.Api.Common/OpenApi/BackOfficeOpenApiDocumentBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| using Microsoft.AspNetCore.Http.Json; | ||
| using Microsoft.AspNetCore.OpenApi; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Umbraco.Cms.Api.Common.DependencyInjection; | ||
| using Umbraco.Cms.Core.DependencyInjection; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
|
||
| /// <summary> | ||
| /// Fluent builder for configuring a custom OpenAPI document. | ||
| /// </summary> | ||
| public sealed class BackOfficeOpenApiDocumentBuilder | ||
| { | ||
| private readonly List<Action<OpenApiOptions>> _configurations = []; | ||
|
|
||
| private string? _title; | ||
| private string? _uiTitle; | ||
| private bool _includedInUi = true; | ||
| private Func<IServiceProvider, JsonOptions>? _httpJsonOptionsFactory; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BackOfficeOpenApiDocumentBuilder"/> class. | ||
| /// </summary> | ||
| /// <param name="documentName">The name of the OpenAPI document being configured.</param> | ||
| internal BackOfficeOpenApiDocumentBuilder(string documentName) | ||
| => DocumentName = documentName; | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the OpenAPI document being configured. | ||
| /// </summary> | ||
| public string DocumentName { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the document's <c>Info.Title</c>. Also used as the UI dropdown label unless overridden via | ||
| /// <see cref="WithUiTitle"/>. | ||
| /// </summary> | ||
| /// <param name="title">The title to display.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder WithTitle(string title) | ||
| { | ||
| _title = title; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Overrides the UI dropdown label for this document. | ||
| /// </summary> | ||
| /// <param name="uiTitle">The label to display.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder WithUiTitle(string uiTitle) | ||
| { | ||
| _uiTitle = uiTitle; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Excludes this document from the UI dropdown. | ||
| /// </summary> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder ExcludeFromUi() | ||
| { | ||
| _includedInUi = false; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds an <see cref="OpenApiOptions"/> configuration callback. Multiple calls compose. | ||
| /// </summary> | ||
| /// <param name="configure">Callback to configure the options.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder ConfigureOpenApiOptions(Action<OpenApiOptions> configure) | ||
| { | ||
| _configurations.Add(configure); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the named <see cref="JsonOptions">Microsoft.AspNetCore.Http.Json.JsonOptions</see> used when | ||
| /// generating this document's schema. Use this to match the serialization conventions of the API | ||
| /// endpoints the document describes. | ||
| /// </summary> | ||
| /// <param name="jsonOptionsName">The name of the registered HTTP <see cref="JsonOptions"/> to apply.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder WithJsonOptions(string jsonOptionsName) | ||
| => WithJsonOptions(sp => sp.GetRequiredService<IOptionsMonitor<JsonOptions>>().Get(jsonOptionsName)); | ||
|
|
||
| /// <summary> | ||
| /// Sets the <see cref="JsonOptions">Microsoft.AspNetCore.Http.Json.JsonOptions</see> used when | ||
| /// generating this document's schema. Use this to match the serialization conventions of the API | ||
| /// endpoints the document describes. | ||
| /// </summary> | ||
| /// <param name="jsonOptions">The HTTP JSON options to apply.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder WithJsonOptions(JsonOptions jsonOptions) | ||
| => WithJsonOptions(_ => jsonOptions); | ||
|
|
||
| /// <summary> | ||
| /// Sets a factory that produces the <see cref="JsonOptions">Microsoft.AspNetCore.Http.Json.JsonOptions</see> | ||
| /// used when generating this document's schema. Use this to match the serialization conventions of the | ||
| /// API endpoints the document describes. | ||
| /// </summary> | ||
| /// <param name="jsonOptionsFactory">Factory invoked when the schema service is first resolved.</param> | ||
| /// <returns>The same builder for chaining.</returns> | ||
| public BackOfficeOpenApiDocumentBuilder WithJsonOptions(Func<IServiceProvider, JsonOptions> jsonOptionsFactory) | ||
| { | ||
| _httpJsonOptionsFactory = jsonOptionsFactory; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies the accumulated configuration to the supplied <see cref="IUmbracoBuilder"/>'s service | ||
| /// collection. Called by <c>AddBackOfficeOpenApiDocument</c> once the user-supplied callback returns. | ||
| /// </summary> | ||
| /// <param name="builder">The Umbraco builder to register services against.</param> | ||
| internal void Build(IUmbracoBuilder builder) | ||
| { | ||
| builder.Services.AddOpenApi( | ||
| DocumentName, | ||
| options => | ||
| { | ||
| options.ShouldInclude = apiDescription => | ||
| apiDescription.ActionDescriptor.HasMapToApiAttribute(DocumentName); | ||
|
|
||
| options.CreateSchemaReferenceId = UmbracoSchemaIdGenerator.CreateSchemaReferenceId; | ||
|
|
||
| if (_title is not null) | ||
| { | ||
| options.AddDocumentTransformer((document, _, _) => | ||
| { | ||
| document.Info.Title = _title; | ||
| return Task.CompletedTask; | ||
| }); | ||
| } | ||
|
|
||
| // Generate operation IDs using Umbraco's naming conventions. | ||
| options.AddOperationTransformer<UmbracoOperationIdTransformer>(); | ||
|
|
||
| // Trim redundant JSON-equivalent MIME types (e.g. text/json, application/*+json, text/plain) | ||
| // that ASP.NET Core adds alongside application/json. | ||
| options.AddOperationTransformer<MimeTypesTransformer>(); | ||
|
|
||
| // Mark non-nullable properties as required so generated SDKs reflect the C# nullability. | ||
| options.AddSchemaTransformer<RequireNonNullablePropertiesSchemaTransformer>(); | ||
|
|
||
| // Tag actions by group name and cleanup unused tags (caused by the tag changes). | ||
| options | ||
| .AddOperationTransformer<TagActionsByGroupNameTransformer>() | ||
| .AddDocumentTransformer<TagActionsByGroupNameTransformer>() | ||
| .AddDocumentTransformer<SortTagsAndPathsTransformer>(); | ||
|
|
||
| foreach (Action<OpenApiOptions> configure in _configurations) | ||
| { | ||
| configure(options); | ||
| } | ||
| }); | ||
|
|
||
| if (_includedInUi) | ||
| { | ||
| builder.Services.AddOpenApiDocumentToUi(DocumentName, _uiTitle ?? _title); | ||
| } | ||
|
|
||
| if (_httpJsonOptionsFactory is not null) | ||
| { | ||
| builder.Services.ReplaceOpenApiSchemaService(DocumentName, _httpJsonOptionsFactory); | ||
| } | ||
| } | ||
| } | ||
|
lauraneto marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.