feat: add a 200 result schema
This commit is contained in:
parent
269d976ad4
commit
ae4d2073c4
@ -19,6 +19,7 @@ public static class SwaggerConfiguration
|
||||
{
|
||||
options.SchemaFilter<SwaggerExampleFilter>();
|
||||
options.OperationFilter<SwaggerDefaultValues>();
|
||||
options.OperationFilter<ActionResultSchemaFilter>();
|
||||
var basePath = AppDomain.CurrentDomain.BaseDirectory;
|
||||
|
||||
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
|
@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.SwaggerOptions;
|
||||
|
||||
public class ActionResultSchemaFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var returnType = context.MethodInfo.ReturnType;
|
||||
if (!returnType.IsEquivalentTo(typeof(ActionResult)) &&
|
||||
!returnType.IsEquivalentTo(typeof(ContentResult)) &&
|
||||
!returnType.IsEquivalentTo(typeof(FileStreamResult)) &&
|
||||
!returnType.IsGenericType)
|
||||
return;
|
||||
|
||||
if (returnType.IsGenericType &&
|
||||
!returnType.GetGenericTypeDefinition().IsEquivalentTo(typeof(ActionResult<>)) &&
|
||||
!returnType.GetGenericTypeDefinition().IsEquivalentTo(typeof(Task<>)))
|
||||
return;
|
||||
|
||||
var genericType = returnType.IsGenericType ? returnType.GetGenericArguments().FirstOrDefault() : returnType;
|
||||
if (genericType == null)
|
||||
return;
|
||||
|
||||
var responseTypeAttributes = context.MethodInfo.GetCustomAttributes(typeof(ProducesResponseTypeAttribute), false)
|
||||
.Cast<ProducesResponseTypeAttribute>()
|
||||
.Where(attr => attr.StatusCode == 200)
|
||||
.ToList();
|
||||
|
||||
var contentType = "application/json";
|
||||
|
||||
if (context.MethodInfo.GetCustomAttributes(typeof(ProducesAttribute), false)
|
||||
.FirstOrDefault() is ProducesAttribute producesAttribute)
|
||||
contentType = producesAttribute.ContentTypes.FirstOrDefault() ?? "application/json";
|
||||
|
||||
if (responseTypeAttributes.Count != 0)
|
||||
{
|
||||
var responseType = responseTypeAttributes.First().Type;
|
||||
genericType = responseType;
|
||||
}
|
||||
|
||||
if (genericType.IsEquivalentTo(typeof(ContentResult)) || genericType.IsEquivalentTo(typeof(FileStreamResult)))
|
||||
{
|
||||
operation.Responses["200"] = new OpenApiResponse
|
||||
{
|
||||
Description = "OK",
|
||||
Content = new Dictionary<string, OpenApiMediaType>
|
||||
{
|
||||
[contentType] = new()
|
||||
}
|
||||
};
|
||||
}
|
||||
else if (genericType == typeof(ActionResult))
|
||||
{
|
||||
operation.Responses["200"] = new OpenApiResponse { Description = "OK" };
|
||||
}
|
||||
else
|
||||
{
|
||||
OpenApiSchema schema;
|
||||
if (genericType.IsGenericType && genericType.GetGenericTypeDefinition() == typeof(ActionResult<>))
|
||||
schema = context.SchemaGenerator.GenerateSchema(genericType.GetGenericArguments().FirstOrDefault(),
|
||||
context.SchemaRepository);
|
||||
else
|
||||
schema = context.SchemaGenerator.GenerateSchema(genericType, context.SchemaRepository);
|
||||
|
||||
operation.Responses["200"] = new OpenApiResponse
|
||||
{
|
||||
Description = "OK",
|
||||
Content = new Dictionary<string, OpenApiMediaType>
|
||||
{
|
||||
[contentType] = new() { Schema = schema }
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -48,7 +48,6 @@ public class AuthController(IOptionsSnapshot<Admin> user, IOptionsSnapshot<Gener
|
||||
|
||||
[HttpGet("OAuth2")]
|
||||
[BadRequestResponse]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
[Produces("text/html")]
|
||||
[MaintenanceModeIgnore]
|
||||
public async Task<ContentResult> OAuth2([FromQuery] string code, [FromQuery] string state)
|
||||
|
@ -40,7 +40,7 @@ public class ImportController(IMediator mediator, IOptionsSnapshot<GeneralConfig
|
||||
/// <returns>Excel file</returns>
|
||||
[HttpPost("ImportToExcel")]
|
||||
[Produces("application/vnd.ms-excel")]
|
||||
public async Task<IActionResult> ImportToExcel([FromBody] ScheduleRequest request)
|
||||
public async Task<FileStreamResult> ImportToExcel([FromBody] ScheduleRequest request)
|
||||
{
|
||||
var result = (await mediator.Send(new GetScheduleListQuery
|
||||
{
|
||||
@ -51,9 +51,6 @@ public class ImportController(IMediator mediator, IOptionsSnapshot<GeneralConfig
|
||||
ProfessorIds = request.Professors
|
||||
})).Schedules.ToList();
|
||||
|
||||
if (result.Count == 0)
|
||||
return NoContent();
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
using var package = new ExcelPackage();
|
||||
var worksheet = package.Workbook.Worksheets.Add("Расписание");
|
||||
|
@ -28,7 +28,7 @@ public class SecurityController(IOptionsSnapshot<GeneralConfig> generalConfig) :
|
||||
[HttpGet("GenerateTotpQrCode")]
|
||||
[Produces("image/svg+xml")]
|
||||
[MaintenanceModeIgnore]
|
||||
public IActionResult GenerateTotpQrCode(
|
||||
public ContentResult GenerateTotpQrCode(
|
||||
[FromQuery] string totpKey,
|
||||
[FromQuery] string label,
|
||||
[FromQuery] string? backgroundColor = null,
|
||||
|
Loading…
x
Reference in New Issue
Block a user