Skip to content

Commit

Permalink
fix: an issue where deprecation extension parsing would fail
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Feb 18, 2025
1 parent e039532 commit b59864c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Globalization;

namespace Microsoft.OpenApi.MicrosoftExtensions;

Expand Down Expand Up @@ -71,6 +73,35 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
writer.WriteEndObject();
}
}
private static readonly DateTimeStyles datesStyle = DateTimeStyles.AssumeUniversal | DateTimeStyles.RoundtripKind;
private static DateTimeOffset? GetDateTimeOffsetValue(string propertyName, JsonObject rawObject)
{
if (!rawObject.TryGetPropertyValue(propertyName.ToFirstCharacterLowerCase(), out var jsonNode) ||
jsonNode is not JsonValue jsonValue ||
jsonNode.GetValueKind() is not JsonValueKind.String)
return null;

if (jsonValue.TryGetValue<string>(out var strValue) &&
DateTimeOffset.TryParse(strValue, CultureInfo.InvariantCulture, datesStyle, out var parsedValue))
{
return parsedValue;
}
if (jsonValue.TryGetValue<DateTimeOffset>(out var returnedDto))
{
return returnedDto;
}
if (jsonValue.TryGetValue<DateTime>(out var returnedDt))
{
return new DateTimeOffset(returnedDt, TimeSpan.FromHours(0));
}
#if NET6_0_OR_GREATER
if (jsonValue.TryGetValue<DateOnly>(out var returnedDo))
{
return new(returnedDo.Year, returnedDo.Month, returnedDo.Day, 0, 0, 0, TimeSpan.FromHours(0));
}
#endif
return null;
}
/// <summary>
/// Parses the <see cref="OpenApiAny"/> to <see cref="OpenApiDeprecationExtension"/>.
/// </summary>
Expand All @@ -80,11 +111,11 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
public static OpenApiDeprecationExtension Parse(JsonNode source)
{
if (source is not JsonObject rawObject) return null;
var extension = new OpenApiDeprecationExtension();
if (rawObject.TryGetPropertyValue(nameof(RemovalDate).ToFirstCharacterLowerCase(), out var removalDate) && removalDate is JsonNode removalDateValue)
extension.RemovalDate = removalDateValue.GetValue<DateTimeOffset>();
if (rawObject.TryGetPropertyValue(nameof(Date).ToFirstCharacterLowerCase(), out var date) && date is JsonNode dateValue)
extension.Date = dateValue.GetValue<DateTimeOffset>();
var extension = new OpenApiDeprecationExtension
{
RemovalDate = GetDateTimeOffsetValue(nameof(RemovalDate), rawObject),
Date = GetDateTimeOffsetValue(nameof(Date), rawObject)
};
if (rawObject.TryGetPropertyValue(nameof(Version).ToFirstCharacterLowerCase(), out var version) && version is JsonNode versionValue)
extension.Version = versionValue.GetValue<string>();
if (rawObject.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var description) && description is JsonNode descriptionValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public void Parses()
{
var oaiValue = new JsonObject
{
{ "date", new OpenApiAny(new DateTimeOffset(2023,05,04, 16, 0, 0, 0, 0, new(4, 0, 0))).Node},
{ "removalDate", new OpenApiAny(new DateTimeOffset(2023,05,04, 16, 0, 0, 0, 0, new(4, 0, 0))).Node},
{ "version", new OpenApiAny("v1.0").Node},
{ "description", new OpenApiAny("removing").Node}
{ "date", new DateTimeOffset(2023,05,04, 16, 0, 0, 0, 0, new(4, 0, 0))},
{ "removalDate", new DateTimeOffset(2023,05,04, 16, 0, 0, 0, 0, new(4, 0, 0))},
{ "version", "v1.0"},
{ "description", "removing"}
};
var value = OpenApiDeprecationExtension.Parse(oaiValue);
Assert.NotNull(value);
Expand All @@ -88,6 +88,23 @@ public void Parses()
Assert.Equal(new DateTimeOffset(2023, 05, 04, 16, 0, 0, 0, 0, new(4, 0, 0)), value.RemovalDate);
}
[Fact]
public void ParsesStringValues()
{
var oaiValue = new JsonObject
{
{ "date", "2023-05-04T16:00:00Z"},
{ "removalDate", "2023-05-04"},
{ "version", "v1.0"},
{ "description", "removing"}
};
var value = OpenApiDeprecationExtension.Parse(oaiValue);
Assert.NotNull(value);
Assert.Equal("v1.0", value.Version);
Assert.Equal("removing", value.Description);
Assert.Equal(new DateTimeOffset(2023, 05, 04, 16, 0, 0, 0, 0, new(0, 0, 0)), value.Date);
Assert.Equal(new DateTimeOffset(2023, 05, 04, 0, 0, 0, 0, 0, new(0, 0, 0)), value.RemovalDate);
}
[Fact]
public void Serializes()
{
var value = new OpenApiDeprecationExtension
Expand Down

0 comments on commit b59864c

Please sign in to comment.