diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs index 4e7e1d08d..6aefbf947 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs @@ -66,7 +66,9 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS { 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") 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