Skip to content

Commit

Permalink
Serialize OpenApiDate values properly to short date (#1102)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
irvinesunday authored and Afsalmc committed Dec 14, 2022
1 parent ad3d267 commit 32d621e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)

case PrimitiveType.Date:
var dateValue = (OpenApiDate)(IOpenApiPrimitive)this;
writer.WriteValue(dateValue.Value);
writer.WriteValue(dateValue.Value.ToShortDateString());
break;

case PrimitiveType.DateTime:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}
]
}
]
],
"aDate": "12/12/2022"
}
}
Original file line number Diff line number Diff line change
@@ -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"}]}]}}
{"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"}}
4 changes: 3 additions & 1 deletion test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -95,7 +96,8 @@ public class OpenApiExampleTests
}
}
}
}
},
["aDate"] = new OpenApiDate(DateTime.Parse("12/12/2022 00:00:00"))
}
};

Expand Down

0 comments on commit 32d621e

Please sign in to comment.