Skip to content

Commit e2f8da0

Browse files
Guessing datetime offset type from empty string
1 parent 2c7663a commit e2f8da0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,24 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
5050
return newObject;
5151
}
5252

53-
if (!(openApiAny is OpenApiString) || ((OpenApiString)openApiAny).IsExplicit())
53+
if (!(openApiAny is OpenApiString))
5454
{
5555
return openApiAny;
5656
}
5757

5858
var value = ((OpenApiString)openApiAny).Value;
59+
60+
// For explicit strings only try to guess if it's a DateTimeOffset
61+
if (((OpenApiString)openApiAny).IsExplicit())
62+
{
63+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
64+
{
65+
return new OpenApiDateTime(dateTimeValue);
66+
}
67+
68+
return openApiAny;
69+
}
70+
5971
if (value == null || value == "null")
6072
{
6173
return new OpenApiNull();

test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiExample/explicitString.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ components:
2020
properties:
2121
sub:
2222
$ref: '#/components/schemas/test-sub-schema'
23-
required:
24-
- origin
2523
example:
2624
sub:
27-
test-property: "12345"
25+
test-property1: "12345"
26+
test-property2: "1970-01-01T00:00:00Z"
2827
test-sub-schema:
2928
type: object
3029
properties:
31-
test-property:
30+
test-property1:
3231
type: string
33-
example: "12345"
32+
example: "12345"
33+
test-property2:
34+
type: string
35+
format: date-time
36+
example: "1970-01-01T00:00:00Z"

0 commit comments

Comments
 (0)