Skip to content

Commit

Permalink
[DMS-377] Loosen date query parameter validation, ignore time portion…
Browse files Browse the repository at this point in the history
… on Date queries (#329)
  • Loading branch information
simpat-adam authored Nov 1, 2024
1 parent a3d6db5 commit 288c4ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,34 +167,38 @@ public async Task Execute(PipelineContext context, Func<Task> next)

string jsonPathString = queryElementAndType.DocumentPathsAndTypes[0].JsonPathString;
string queryFieldName = queryElementAndType.QueryFieldName;
object queryFieldValue = queryElementAndType.Value;
string queryFieldValue = queryElementAndType.Value;

switch (queryElementAndType.DocumentPathsAndTypes[0].Type)
{
case "boolean":
if (!bool.TryParse(queryFieldValue?.ToString(), out _))
if (!bool.TryParse(queryFieldValue, out _))
{
AddValidationError(validationErrors, jsonPathString, queryFieldValue!, queryFieldName);
AddValidationError(validationErrors, jsonPathString, queryFieldValue, queryFieldName);
}
break;
case "date":
if (
!DateTime.TryParseExact(
queryFieldValue.ToString(),
"yyyy-MM-dd",
DateTime.TryParse(
queryFieldValue,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out _
out var dateTime
)
)
{
// query parameter was valid but ensure we only pass the date portion downstream to queries
queryFieldValue = dateTime.ToString("yyyy-MM-dd");
}
else
{
AddValidationError(validationErrors, jsonPathString, queryFieldValue, queryFieldName);
}
break;
case "date-time":
if (
!DateTime.TryParse(
queryFieldValue.ToString(),
queryFieldValue,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out _
Expand All @@ -206,7 +210,7 @@ out _
break;

case "number":
if (!decimal.TryParse(queryFieldValue.ToString(), out _))
if (!decimal.TryParse(queryFieldValue, out _))
{
AddValidationError(validationErrors, jsonPathString, queryFieldValue, queryFieldName);
}
Expand All @@ -222,7 +226,7 @@ out _
case "time":
if (
!DateTime.TryParseExact(
queryFieldValue.ToString(),
queryFieldValue,
"HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
Expand All @@ -246,7 +250,7 @@ out _
queryElementAndType
.DocumentPathsAndTypes.Select(x => new JsonPath(x.JsonPathString))
.ToArray(),
queryElementAndType.Value
queryFieldValue
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,27 @@ Feature: Query String handling for GET requests for Resource Queries
}]
"""

@API-124
Scenario: 01.1 Ensure clients can GET information when querying by valid datetime ignoring time
When a GET request is made to "/ed-fi/academicWeeks?beginDate=2024-05-15T17:30:00.000000Z"
Then it should respond with 200
And the response body is
"""
[{
"id": "{id}",
"schoolReference": {
"schoolId": 2
},
"weekIdentifier": "Week One",
"beginDate": "2024-05-15",
"endDate": "2024-05-22",
"totalInstructionalDays": 2
}]
"""

@API-125
Scenario: 02 Ensure clients can't GET information when querying by invalid date
When a GET request is made to "/ed-fi/academicWeeks?beginDate=024-04-09"
When a GET request is made to "/ed-fi/academicWeeks?beginDate=099-99-09"
Then it should respond with 400
And the response body is
"""
Expand All @@ -40,7 +58,7 @@ Feature: Query String handling for GET requests for Resource Queries
"status": 400,
"correlationId": null,
"validationErrors": {
"$.beginDate": ["The value '024-04-09' is not valid for beginDate."]
"$.beginDate": ["The value '099-99-09' is not valid for beginDate."]
},
"errors": []
}
Expand Down

0 comments on commit 288c4ee

Please sign in to comment.