From 16e9c7abe0b50c1c7fbf9b7aff934a403ad1db9b Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 16 Jan 2024 12:58:11 +0300 Subject: [PATCH 1/3] Use the TimeOfDay property of a date value to determine whether its a date-only or date-time property --- .../ParseNodes/OpenApiAnyConverter.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs index 4e7e1d08d..e055072e0 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs @@ -63,10 +63,12 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS { // More narrow type detection for explicit strings, only check types that are passed as strings if (schema == null) - { + { if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue)) { - return new OpenApiDateTime(dateTimeValue); + // if the time component is exactly midnight(00:00:00) meaning no time has elapsed, return a date-only value + return dateTimeValue.TimeOfDay == TimeSpan.Zero ? new OpenApiDate(dateTimeValue.Date) + : new OpenApiDateTime(dateTimeValue); } } else if (type == "string") From 230a17bb134f68e6df18f6e3f13c23c9b13d3a5e Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 16 Jan 2024 12:58:39 +0300 Subject: [PATCH 2/3] Add a test to validate --- .../V3Tests/OpenApiDocumentTests.cs | 17 ++++++++ .../documentWithDateExampleInSchema.yaml | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/documentWithDateExampleInSchema.yaml diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index 1f86ab895..a9401897b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs @@ -1338,5 +1338,22 @@ public void ParseDocumentWithReferencedSecuritySchemeWorks() Assert.False(securityScheme.UnresolvedReference); Assert.NotNull(securityScheme.Flows); } + + [Fact] + public void ValidateExampleShouldNotHaveDataTypeMismatch() + { + // Arrange + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "documentWithDateExampleInSchema.yaml")); + + // Act + var doc = new OpenApiStreamReader(new() + { + ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences + }).Read(stream, out var diagnostic); + + // Assert + var warnings = diagnostic.Warnings; + Assert.False(warnings.Any()); + } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/documentWithDateExampleInSchema.yaml b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/documentWithDateExampleInSchema.yaml new file mode 100644 index 000000000..ad8c525cd --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiDocument/documentWithDateExampleInSchema.yaml @@ -0,0 +1,42 @@ +openapi: 3.0.0 +info: + title: Sample API + description: Lorem Ipsum + version: 1.0.0 +servers: + - url: http://api.example.com/v1 + description: Lorem Ipsum +paths: + /issues: + get: + summary: Returns a list of issues. + description: Lorem Ipsum + responses: + "200": + description: Lorem Ipsum + content: + application/json: + schema: + type: object + required: + - data + properties: + data: + type: array + items: + $ref: "#/components/schemas/issueData" + example: + data: + - issuedAt: "2023-10-12" +components: + schemas: + issueData: + type: object + title: Issue Data + description: Information about the issue. + properties: + issuedAt: + type: string + format: date + description: Lorem Ipsum + example: "2023-10-12" \ No newline at end of file From ad57d5c620c0dd208c62c0ed0ea5777e2133d325 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 16 Jan 2024 13:02:05 +0300 Subject: [PATCH 3/3] Fix formatting --- src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs index e055072e0..6aefbf947 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs @@ -63,7 +63,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS { // More narrow type detection for explicit strings, only check types that are passed as strings if (schema == null) - { + { if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue)) { // if the time component is exactly midnight(00:00:00) meaning no time has elapsed, return a date-only value