From 4ce462f88fe82a9602031a6c25a270754070f337 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 19 Sep 2021 19:01:48 +0200 Subject: [PATCH] Swagger: Adding alternative routes --- src/Directory.Build.props | 8 ++++ .../Kyoo.Abstractions.csproj | 17 ++------ .../AltRoute/AltHttpGetAttribute.cs | 19 +++++++++ .../Attributes/AltRoute/AltRouteAttribute.cs | 39 +++++++++++++++++++ src/Kyoo.Core/Kyoo.Core.csproj | 1 + src/Kyoo.Core/Views/CollectionApi.cs | 11 +++--- src/Kyoo.Swagger/Kyoo.Swagger.csproj | 1 + src/Kyoo.Swagger/SwaggerModule.cs | 21 ++++++++-- 8 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltHttpGetAttribute.cs create mode 100644 src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltRouteAttribute.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 47ab383d..5efb3ebc 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -3,10 +3,18 @@ Kyoo Kyoo Copyright (c) Kyoo + true GPL-3.0-or-later true + https://github.com/AnonymusRaccoon/Kyoo git + true + https://github.com/AnonymusRaccoon/Kyoo + + 1.0.0 + true + snupkg diff --git a/src/Kyoo.Abstractions/Kyoo.Abstractions.csproj b/src/Kyoo.Abstractions/Kyoo.Abstractions.csproj index faf51b1b..59fdf472 100644 --- a/src/Kyoo.Abstractions/Kyoo.Abstractions.csproj +++ b/src/Kyoo.Abstractions/Kyoo.Abstractions.csproj @@ -1,20 +1,9 @@ - net5.0 - Kyoo.Abstractions - Zoe Roux - Base package to create plugins for Kyoo. - https://github.com/AnonymusRaccoon/Kyoo - true - https://github.com/AnonymusRaccoon/Kyoo - SDG - GPL-3.0-or-later - true - 1.0.0 - true - snupkg default + Kyoo.Abstractions + Base package to create plugins for Kyoo. Kyoo.Abstractions @@ -22,9 +11,9 @@ + - diff --git a/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltHttpGetAttribute.cs b/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltHttpGetAttribute.cs new file mode 100644 index 00000000..99f843b6 --- /dev/null +++ b/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltHttpGetAttribute.cs @@ -0,0 +1,19 @@ +using JetBrains.Annotations; +using Microsoft.AspNetCore.Mvc; + +namespace Kyoo.Abstractions.Models.Attributes +{ + /// + /// A custom that indicate an alternatives, hidden route. + /// + public class AltHttpGetAttribute : HttpGetAttribute + { + /// + /// Create a new . + /// + /// The route template, see . + public AltHttpGetAttribute([NotNull] [RouteTemplateAttribute] string template) + : base(template) + { } + } +} diff --git a/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltRouteAttribute.cs b/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltRouteAttribute.cs new file mode 100644 index 00000000..71734979 --- /dev/null +++ b/src/Kyoo.Abstractions/Models/Attributes/AltRoute/AltRouteAttribute.cs @@ -0,0 +1,39 @@ +// Kyoo - A portable and vast media library solution. +// Copyright (c) Kyoo. +// +// See AUTHORS.md and LICENSE file in the project root for full license information. +// +// Kyoo is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// Kyoo is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Kyoo. If not, see . + +using System; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Mvc; + +namespace Kyoo.Abstractions.Models.Attributes +{ + /// + /// A custom that indicate an alternatives, hidden route. + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] + public class AltRouteAttribute : RouteAttribute + { + /// + /// Create a new . + /// + /// The route template, see . + public AltRouteAttribute([NotNull] [RouteTemplateAttribute] string template) + : base(template) + { } + } +} diff --git a/src/Kyoo.Core/Kyoo.Core.csproj b/src/Kyoo.Core/Kyoo.Core.csproj index 58a23762..03e8785e 100644 --- a/src/Kyoo.Core/Kyoo.Core.csproj +++ b/src/Kyoo.Core/Kyoo.Core.csproj @@ -60,6 +60,7 @@ + diff --git a/src/Kyoo.Core/Views/CollectionApi.cs b/src/Kyoo.Core/Views/CollectionApi.cs index db4ac5bc..6ff5fe9b 100644 --- a/src/Kyoo.Core/Views/CollectionApi.cs +++ b/src/Kyoo.Core/Views/CollectionApi.cs @@ -22,6 +22,7 @@ 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; @@ -30,8 +31,8 @@ using Microsoft.Extensions.Options; namespace Kyoo.Core.Api { - [Route("api/collection")] [Route("api/collections")] + [AltRoute("api/collection")] [ApiController] [PartialPermission(nameof(CollectionApi))] public class CollectionApi : CrudApi @@ -51,8 +52,8 @@ namespace Kyoo.Core.Api _thumbs = thumbs; } - [HttpGet("{id:int}/show")] [HttpGet("{id:int}/shows")] + [AltHttpGet("{id:int}/show")] [PartialPermission(Kind.Read)] public async Task>> GetShows(int id, [FromQuery] string sortBy, @@ -77,8 +78,8 @@ namespace Kyoo.Core.Api } } - [HttpGet("{slug}/show")] [HttpGet("{slug}/shows")] + [AltHttpGet("{slug}/show")] [PartialPermission(Kind.Read)] public async Task>> GetShows(string slug, [FromQuery] string sortBy, @@ -103,8 +104,8 @@ namespace Kyoo.Core.Api } } - [HttpGet("{id:int}/library")] [HttpGet("{id:int}/libraries")] + [AltHttpGet("{id:int}/library")] [PartialPermission(Kind.Read)] public async Task>> GetLibraries(int id, [FromQuery] string sortBy, @@ -129,8 +130,8 @@ namespace Kyoo.Core.Api } } - [HttpGet("{slug}/library")] [HttpGet("{slug}/libraries")] + [AltHttpGet("{slug}/library")] [PartialPermission(Kind.Read)] public async Task>> GetLibraries(string slug, [FromQuery] string sortBy, diff --git a/src/Kyoo.Swagger/Kyoo.Swagger.csproj b/src/Kyoo.Swagger/Kyoo.Swagger.csproj index 13104cde..713eef6e 100644 --- a/src/Kyoo.Swagger/Kyoo.Swagger.csproj +++ b/src/Kyoo.Swagger/Kyoo.Swagger.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Kyoo.Swagger/SwaggerModule.cs b/src/Kyoo.Swagger/SwaggerModule.cs index c53703d4..84375c49 100644 --- a/src/Kyoo.Swagger/SwaggerModule.cs +++ b/src/Kyoo.Swagger/SwaggerModule.cs @@ -19,9 +19,10 @@ 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.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; @@ -47,9 +48,9 @@ namespace Kyoo.Swagger /// public void Configure(IServiceCollection services) { - services.AddSwaggerGen(x => + services.AddSwaggerGen(options => { - x.SwaggerDoc("v1", new OpenApiInfo + options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Kyoo API", @@ -67,7 +68,15 @@ namespace Kyoo.Swagger }); foreach (string documentation in Directory.GetFiles(AppContext.BaseDirectory, "*.xml")) - x.IncludeXmlComments(documentation); + options.IncludeXmlComments(documentation); + + options.UseAllOfForInheritance(); + + options.DocInclusionPredicate((_, apiDescription) => + { + return apiDescription.ActionDescriptor.EndpointMetadata + .All(x => x is not AltRouteAttribute && x is not AltHttpGetAttribute); + }); }); } @@ -78,6 +87,10 @@ namespace Kyoo.Swagger SA.New(app => app.UseSwaggerUI(x => { x.SwaggerEndpoint("/swagger/v1/swagger.json", "Kyoo v1"); + }), SA.Before), + SA.New(app => app.UseReDoc(x => + { + x.SpecUrl = "/swagger/v1/swagger.json"; }), SA.Before) }; }