Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/WebApiSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions samples/WebApiSample/WebApiSample.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />

<PackageReference Include="NodaTime" Version="3.0.3" />
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<Import Project="..\..\common.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NodaTime" Version="3.0.0" />
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.0" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.0.0" />
</ItemGroup>
<ItemGroup>

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,9 +23,10 @@ public NamingPolicyParameterFilter(NodaTimeSchemaSettings nodaTimeSchemaSettings
}

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

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MicroElements.Swashbuckle.NodaTime/Schemas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
16 changes: 8 additions & 8 deletions src/MicroElements.Swashbuckle.NodaTime/SchemasFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,17 +45,17 @@ public Schemas CreateSchemas()
ZonedDateTime = () => StringSchema(examples.ZonedDateTime),
Interval = () => new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>
{
{ ResolvePropertyName(nameof(Interval.Start)), StringSchema(examples.Interval.Start, "date-time") },
{ ResolvePropertyName(nameof(Interval.End)), StringSchema(examples.Interval.End, "date-time") },
},
},
DateInterval = () => new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>
{
{ ResolvePropertyName(nameof(DateInterval.Start)), StringSchema(examples.DateInterval.Start, "date") },
{ ResolvePropertyName(nameof(DateInterval.End)), StringSchema(examples.DateInterval.End, "date") },
Expand All @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
36 changes: 17 additions & 19 deletions test/MicroElements.Swashbuckle.NodaTime.Tests/SchemasTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,71 +47,68 @@ 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");
}
}

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();
}
}
Expand Down