Skip to content

Commit 32d621e

Browse files
irvinesundayAfsalmc
authored andcommitted
Serialize OpenApiDate values properly to short date (#1102)
* Serialize OpenApiDate properly as Date * Change test date value Using value 01/01/0001 could be represented as 1/1/0001 in a machine using a different regional format, and thus cause the test to fail
1 parent ad3d267 commit 32d621e

4 files changed

+7
-4
lines changed

src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
118118

119119
case PrimitiveType.Date:
120120
var dateValue = (OpenApiDate)(IOpenApiPrimitive)this;
121-
writer.WriteValue(dateValue.Value);
121+
writer.WriteValue(dateValue.Value.ToShortDateString());
122122
break;
123123

124124
case PrimitiveType.DateTime:

test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.SerializeReferencedExampleAsV3JsonWithoutReferenceWorks_produceTerseOutput=False.verified.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222
]
2323
}
24-
]
24+
],
25+
"aDate": "12/12/2022"
2526
}
2627
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"value":{"versions":[{"status":"Status1","id":"v1","links":[{"href":"http://example.com/1","rel":"sampleRel1"}]},{"status":"Status2","id":"v2","links":[{"href":"http://example.com/2","rel":"sampleRel2"}]}]}}
1+
{"value":{"versions":[{"status":"Status1","id":"v1","links":[{"href":"http://example.com/1","rel":"sampleRel1"}]},{"status":"Status2","id":"v2","links":[{"href":"http://example.com/2","rel":"sampleRel2"}]}],"aDate":"12/12/2022"}}

test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Globalization;
56
using System.IO;
67
using System.Text;
@@ -95,7 +96,8 @@ public class OpenApiExampleTests
9596
}
9697
}
9798
}
98-
}
99+
},
100+
["aDate"] = new OpenApiDate(DateTime.Parse("12/12/2022 00:00:00"))
99101
}
100102
};
101103

0 commit comments

Comments
 (0)