-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
133 changed files
with
2,669 additions
and
1,423 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,9 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
|
||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public interface IOperationIdSelector | ||
{ | ||
string? OperationId(ApiDescription apiDescription, ApiVersioningOptions apiVersioningOptions); | ||
} |
This file contains 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,6 @@ | ||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public interface ISchemaIdSelector | ||
{ | ||
string SchemaId(Type type); | ||
} |
This file contains 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 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,79 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public class OperationIdSelector : IOperationIdSelector | ||
{ | ||
public virtual string? OperationId(ApiDescription apiDescription, ApiVersioningOptions apiVersioningOptions) | ||
{ | ||
if (apiDescription.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor | ||
|| controllerActionDescriptor.ControllerTypeInfo.Namespace?.StartsWith("Umbraco.Cms.Api") is not true) | ||
{ | ||
return null; | ||
} | ||
|
||
return UmbracoOperationId(apiDescription, apiVersioningOptions); | ||
} | ||
|
||
protected string? UmbracoOperationId(ApiDescription apiDescription, ApiVersioningOptions apiVersioningOptions) | ||
{ | ||
if (apiDescription.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) | ||
{ | ||
return null; | ||
} | ||
|
||
ApiVersion defaultVersion = apiVersioningOptions.DefaultApiVersion; | ||
var httpMethod = apiDescription.HttpMethod?.ToLower().ToFirstUpper() ?? "Get"; | ||
|
||
// if the route info "Name" is supplied we'll use this explicitly as the operation ID | ||
// - usage example: [HttpGet("my-api/route}", Name = "MyCustomRoute")] | ||
if (string.IsNullOrWhiteSpace(apiDescription.ActionDescriptor.AttributeRouteInfo?.Name) == false) | ||
{ | ||
var explicitOperationId = apiDescription.ActionDescriptor.AttributeRouteInfo!.Name; | ||
return explicitOperationId.InvariantStartsWith(httpMethod) | ||
? explicitOperationId | ||
: $"{httpMethod}{explicitOperationId}"; | ||
} | ||
|
||
var relativePath = apiDescription.RelativePath; | ||
|
||
if (string.IsNullOrWhiteSpace(relativePath)) | ||
{ | ||
throw new Exception( | ||
$"There is no relative path for controller action {apiDescription.ActionDescriptor.RouteValues["controller"]}"); | ||
} | ||
|
||
// Remove the prefixed base path with version, e.g. /umbraco/management/api/v1/tracked-reference/{id} => tracked-reference/{id} | ||
var unprefixedRelativePath = OperationIdRegexes | ||
.VersionPrefixRegex() | ||
.Replace(relativePath, string.Empty); | ||
|
||
// Remove template placeholders, e.g. tracked-reference/{id} => tracked-reference/Id | ||
var formattedOperationId = OperationIdRegexes | ||
.TemplatePlaceholdersRegex() | ||
.Replace(unprefixedRelativePath, m => $"By{m.Groups[1].Value.ToFirstUpper()}"); | ||
|
||
// Remove dashes (-) and slashes (/) and convert the following letter to uppercase with | ||
// the word "By" in front, e.g. tracked-reference/Id => TrackedReferenceById | ||
formattedOperationId = OperationIdRegexes | ||
.ToCamelCaseRegex() | ||
.Replace(formattedOperationId, m => m.Groups[1].Value.ToUpper()); | ||
|
||
//Get map to version attribute | ||
string? version = null; | ||
|
||
var versionAttributeValue = controllerActionDescriptor.MethodInfo.GetMapToApiVersionAttributeValue(); | ||
|
||
// We only wanna add a version, if it is not the default one. | ||
if (string.Equals(versionAttributeValue, defaultVersion.ToString()) == false) | ||
{ | ||
version = versionAttributeValue; | ||
} | ||
|
||
// Return the operation ID with the formatted http method verb in front, e.g. GetTrackedReferenceById | ||
return $"{httpMethod}{formattedOperationId.ToFirstUpper()}{version}"; | ||
} | ||
} |
This file contains 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.