Skip to content

Commit ff70656

Browse files
author
Bart Koelman
committed
Fix rendering StringId in errors for injected resources
1 parent 8fe35df commit ff70656

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

src/Examples/JsonApiDotNetCoreExample/HexadecimalObfuscationCodec.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static string FromHexString(string hexString)
3333
bytes.Add(bt);
3434
}
3535

36-
var chars = Encoding.Unicode.GetChars(bytes.ToArray());
36+
var chars = Encoding.ASCII.GetChars(bytes.ToArray());
3737
return new string(chars);
3838
}
3939

@@ -52,7 +52,7 @@ private static string ToHexString(string value)
5252
{
5353
var builder = new StringBuilder();
5454

55-
foreach (byte bt in Encoding.Unicode.GetBytes(value))
55+
foreach (byte bt in Encoding.ASCII.GetBytes(value))
5656
{
5757
builder.Append(bt.ToString("X2"));
5858
}

src/JsonApiDotNetCore/Extensions/QueryableExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ private static IQueryable<TSource> CallGenericWhereContainsMethod<TSource>(IQuer
206206
member = Expression.Property(entity, filter.Attribute.PropertyInfo.Name);
207207

208208
var method = ContainsMethod.MakeGenericMethod(member.Type);
209-
210-
var listType = typeof(List<>).MakeGenericType(member.Type);
211-
var list = (IList)TypeHelper.CreateInstance(listType);
209+
var list = TypeHelper.CreateListFor(member.Type);
212210

213211
foreach (var value in propertyValues)
214212
{

src/JsonApiDotNetCore/Internal/TypeHelper.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,6 @@ public static Type GetIdType(Type resourceType)
219219
return property.PropertyType;
220220
}
221221

222-
public static T CreateInstance<T>()
223-
{
224-
return (T) CreateInstance(typeof(T));
225-
}
226-
227222
public static object CreateInstance(Type type)
228223
{
229224
if (type == null) throw new ArgumentNullException(nameof(type));
@@ -251,9 +246,9 @@ public static object GetResourceTypedId(IIdentifiable resource)
251246
return property.GetValue(resource);
252247
}
253248

254-
public static string GetResourceStringId<TResource, TId>(TId id) where TResource : class, IIdentifiable<TId>
249+
public static string GetResourceStringId<TResource, TId>(TId id, IResourceFactory resourceFactory) where TResource : class, IIdentifiable<TId>
255250
{
256-
TResource tempResource = CreateInstance<TResource>();
251+
TResource tempResource = resourceFactory.CreateInstance<TResource>();
257252
tempResource.Id = id;
258253
return tempResource.StringId;
259254
}

src/JsonApiDotNetCore/Services/DefaultResourceService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public virtual async Task DeleteAsync(TId id)
9191

9292
_hookExecutor.BeforeDelete(AsList(entity), ResourcePipeline.Delete);
9393
}
94-
94+
9595
var succeeded = await _repository.DeleteAsync(id);
9696
if (!succeeded)
9797
{
98-
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id);
98+
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id, _resourceFactory);
9999
throw new ResourceNotFoundException(resourceId, _currentRequestResource.ResourceName);
100100
}
101101

@@ -149,7 +149,7 @@ public virtual async Task<TResource> GetAsync(TId id)
149149

150150
if (entity == null)
151151
{
152-
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id);
152+
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id, _resourceFactory);
153153
throw new ResourceNotFoundException(resourceId, _currentRequestResource.ResourceName);
154154
}
155155

@@ -177,7 +177,7 @@ public virtual async Task<TResource> GetRelationshipsAsync(TId id, string relati
177177

178178
if (entity == null)
179179
{
180-
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id);
180+
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id, _resourceFactory);
181181
throw new ResourceNotFoundException(resourceId, _currentRequestResource.ResourceName);
182182
}
183183

@@ -207,7 +207,7 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource requestEntity
207207
TResource databaseEntity = await _repository.Get(id).FirstOrDefaultAsync();
208208
if (databaseEntity == null)
209209
{
210-
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id);
210+
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id, _resourceFactory);
211211
throw new ResourceNotFoundException(resourceId, _currentRequestResource.ResourceName);
212212
}
213213

@@ -243,7 +243,7 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
243243

244244
if (entity == null)
245245
{
246-
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id);
246+
string resourceId = TypeHelper.GetResourceStringId<TResource, TId>(id, _resourceFactory);
247247
throw new ResourceNotFoundException(resourceId, _currentRequestResource.ResourceName);
248248
}
249249

test/JsonApiDotNetCoreExampleTests/Acceptance/InjectableResourceTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Bogus;
66
using JsonApiDotNetCore.Models;
7+
using JsonApiDotNetCore.Models.JsonApiDocuments;
78
using JsonApiDotNetCoreExample;
89
using JsonApiDotNetCoreExample.Data;
910
using JsonApiDotNetCoreExample.Models;
@@ -195,5 +196,27 @@ public async Task Can_Get_Passports_With_Sparse_Fieldset()
195196
Assert.DoesNotContain(document.Included,
196197
resource => resource.Attributes.ContainsKey("lastName"));
197198
}
199+
200+
[Fact]
201+
public async Task Fail_When_Deleting_Missing_Passport()
202+
{
203+
// Arrange
204+
string passportId = HexadecimalObfuscationCodec.Encode(1234567890);
205+
206+
var request = new HttpRequestMessage(HttpMethod.Delete, "/api/v1/passports/" + passportId);
207+
208+
// Act
209+
var response = await _fixture.Client.SendAsync(request);
210+
211+
// Assert
212+
_fixture.AssertEqualStatusCode(HttpStatusCode.NotFound, response);
213+
214+
var body = await response.Content.ReadAsStringAsync();
215+
var errorDocument = JsonConvert.DeserializeObject<ErrorDocument>(body);
216+
Assert.Single(errorDocument.Errors);
217+
Assert.Equal(HttpStatusCode.NotFound, errorDocument.Errors[0].StatusCode);
218+
Assert.Equal("The requested resource does not exist.", errorDocument.Errors[0].Title);
219+
Assert.Equal("Resource of type 'passports' with id '" + passportId + "' does not exist.", errorDocument.Errors[0].Detail);
220+
}
198221
}
199222
}

0 commit comments

Comments
 (0)