Skip to content

Commit d710b4b

Browse files
author
Bart Koelman
committed
Added missing test for error case
1 parent 61befc4 commit d710b4b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/JsonApiDotNetCore/QueryParameterServices/SparseFieldsService.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ private void RegisterRelatedResourceFields(IEnumerable<string> fields, Relations
130130
var attr = relationProperty.Attributes.SingleOrDefault(a => a.Is(field));
131131
if (attr == null)
132132
{
133-
// TODO: Add unit test for this error, once the nesting limitation is removed and this code becomes reachable again.
134-
135133
throw new InvalidQueryStringParameterException(parameterName,
136-
"Sparse field navigation path refers to an invalid related field.",
137-
$"Related resource '{relationship.RightType.Name}' does not contain an attribute named '{field}'.");
134+
"The specified field does not exist on the requested related resource.",
135+
$"The field '{field}' does not exist on related resource '{relationship.PublicRelationshipName}' of type '{relationProperty.ResourceName}'.");
138136
}
139137

140138
if (attr.PropertyInfo.SetMethod == null)

test/UnitTests/QueryParameters/SparseFieldsServiceTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JsonApiDotNetCoreExample.Models;
88
using Microsoft.Extensions.Primitives;
99
using Xunit;
10+
using Person = UnitTests.TestModels.Person;
1011

1112
namespace UnitTests.QueryParameters
1213
{
@@ -162,6 +163,40 @@ public void Parse_InvalidField_ThrowsJsonApiException()
162163
Assert.Equal("fields", exception.Error.Source.Parameter);
163164
}
164165

166+
[Fact]
167+
public void Parse_InvalidRelatedField_ThrowsJsonApiException()
168+
{
169+
// Arrange
170+
var idAttribute = new AttrAttribute("id") {PropertyInfo = typeof(Article).GetProperty(nameof(Article.Id))};
171+
172+
var query = new KeyValuePair<string, StringValues>("fields[author]", "invalid");
173+
174+
var resourceContext = new ResourceContext
175+
{
176+
ResourceName = "articles",
177+
Attributes = new List<AttrAttribute> {idAttribute},
178+
Relationships = new List<RelationshipAttribute>
179+
{
180+
new HasOneAttribute("author")
181+
{
182+
PropertyInfo = typeof(Article).GetProperty(nameof(Article.Author)),
183+
RightType = typeof(Person)
184+
}
185+
}
186+
};
187+
188+
var service = GetService(resourceContext);
189+
190+
// Act, assert
191+
var exception = Assert.Throws<InvalidQueryStringParameterException>(() => service.Parse(query.Key, query.Value));
192+
193+
Assert.Equal("fields[author]", exception.QueryParameterName);
194+
Assert.Equal(HttpStatusCode.BadRequest, exception.Error.StatusCode);
195+
Assert.Equal("The specified field does not exist on the requested related resource.", exception.Error.Title);
196+
Assert.Equal("The field 'invalid' does not exist on related resource 'author' of type 'people'.", exception.Error.Detail);
197+
Assert.Equal("fields[author]", exception.Error.Source.Parameter);
198+
}
199+
165200
[Fact]
166201
public void Parse_LegacyNotation_ThrowsJsonApiException()
167202
{

0 commit comments

Comments
 (0)