|
7 | 7 | using JsonApiDotNetCoreExample.Models;
|
8 | 8 | using Microsoft.Extensions.Primitives;
|
9 | 9 | using Xunit;
|
| 10 | +using Person = UnitTests.TestModels.Person; |
10 | 11 |
|
11 | 12 | namespace UnitTests.QueryParameters
|
12 | 13 | {
|
@@ -162,6 +163,40 @@ public void Parse_InvalidField_ThrowsJsonApiException()
|
162 | 163 | Assert.Equal("fields", exception.Error.Source.Parameter);
|
163 | 164 | }
|
164 | 165 |
|
| 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 | + |
165 | 200 | [Fact]
|
166 | 201 | public void Parse_LegacyNotation_ThrowsJsonApiException()
|
167 | 202 | {
|
|
0 commit comments