Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb59864c

Browse files
committed
fix: an issue where deprecation extension parsing would fail
1 parente039532 commitb59864c

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

‎src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiDeprecationExtension.cs‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
usingMicrosoft.OpenApi.Interfaces;
1010
usingMicrosoft.OpenApi.Writers;
1111
usingSystem.Text.Json.Nodes;
12+
usingSystem.Text.Json;
13+
usingSystem.Globalization;
1214

1315
namespaceMicrosoft.OpenApi.MicrosoftExtensions;
1416

@@ -71,6 +73,35 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
7173
writer.WriteEndObject();
7274
}
7375
}
76+
privatestaticreadonlyDateTimeStylesdatesStyle=DateTimeStyles.AssumeUniversal|DateTimeStyles.RoundtripKind;
77+
privatestaticDateTimeOffset?GetDateTimeOffsetValue(stringpropertyName,JsonObjectrawObject)
78+
{
79+
if(!rawObject.TryGetPropertyValue(propertyName.ToFirstCharacterLowerCase(),outvarjsonNode)||
80+
jsonNodeis notJsonValuejsonValue||
81+
jsonNode.GetValueKind()is notJsonValueKind.String)
82+
returnnull;
83+
84+
if(jsonValue.TryGetValue<string>(outvarstrValue)&&
85+
DateTimeOffset.TryParse(strValue,CultureInfo.InvariantCulture,datesStyle,outvarparsedValue))
86+
{
87+
returnparsedValue;
88+
}
89+
if(jsonValue.TryGetValue<DateTimeOffset>(outvarreturnedDto))
90+
{
91+
returnreturnedDto;
92+
}
93+
if(jsonValue.TryGetValue<DateTime>(outvarreturnedDt))
94+
{
95+
returnnewDateTimeOffset(returnedDt,TimeSpan.FromHours(0));
96+
}
97+
#ifNET6_0_OR_GREATER
98+
if(jsonValue.TryGetValue<DateOnly>(outvarreturnedDo))
99+
{
100+
returnnew(returnedDo.Year,returnedDo.Month,returnedDo.Day,0,0,0,TimeSpan.FromHours(0));
101+
}
102+
#endif
103+
returnnull;
104+
}
74105
/// <summary>
75106
/// Parses the <see cref="OpenApiAny"/> to <see cref="OpenApiDeprecationExtension"/>.
76107
/// </summary>
@@ -80,11 +111,11 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
80111
publicstaticOpenApiDeprecationExtensionParse(JsonNodesource)
81112
{
82113
if(sourceis notJsonObjectrawObject)returnnull;
83-
varextension=newOpenApiDeprecationExtension();
84-
if(rawObject.TryGetPropertyValue(nameof(RemovalDate).ToFirstCharacterLowerCase(),outvarremovalDate)&&removalDateisJsonNoderemovalDateValue)
85-
extension.RemovalDate=removalDateValue.GetValue<DateTimeOffset>();
86-
if(rawObject.TryGetPropertyValue(nameof(Date).ToFirstCharacterLowerCase(),outvardate)&&dateisJsonNodedateValue)
87-
extension.Date=dateValue.GetValue<DateTimeOffset>();
114+
varextension=newOpenApiDeprecationExtension
115+
{
116+
RemovalDate=GetDateTimeOffsetValue(nameof(RemovalDate),rawObject),
117+
Date=GetDateTimeOffsetValue(nameof(Date),rawObject)
118+
};
88119
if(rawObject.TryGetPropertyValue(nameof(Version).ToFirstCharacterLowerCase(),outvarversion)&&versionisJsonNodeversionValue)
89120
extension.Version=versionValue.GetValue<string>();
90121
if(rawObject.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(),outvardescription)&&descriptionisJsonNodedescriptionValue)

‎test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiDeprecationExtensionTests.cs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public void Parses()
7575
{
7676
varoaiValue=newJsonObject
7777
{
78-
{"date",newOpenApiAny(newDateTimeOffset(2023,05,04,16,0,0,0,0,new(4,0,0))).Node},
79-
{"removalDate",newOpenApiAny(newDateTimeOffset(2023,05,04,16,0,0,0,0,new(4,0,0))).Node},
80-
{"version",newOpenApiAny("v1.0").Node},
81-
{"description",newOpenApiAny("removing").Node}
78+
{"date",newDateTimeOffset(2023,05,04,16,0,0,0,0,new(4,0,0))},
79+
{"removalDate",newDateTimeOffset(2023,05,04,16,0,0,0,0,new(4,0,0))},
80+
{"version","v1.0"},
81+
{"description","removing"}
8282
};
8383
varvalue=OpenApiDeprecationExtension.Parse(oaiValue);
8484
Assert.NotNull(value);
@@ -88,6 +88,23 @@ public void Parses()
8888
Assert.Equal(newDateTimeOffset(2023,05,04,16,0,0,0,0,new(4,0,0)),value.RemovalDate);
8989
}
9090
[Fact]
91+
publicvoidParsesStringValues()
92+
{
93+
varoaiValue=newJsonObject
94+
{
95+
{"date","2023-05-04T16:00:00Z"},
96+
{"removalDate","2023-05-04"},
97+
{"version","v1.0"},
98+
{"description","removing"}
99+
};
100+
varvalue=OpenApiDeprecationExtension.Parse(oaiValue);
101+
Assert.NotNull(value);
102+
Assert.Equal("v1.0",value.Version);
103+
Assert.Equal("removing",value.Description);
104+
Assert.Equal(newDateTimeOffset(2023,05,04,16,0,0,0,0,new(0,0,0)),value.Date);
105+
Assert.Equal(newDateTimeOffset(2023,05,04,0,0,0,0,0,new(0,0,0)),value.RemovalDate);
106+
}
107+
[Fact]
91108
publicvoidSerializes()
92109
{
93110
varvalue=newOpenApiDeprecationExtension

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp