Skip to content

Commit efb6831

Browse files
Merge pull request #1530 from microsoft/mk/fix-data-and-type-mismatch
Fix data and type mismatch warning for an example object with a date value
2 parents 31f39c4 + ad57d5c commit efb6831

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
6666
{
6767
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
6868
{
69-
return new OpenApiDateTime(dateTimeValue);
69+
// if the time component is exactly midnight(00:00:00) meaning no time has elapsed, return a date-only value
70+
return dateTimeValue.TimeOfDay == TimeSpan.Zero ? new OpenApiDate(dateTimeValue.Date)
71+
: new OpenApiDateTime(dateTimeValue);
7072
}
7173
}
7274
else if (type == "string")

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,5 +1338,22 @@ public void ParseDocumentWithReferencedSecuritySchemeWorks()
13381338
Assert.False(securityScheme.UnresolvedReference);
13391339
Assert.NotNull(securityScheme.Flows);
13401340
}
1341+
1342+
[Fact]
1343+
public void ValidateExampleShouldNotHaveDataTypeMismatch()
1344+
{
1345+
// Arrange
1346+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "documentWithDateExampleInSchema.yaml"));
1347+
1348+
// Act
1349+
var doc = new OpenApiStreamReader(new()
1350+
{
1351+
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences
1352+
}).Read(stream, out var diagnostic);
1353+
1354+
// Assert
1355+
var warnings = diagnostic.Warnings;
1356+
Assert.False(warnings.Any());
1357+
}
13411358
}
13421359
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: Lorem Ipsum
5+
version: 1.0.0
6+
servers:
7+
- url: http://api.example.com/v1
8+
description: Lorem Ipsum
9+
paths:
10+
/issues:
11+
get:
12+
summary: Returns a list of issues.
13+
description: Lorem Ipsum
14+
responses:
15+
"200":
16+
description: Lorem Ipsum
17+
content:
18+
application/json:
19+
schema:
20+
type: object
21+
required:
22+
- data
23+
properties:
24+
data:
25+
type: array
26+
items:
27+
$ref: "#/components/schemas/issueData"
28+
example:
29+
data:
30+
- issuedAt: "2023-10-12"
31+
components:
32+
schemas:
33+
issueData:
34+
type: object
35+
title: Issue Data
36+
description: Information about the issue.
37+
properties:
38+
issuedAt:
39+
type: string
40+
format: date
41+
description: Lorem Ipsum
42+
example: "2023-10-12"

0 commit comments

Comments
 (0)