-
Notifications
You must be signed in to change notification settings - Fork 2.8k
APIs: Use EndpointMetadata to check for MapToApiAttribute at runtime to include Controller in Swagger document #20144
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
Open
mdubbelv
wants to merge
9
commits into
umbraco:main
Choose a base branch
from
mdubbelv:v16/bugfix/20143-hasmaptoapiattribute-use-endpointmetadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+37
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a01496b
Use EndpointMetadata to check for existing MapToApiAttribute at runtime
mdubbelv-consid a3ec05e
fix api breaking change
mdubbelv-consid 0bdd155
revert MethodInfoApiCommonExtensions.cs
mdubbelv 2e8a489
remove empty line in ActionDescriptorApiCommonExtensions.cs
mdubbelv 4fca335
Merge branch 'main' into v16/bugfix/20143-hasmaptoapiattribute-use-en…
mdubbelv 2d99e1a
Add xml comments to ActionDescriptorApiCommonExtensions
mdubbelv-consid 79830f1
Merge branch 'v16/bugfix/20143-hasmaptoapiattribute-use-endpointmetad…
mdubbelv-consid 8b92f2f
Revert boy scout refactoring to primary constructur
mdubbelv-consid 44e7ebd
Better xml comments in ActionDescriptorApiCommonExtensions
mdubbelv-consid 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
36 changes: 36 additions & 0 deletions
36
src/Umbraco.Cms.Api.Common/Extensions/ActionDescriptorApiCommonExtensions.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,36 @@ | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Umbraco.Cms.Api.Common.Attributes; | ||
using Umbraco.Cms.Api.Common.Configuration; | ||
|
||
namespace Umbraco.Extensions; | ||
|
||
/// <summary> | ||
/// Provides extension methods for <see cref="ActionDescriptor"/> to work with <see cref="MapToApiAttribute"/>. | ||
/// </summary> | ||
public static class ActionDescriptorApiCommonExtensions | ||
{ | ||
/// <summary> | ||
/// Determines whether the <see cref="ActionDescriptor"/> has a <see cref="MapToApiAttribute"/> with the specified API name. | ||
/// The check is made in runtime to support attributes added in runtime. | ||
/// </summary> | ||
/// <param name="actionDescriptor">The action descriptor to inspect.</param> | ||
/// <param name="apiName">The API name to check for.</param> | ||
/// <returns> | ||
/// <c>true</c> if the <see cref="MapToApiAttribute"/> is present and matches the specified API name, | ||
/// or if the attribute is not present and the API name matches the default API name; otherwise, <c>false</c>. | ||
/// </returns> | ||
public static bool HasMapToApiAttribute(this ActionDescriptor actionDescriptor, string apiName) | ||
{ | ||
var value = actionDescriptor.GetMapToApiAttributeValue(); | ||
|
||
return value == apiName | ||
|| (value is null && apiName == DefaultApiConfiguration.ApiName); | ||
} | ||
|
||
private static string? GetMapToApiAttributeValue(this ActionDescriptor actionDescriptor) | ||
{ | ||
IEnumerable<MapToApiAttribute> mapToApiAttributes = actionDescriptor?.EndpointMetadata?.OfType<MapToApiAttribute>() ?? []; | ||
|
||
return mapToApiAttributes.SingleOrDefault()?.ApiName; | ||
} | ||
} |
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.