Skip to content

Commit

Permalink
Merge pull request #96 from BigDaddy1337/fix/CSHARP-168-swagger-timespan
Browse files Browse the repository at this point in the history
fix(autodoc): parse TimeSpan as string type
  • Loading branch information
OptimumDev authored Mar 7, 2024
2 parents 258d4d8 + 29f2430 commit 8fc1f62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Tochka.JsonRpc.Swagger/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TimeSpan>(() => new OpenApiSchema
{
Type = "string",
Example = new OpenApiString("00:00:00")
});
});

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,4 +59,24 @@ public async Task GetUi_Returns200()

response.StatusCode.Should().Be(HttpStatusCode.OK);
}


[Test]
public async Task TimeSpan_ParsingAsString()
{
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);

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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 8fc1f62

Please sign in to comment.