Skip to content

Commit b8803e4

Browse files
committed
Return proper response for empty parameter name
1 parent 4427187 commit b8803e4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/JsonApiDotNetCore/QueryStrings/QueryStringReader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public void ReadAll(DisableQueryStringAttribute? disableQueryStringAttribute)
3838

3939
foreach ((string parameterName, StringValues parameterValue) in _queryStringAccessor.Query)
4040
{
41+
if (parameterName.Length == 0)
42+
{
43+
throw new InvalidQueryException("Empty query string parameter name.", null);
44+
}
45+
4146
IQueryStringParameterReader? reader = _parameterReaders.FirstOrDefault(nextReader => nextReader.CanRead(parameterName));
4247

4348
if (reader != null)

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/QueryStringTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ public async Task Can_use_unknown_query_string_parameter()
6363
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
6464
}
6565

66+
[Fact]
67+
public async Task Cannot_use_empty_query_string_parameter_name()
68+
{
69+
// Arrange
70+
var options = (JsonApiOptions)_testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
71+
options.AllowUnknownQueryStringParameters = false;
72+
73+
const string route = "calendars?=";
74+
75+
// Act
76+
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
77+
78+
// Assert
79+
httpResponse.ShouldHaveStatusCode(HttpStatusCode.BadRequest);
80+
81+
responseDocument.Errors.ShouldHaveCount(1);
82+
83+
ErrorObject error = responseDocument.Errors[0];
84+
error.StatusCode.Should().Be(HttpStatusCode.BadRequest);
85+
error.Title.Should().Be("Empty query string parameter name.");
86+
error.Detail.Should().BeNull();
87+
error.Source.Should().BeNull();
88+
}
89+
6690
[Theory]
6791
[InlineData("filter")]
6892
[InlineData("sort")]

0 commit comments

Comments
 (0)