mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-04 22:24:14 -04:00
Swagger: Cleaning up alternative routes and sort order
This commit is contained in:
parent
4ce462f88f
commit
ced12c2fe6
@ -11,7 +11,6 @@
|
||||
<PackageReference Include="Autofac" Version="6.2.0" />
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="System.ComponentModel.Composition" Version="5.0.0" />
|
||||
|
@ -1,19 +0,0 @@
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Kyoo.Abstractions.Models.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom <see cref="HttpGetAttribute"/> that indicate an alternatives, hidden route.
|
||||
/// </summary>
|
||||
public class AltHttpGetAttribute : HttpGetAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new <see cref="AltHttpGetAttribute"/>.
|
||||
/// </summary>
|
||||
/// <param name="template">The route template, see <see cref="RouteAttribute.Template"/>.</param>
|
||||
public AltHttpGetAttribute([NotNull] [RouteTemplateAttribute] string template)
|
||||
: base(template)
|
||||
{ }
|
||||
}
|
||||
}
|
@ -16,24 +16,17 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Kyoo.Abstractions.Models.Attributes
|
||||
namespace Kyoo.Abstractions.Models.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom <see cref="RouteAttribute"/> that indicate an alternatives, hidden route.
|
||||
/// A class containing constant numbers.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
|
||||
public class AltRouteAttribute : RouteAttribute
|
||||
public static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new <see cref="AltRouteAttribute"/>.
|
||||
/// A property to use on a Microsoft.AspNet.MVC.Route.Order property to mark it as an alternative route
|
||||
/// that won't be included on the swagger.
|
||||
/// </summary>
|
||||
/// <param name="template">The route template, see <see cref="RouteAttribute.Template"/>.</param>
|
||||
public AltRouteAttribute([NotNull] [RouteTemplateAttribute] string template)
|
||||
: base(template)
|
||||
{ }
|
||||
public const int AlternativeRoute = 1;
|
||||
}
|
||||
}
|
@ -22,17 +22,17 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Attributes;
|
||||
using Kyoo.Abstractions.Models.Exceptions;
|
||||
using Kyoo.Abstractions.Models.Permissions;
|
||||
using Kyoo.Core.Models.Options;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using static Kyoo.Abstractions.Models.Utils.Constants;
|
||||
|
||||
namespace Kyoo.Core.Api
|
||||
{
|
||||
[Route("api/collections")]
|
||||
[AltRoute("api/collection")]
|
||||
[Route("api/collection", Order = AlternativeRoute)]
|
||||
[ApiController]
|
||||
[PartialPermission(nameof(CollectionApi))]
|
||||
public class CollectionApi : CrudApi<Collection>
|
||||
@ -53,7 +53,7 @@ namespace Kyoo.Core.Api
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/shows")]
|
||||
[AltHttpGet("{id:int}/show")]
|
||||
[HttpGet("{id:int}/show", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
public async Task<ActionResult<Page<Show>>> GetShows(int id,
|
||||
[FromQuery] string sortBy,
|
||||
@ -79,7 +79,7 @@ namespace Kyoo.Core.Api
|
||||
}
|
||||
|
||||
[HttpGet("{slug}/shows")]
|
||||
[AltHttpGet("{slug}/show")]
|
||||
[HttpGet("{slug}/show", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
public async Task<ActionResult<Page<Show>>> GetShows(string slug,
|
||||
[FromQuery] string sortBy,
|
||||
@ -105,7 +105,7 @@ namespace Kyoo.Core.Api
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/libraries")]
|
||||
[AltHttpGet("{id:int}/library")]
|
||||
[HttpGet("{id:int}/library", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
public async Task<ActionResult<Page<Library>>> GetLibraries(int id,
|
||||
[FromQuery] string sortBy,
|
||||
@ -131,7 +131,7 @@ namespace Kyoo.Core.Api
|
||||
}
|
||||
|
||||
[HttpGet("{slug}/libraries")]
|
||||
[AltHttpGet("{slug}/library")]
|
||||
[HttpGet("{slug}/library", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
public async Task<ActionResult<Page<Library>>> GetLibraries(string slug,
|
||||
[FromQuery] string sortBy,
|
||||
|
@ -19,12 +19,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models.Attributes;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using static Kyoo.Abstractions.Models.Utils.Constants;
|
||||
|
||||
namespace Kyoo.Swagger
|
||||
{
|
||||
@ -71,12 +70,9 @@ namespace Kyoo.Swagger
|
||||
options.IncludeXmlComments(documentation);
|
||||
|
||||
options.UseAllOfForInheritance();
|
||||
|
||||
options.DocInclusionPredicate((_, apiDescription) =>
|
||||
{
|
||||
return apiDescription.ActionDescriptor.EndpointMetadata
|
||||
.All(x => x is not AltRouteAttribute && x is not AltHttpGetAttribute);
|
||||
});
|
||||
options.SwaggerGeneratorOptions.SortKeySelector = x => x.RelativePath;
|
||||
options.DocInclusionPredicate((_, apiDescription)
|
||||
=> apiDescription.ActionDescriptor.AttributeRouteInfo?.Order != AlternativeRoute);
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user