Skip to content

Commit

Permalink
Merge pull request #1530 from microsoft/mk/fix-data-and-type-mismatch
Browse files Browse the repository at this point in the history
Fix data and type mismatch warning for an example object with a date value
  • Loading branch information
MaggieKimani1 authored Jan 16, 2024
2 parents 31f39c4 + ad57d5c commit efb6831
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit efb6831

Please sign in to comment.