diff --git a/samples/WebApiSample/Startup.cs b/samples/WebApiSample/Startup.cs index 61753a9..3c299e7 100644 --- a/samples/WebApiSample/Startup.cs +++ b/samples/WebApiSample/Startup.cs @@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using NodaTime; diff --git a/samples/WebApiSample/WebApiSample.csproj b/samples/WebApiSample/WebApiSample.csproj index 0b123f5..62396fb 100644 --- a/samples/WebApiSample/WebApiSample.csproj +++ b/samples/WebApiSample/WebApiSample.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net8.0 @@ -9,7 +9,8 @@ - + + diff --git a/src/MicroElements.Swashbuckle.NodaTime/MicroElements.Swashbuckle.NodaTime.csproj b/src/MicroElements.Swashbuckle.NodaTime/MicroElements.Swashbuckle.NodaTime.csproj index 4a61f62..e6a45c3 100644 --- a/src/MicroElements.Swashbuckle.NodaTime/MicroElements.Swashbuckle.NodaTime.csproj +++ b/src/MicroElements.Swashbuckle.NodaTime/MicroElements.Swashbuckle.NodaTime.csproj @@ -2,7 +2,7 @@ - netstandard2.0 + net8.0 enable latest @@ -10,8 +10,12 @@ - - + + + + + + diff --git a/src/MicroElements.Swashbuckle.NodaTime/NamingPolicyParameterFilter.cs b/src/MicroElements.Swashbuckle.NodaTime/NamingPolicyParameterFilter.cs index 182e4cf..0ac1911 100644 --- a/src/MicroElements.Swashbuckle.NodaTime/NamingPolicyParameterFilter.cs +++ b/src/MicroElements.Swashbuckle.NodaTime/NamingPolicyParameterFilter.cs @@ -1,7 +1,7 @@ // Copyright (c) MicroElements. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using Swashbuckle.AspNetCore.SwaggerGen; namespace MicroElements.Swashbuckle.NodaTime @@ -23,9 +23,10 @@ public NamingPolicyParameterFilter(NodaTimeSchemaSettings nodaTimeSchemaSettings } /// - public void Apply(OpenApiParameter parameter, ParameterFilterContext context) + public void Apply(IOpenApiParameter parameter, ParameterFilterContext context) { - parameter.Name = _nodaTimeSchemaSettings.ResolvePropertyName(parameter.Name); + if (parameter is OpenApiParameter openApiParameter) + openApiParameter.Name = _nodaTimeSchemaSettings.ResolvePropertyName(parameter.Name); } } } diff --git a/src/MicroElements.Swashbuckle.NodaTime/NodaTimeSchemaSettingsFactory.cs b/src/MicroElements.Swashbuckle.NodaTime/NodaTimeSchemaSettingsFactory.cs index 0913363..12f0367 100644 --- a/src/MicroElements.Swashbuckle.NodaTime/NodaTimeSchemaSettingsFactory.cs +++ b/src/MicroElements.Swashbuckle.NodaTime/NodaTimeSchemaSettingsFactory.cs @@ -30,8 +30,6 @@ public static NodaTimeSchemaSettings CreateNodaTimeSchemaSettingsForNewtonsoftJs string FormatToJson(object value) { string formatToJson = JsonConvert.SerializeObject(value, serializerSettings); - if (formatToJson.StartsWith("\"") && formatToJson.EndsWith("\"")) - formatToJson = formatToJson.Substring(1, formatToJson.Length - 2); return formatToJson; } @@ -64,15 +62,7 @@ public static NodaTimeSchemaSettings CreateNodaTimeSchemaSettingsForSystemTextJs { string FormatToJson(object value) { - if (value is DateTimeZone dateTimeZone) - { - // TODO: remove after PR released: https://github.com/nodatime/nodatime.serialization/pull/57 - return dateTimeZone.Id; - } - string formatToJson = System.Text.Json.JsonSerializer.Serialize(value, jsonSerializerOptions); - if (formatToJson.StartsWith("\"") && formatToJson.EndsWith("\"")) - formatToJson = formatToJson.Substring(1, formatToJson.Length - 2); return formatToJson; } diff --git a/src/MicroElements.Swashbuckle.NodaTime/Schemas.cs b/src/MicroElements.Swashbuckle.NodaTime/Schemas.cs index f766751..5ca8729 100644 --- a/src/MicroElements.Swashbuckle.NodaTime/Schemas.cs +++ b/src/MicroElements.Swashbuckle.NodaTime/Schemas.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; namespace MicroElements.Swashbuckle.NodaTime { diff --git a/src/MicroElements.Swashbuckle.NodaTime/SchemasFactory.cs b/src/MicroElements.Swashbuckle.NodaTime/SchemasFactory.cs index a85acd8..95bf4b8 100644 --- a/src/MicroElements.Swashbuckle.NodaTime/SchemasFactory.cs +++ b/src/MicroElements.Swashbuckle.NodaTime/SchemasFactory.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; +using System.Text.Json.Nodes; +using Microsoft.OpenApi; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using NodaTime; @@ -45,8 +45,8 @@ public Schemas CreateSchemas() ZonedDateTime = () => StringSchema(examples.ZonedDateTime), Interval = () => new OpenApiSchema { - Type = "object", - Properties = new Dictionary + Type = JsonSchemaType.Object, + Properties = new Dictionary { { ResolvePropertyName(nameof(Interval.Start)), StringSchema(examples.Interval.Start, "date-time") }, { ResolvePropertyName(nameof(Interval.End)), StringSchema(examples.Interval.End, "date-time") }, @@ -54,8 +54,8 @@ public Schemas CreateSchemas() }, DateInterval = () => new OpenApiSchema { - Type = "object", - Properties = new Dictionary + Type = JsonSchemaType.Object, + Properties = new Dictionary { { ResolvePropertyName(nameof(DateInterval.Start)), StringSchema(examples.DateInterval.Start, "date") }, { ResolvePropertyName(nameof(DateInterval.End)), StringSchema(examples.DateInterval.End, "date") }, @@ -74,9 +74,9 @@ private OpenApiSchema StringSchema(object exampleObject, string? format = null) { return new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Example = _settings.ShouldGenerateExamples - ? new OpenApiString(FormatToJson(exampleObject)) + ? JsonNode.Parse(FormatToJson(exampleObject)) : null, Format = format }; diff --git a/test/MicroElements.Swashbuckle.NodaTime.Tests/MicroElements.Swashbuckle.NodaTime.Tests.csproj b/test/MicroElements.Swashbuckle.NodaTime.Tests/MicroElements.Swashbuckle.NodaTime.Tests.csproj index cdff988..e994632 100644 --- a/test/MicroElements.Swashbuckle.NodaTime.Tests/MicroElements.Swashbuckle.NodaTime.Tests.csproj +++ b/test/MicroElements.Swashbuckle.NodaTime.Tests/MicroElements.Swashbuckle.NodaTime.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net8.0 false diff --git a/test/MicroElements.Swashbuckle.NodaTime.Tests/SchemasTests.cs b/test/MicroElements.Swashbuckle.NodaTime.Tests/SchemasTests.cs index 1f550c8..1c2089b 100644 --- a/test/MicroElements.Swashbuckle.NodaTime.Tests/SchemasTests.cs +++ b/test/MicroElements.Swashbuckle.NodaTime.Tests/SchemasTests.cs @@ -1,8 +1,9 @@ using System; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Nodes; using FluentAssertions; -using Microsoft.OpenApi.Any; +using Microsoft.OpenApi; using Newtonsoft.Json; using NodaTime; using NodaTime.Serialization.JsonNet; @@ -46,59 +47,59 @@ private static void CheckGeneratedSchema(NodaTimeSchemaSettings nodaTimeSchemaSe { Schemas schemas = new SchemasFactory(nodaTimeSchemaSettings).CreateSchemas(); - schemas.Instant().Type.Should().Be("string"); + schemas.Instant().Type.Should().Be(JsonSchemaType.String); schemas.Instant().Format.Should().Be("date-time"); schemas.Instant().Example.AsString().Should().Be("2020-05-23T10:30:50Z"); - schemas.LocalDate().Type.Should().Be("string"); + schemas.LocalDate().Type.Should().Be(JsonSchemaType.String); schemas.LocalDate().Format.Should().Be("date"); schemas.LocalDate().Example.AsString().Should().Be("2020-05-23"); - schemas.LocalTime().Type.Should().Be("string"); + schemas.LocalTime().Type.Should().Be(JsonSchemaType.String); schemas.LocalTime().Format.Should().Be(null); schemas.LocalTime().Example.AsString().Should().Be("13:30:50"); - schemas.LocalDateTime().Type.Should().Be("string"); + schemas.LocalDateTime().Type.Should().Be(JsonSchemaType.String); schemas.LocalDateTime().Format.Should().Be(null); schemas.LocalDateTime().Example.AsString().Should().Be("2020-05-23T13:30:50"); - schemas.OffsetDateTime().Type.Should().Be("string"); + schemas.OffsetDateTime().Type.Should().Be(JsonSchemaType.String); schemas.OffsetDateTime().Format.Should().Be("date-time"); schemas.OffsetDateTime().Example.AsString().Should().Be("2020-05-23T13:30:50+03:00"); - schemas.ZonedDateTime().Type.Should().Be("string"); + schemas.ZonedDateTime().Type.Should().Be(JsonSchemaType.String); schemas.ZonedDateTime().Format.Should().Be(null); schemas.ZonedDateTime().Example.AsString().Should().Be("2020-05-23T13:30:50+03 Europe/Moscow"); - schemas.Interval().Type.Should().Be("object"); + schemas.Interval().Type.Should().Be(JsonSchemaType.Object); schemas.Interval().Properties["Start"].Example.AsString().Should().Be("2020-05-23T10:30:50Z"); schemas.Interval().Properties["End"].Example.AsString().Should().Be("2020-05-24T11:31:51.001Z"); - schemas.DateInterval().Type.Should().Be("object"); + schemas.DateInterval().Type.Should().Be(JsonSchemaType.Object); schemas.DateInterval().Properties["Start"].Example.AsString().Should().Be("2020-05-23"); schemas.DateInterval().Properties["End"].Example.AsString().Should().Be("2020-05-24"); - schemas.Offset().Type.Should().Be("string"); + schemas.Offset().Type.Should().Be(JsonSchemaType.String); schemas.Offset().Format.Should().Be(null); schemas.Offset().Example.AsString().Should().Be("+03"); - schemas.Period().Type.Should().Be("string"); + schemas.Period().Type.Should().Be(JsonSchemaType.String); schemas.Period().Format.Should().Be(null); schemas.Period().Example.AsString().Should().Be("P1DT1H1M1S1s"); - schemas.Duration().Type.Should().Be("string"); + schemas.Duration().Type.Should().Be(JsonSchemaType.String); schemas.Duration().Format.Should().Be(null); schemas.Duration().Example.AsString().Should().Be("25:01:01.001"); - schemas.OffsetDate().Type.Should().Be("string"); + schemas.OffsetDate().Type.Should().Be(JsonSchemaType.String); schemas.OffsetDate().Format.Should().Be(null); schemas.OffsetDate().Example.AsString().Should().Be("2020-05-23+03"); - schemas.OffsetTime().Type.Should().Be("string"); + schemas.OffsetTime().Type.Should().Be(JsonSchemaType.String); schemas.OffsetTime().Format.Should().Be(null); schemas.OffsetTime().Example.AsString().Should().Be("13:30:50+03"); - schemas.DateTimeZone().Type.Should().Be("string"); + schemas.DateTimeZone().Type.Should().Be(JsonSchemaType.String); schemas.DateTimeZone().Format.Should().Be(null); schemas.DateTimeZone().Example.AsString().Should().Be("Europe/Moscow"); } @@ -106,11 +107,8 @@ private static void CheckGeneratedSchema(NodaTimeSchemaSettings nodaTimeSchemaSe internal static class TestExtensions { - public static string AsString(this IOpenApiAny openApiAny) + public static string AsString(this JsonNode openApiAny) { - if (openApiAny is OpenApiString openApiString) - return openApiString.Value; - return openApiAny.ToString(); } }