From 730280f1ec95d2c01d0d8fddba8df62885546ff0 Mon Sep 17 00:00:00 2001 From: Sergey Bessonov Date: Wed, 6 Mar 2024 17:37:18 +0500 Subject: [PATCH 1/2] fix(autodoc): parse TimeSpan as string type --- src/Tochka.JsonRpc.Swagger/Extensions.cs | 8 ++++++ .../IntegrationTests.cs | 27 +++++++++++++++++++ .../CustomGroupNameJsonRpcController.cs | 7 +++++ 3 files changed, 42 insertions(+) diff --git a/src/Tochka.JsonRpc.Swagger/Extensions.cs b/src/Tochka.JsonRpc.Swagger/Extensions.cs index 577de8bf..c7850887 100644 --- a/src/Tochka.JsonRpc.Swagger/Extensions.cs +++ b/src/Tochka.JsonRpc.Swagger/Extensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerUI; @@ -64,6 +65,13 @@ public static IServiceCollection AddSwaggerWithJsonRpc(this IServiceCollection s c.IncludeXmlComments(xmlPath); c.SupportNonNullableReferenceTypes(); + + // https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2505 + c.MapType(() => new OpenApiSchema + { + Type = "string", + Example = new OpenApiString("00:00:00") + }); }); return services; diff --git a/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs b/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs index 02acc5e0..5b0accb0 100644 --- a/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs +++ b/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using FluentAssertions; using NUnit.Framework; +using Tochka.JsonRpc.Tests.WebApplication.Controllers; using Tochka.JsonRpc.TestUtils.Integration; namespace Tochka.JsonRpc.Swagger.Tests.Integration; @@ -58,4 +59,30 @@ public async Task GetUi_Returns200() response.StatusCode.Should().Be(HttpStatusCode.OK); } + + + [Test] + public async Task ObectTypes_Parsing() + { + var response = await ApiClient.GetAsync("/swagger/custom_v1/swagger.json"); + + response.StatusCode.Should().Be(HttpStatusCode.OK); + var responseContent = await response.Content.ReadAsStringAsync(); + var responseJson = JsonDocument.Parse(responseContent); + + var propertiesJson = responseJson.RootElement.GetProperty("components") + .GetProperty("schemas") + .GetProperty(nameof(TestObject)) + .GetProperty("properties"); + + EnsureObjectType(nameof(TestObject.Ts), "string"); + return; + + void EnsureObjectType(string field, string swaggerType) + { + propertiesJson.GetProperty(field.ToLower()) + .TryGetProperty("type", out var typePropertyJson).Should().BeTrue(); + typePropertyJson.GetString().Should().Be(swaggerType); + } + } } diff --git a/src/tests/Tochka.JsonRpc.Tests.WebApplication/Controllers/CustomGroupNameJsonRpcController.cs b/src/tests/Tochka.JsonRpc.Tests.WebApplication/Controllers/CustomGroupNameJsonRpcController.cs index 09387bfc..fe42eb1f 100644 --- a/src/tests/Tochka.JsonRpc.Tests.WebApplication/Controllers/CustomGroupNameJsonRpcController.cs +++ b/src/tests/Tochka.JsonRpc.Tests.WebApplication/Controllers/CustomGroupNameJsonRpcController.cs @@ -3,8 +3,15 @@ namespace Tochka.JsonRpc.Tests.WebApplication.Controllers; +public class TestObject +{ + public TimeSpan Ts { get; set; } = DateTime.Now.TimeOfDay; +} + [ApiExplorerSettings(GroupName = "custom")] public class CustomGroupNameJsonRpcController : JsonRpcControllerBase { public bool CustomGroup() => true; + + public TestObject TestObjectTypes() => new(); } From 29f2430f8534819de702d8c5357f2815ce6e99c7 Mon Sep 17 00:00:00 2001 From: Sergey Bessonov Date: Thu, 7 Mar 2024 14:11:51 +0500 Subject: [PATCH 2/2] fix test --- .../IntegrationTests.cs | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs b/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs index 5b0accb0..b3c1d5f0 100644 --- a/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs +++ b/src/tests/Tochka.JsonRpc.Swagger.Tests.Integration/IntegrationTests.cs @@ -62,7 +62,7 @@ public async Task GetUi_Returns200() [Test] - public async Task ObectTypes_Parsing() + public async Task TimeSpan_ParsingAsString() { var response = await ApiClient.GetAsync("/swagger/custom_v1/swagger.json"); @@ -70,19 +70,13 @@ public async Task ObectTypes_Parsing() var responseContent = await response.Content.ReadAsStringAsync(); var responseJson = JsonDocument.Parse(responseContent); - var propertiesJson = responseJson.RootElement.GetProperty("components") - .GetProperty("schemas") - .GetProperty(nameof(TestObject)) - .GetProperty("properties"); - - EnsureObjectType(nameof(TestObject.Ts), "string"); - return; - - void EnsureObjectType(string field, string swaggerType) - { - propertiesJson.GetProperty(field.ToLower()) - .TryGetProperty("type", out var typePropertyJson).Should().BeTrue(); - typePropertyJson.GetString().Should().Be(swaggerType); - } + responseJson.RootElement.GetProperty("components") + .GetProperty("schemas") + .GetProperty(nameof(TestObject)) + .GetProperty("properties") + .GetProperty(nameof(TestObject.Ts).ToLower()) + .TryGetProperty("type", out var typePropertyJson).Should().BeTrue(); + + typePropertyJson.GetString().Should().Be("string"); } }