-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swagger sub types selectors (take 2) (#17132)
* Initial implementation * Remove conflicting constructor (was obsolete for 15 anyway) * Don't use primary constructors * Fix swagger path segment qualifier * Make non-interface method protected * Use constant for splitting string * Update document name parsing --------- Co-authored-by: mattbrailsford <[email protected]>
- Loading branch information
1 parent
b496186
commit 79ff0e0
Showing
7 changed files
with
105 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public interface ISubTypesHandler | ||
{ | ||
bool CanHandle(Type type, string documentName); | ||
|
||
IEnumerable<Type> Handle(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public interface ISubTypesSelector | ||
{ | ||
IEnumerable<Type> SubTypes(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Umbraco.Cms.Api.Common.Serialization; | ||
|
||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public class SubTypesHandler : ISubTypesHandler | ||
{ | ||
private readonly IUmbracoJsonTypeInfoResolver _umbracoJsonTypeInfoResolver; | ||
|
||
public SubTypesHandler(IUmbracoJsonTypeInfoResolver umbracoJsonTypeInfoResolver) | ||
=> _umbracoJsonTypeInfoResolver = umbracoJsonTypeInfoResolver; | ||
|
||
protected virtual bool CanHandle(Type type) | ||
=> type.Namespace?.StartsWith("Umbraco.Cms") is true; | ||
|
||
public virtual bool CanHandle(Type type, string documentName) | ||
=> CanHandle(type); | ||
|
||
public virtual IEnumerable<Type> Handle(Type type) | ||
=> _umbracoJsonTypeInfoResolver.FindSubTypes(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Umbraco.Cms.Api.Common.Serialization; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Configuration.Models; | ||
using Umbraco.Cms.Core.Hosting; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Api.Common.OpenApi; | ||
|
||
public class SubTypesSelector : ISubTypesSelector | ||
{ | ||
private readonly IOptions<GlobalSettings> _settings; | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IEnumerable<ISubTypesHandler> _subTypeHandlers; | ||
private readonly IUmbracoJsonTypeInfoResolver _umbracoJsonTypeInfoResolver; | ||
|
||
public SubTypesSelector( | ||
IOptions<GlobalSettings> settings, | ||
IHostingEnvironment hostingEnvironment, | ||
IHttpContextAccessor httpContextAccessor, | ||
IEnumerable<ISubTypesHandler> subTypeHandlers, | ||
IUmbracoJsonTypeInfoResolver umbracoJsonTypeInfoResolver) | ||
{ | ||
_settings = settings; | ||
_hostingEnvironment = hostingEnvironment; | ||
_httpContextAccessor = httpContextAccessor; | ||
_subTypeHandlers = subTypeHandlers; | ||
_umbracoJsonTypeInfoResolver = umbracoJsonTypeInfoResolver; | ||
} | ||
|
||
public IEnumerable<Type> SubTypes(Type type) | ||
{ | ||
var backOfficePath = _settings.Value.GetBackOfficePath(_hostingEnvironment); | ||
var swaggerPath = $"{backOfficePath}/swagger"; | ||
|
||
if (_httpContextAccessor.HttpContext?.Request.Path.StartsWithSegments(swaggerPath) ?? false) | ||
{ | ||
// Split the path into segments | ||
var segments = _httpContextAccessor.HttpContext.Request.Path.Value! | ||
.Substring(swaggerPath.Length) | ||
.TrimStart(Constants.CharArrays.ForwardSlash) | ||
.Split(Constants.CharArrays.ForwardSlash); | ||
|
||
// Extract the document name from the path | ||
var documentName = segments[0]; | ||
|
||
// Find the first handler that can handle the type / document name combination | ||
ISubTypesHandler? handler = _subTypeHandlers.FirstOrDefault(h => h.CanHandle(type, documentName)); | ||
if (handler != null) | ||
{ | ||
return handler.Handle(type); | ||
} | ||
} | ||
|
||
// Default implementation to maintain backwards compatibility | ||
return _umbracoJsonTypeInfoResolver.FindSubTypes(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