Skip to content

Commit b0146c4

Browse files
committed
refactor(*): further reduction of Dasherize usage
there should be very few places .Dasherize is actually used. most of the time we should stick to using either the Internal or Public attr/relationship names
1 parent fd5e04a commit b0146c4

File tree

12 files changed

+28
-33
lines changed

12 files changed

+28
-33
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using JsonApiDotNetCore.Extensions;
54
using JsonApiDotNetCore.Internal;
65
using JsonApiDotNetCore.Models;
76
using JsonApiDotNetCore.Services;
@@ -148,7 +147,7 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
148147
}
149148
};
150149

151-
if (RelationshipIsIncluded(r.InternalRelationshipName))
150+
if (RelationshipIsIncluded(r.PublicRelationshipName))
152151
{
153152
var navigationEntity = _jsonApiContext.ContextGraph
154153
.GetRelationship(entity, r.InternalRelationshipName);
@@ -161,7 +160,7 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
161160
relationshipData.SingleData = GetRelationship(navigationEntity, r.InternalRelationshipName);
162161
}
163162

164-
data.Relationships.Add(r.InternalRelationshipName.Dasherize(), relationshipData);
163+
data.Relationships.Add(r.PublicRelationshipName, relationshipData);
165164
});
166165
}
167166

@@ -171,7 +170,7 @@ private List<DocumentData> GetIncludedEntities(ContextEntity contextEntity, IIde
171170

172171
contextEntity.Relationships.ForEach(r =>
173172
{
174-
if (!RelationshipIsIncluded(r.InternalRelationshipName)) return;
173+
if (!RelationshipIsIncluded(r.PublicRelationshipName)) return;
175174

176175
var navigationEntity = _jsonApiContext.ContextGraph.GetRelationship(entity, r.InternalRelationshipName);
177176

@@ -214,7 +213,7 @@ private DocumentData GetIncludedEntity(IIdentifiable entity)
214213
private bool RelationshipIsIncluded(string relationshipName)
215214
{
216215
return _jsonApiContext.IncludedRelationships != null &&
217-
_jsonApiContext.IncludedRelationships.Contains(relationshipName.ToProperCase());
216+
_jsonApiContext.IncludedRelationships.Contains(relationshipName);
218217
}
219218

220219
private List<Dictionary<string, string>> GetRelationships(IEnumerable<object> entities, string relationshipName)

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void BuildContextGraph<TContext>(Action<IContextGraphBuilder> builder)
2929
public void BuildContextGraph(Action<IContextGraphBuilder> builder)
3030
{
3131
if(builder == null)
32-
throw new ArgumentException("Cannot build non-EF context graph without a IContextGraphBuilder action", nameof(builder));
32+
throw new ArgumentException("Cannot build non-EF context graph without an IContextGraphBuilder action", nameof(builder));
3333

3434
var contextGraphBuilder = new ContextGraphBuilder();
3535

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ public virtual async Task<bool> DeleteAsync(TId id)
138138
public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string relationshipName)
139139
{
140140
var entity = _jsonApiContext.RequestEntity;
141-
if(entity.Relationships.Any(r => r.InternalRelationshipName == relationshipName))
142-
return entities.Include(relationshipName);
141+
var relationship = entity.Relationships.FirstOrDefault(r => r.PublicRelationshipName == relationshipName);
142+
if(relationship != null)
143+
return entities.Include(relationship.InternalRelationshipName);
143144

144-
throw new JsonApiException("400", "Invalid relationship",
145+
throw new JsonApiException("400", $"Invalid relationship {relationshipName} on {entity.EntityName}",
145146
$"{entity.EntityName} does not have a relationship named {relationshipName}");
146147
}
147148

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ private List<string> ParseIncludedRelationships(string value)
147147

148148
return value
149149
.Split(',')
150-
.Select(s => s.ToProperCase())
151150
.ToList();
152151
}
153152

src/JsonApiDotNetCore/Models/DocumentData.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
using System.Collections.Generic;
2-
using JsonApiDotNetCore.Extensions;
32
using Newtonsoft.Json;
43

54
namespace JsonApiDotNetCore.Models
65
{
76
public class DocumentData
87
{
9-
private string _type;
10-
118
[JsonProperty("type")]
12-
public string Type
13-
{
14-
get { return _type.Dasherize(); }
15-
set { _type = value; }
16-
}
9+
public string Type { get; set; }
1710

1811
[JsonProperty("id")]
1912
public string Id { get; set; }

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private object _setHasOneRelationship(object entity,
135135
if (entityProperty == null)
136136
throw new JsonApiException("400", $"{contextEntity.EntityType.Name} does not contain an relationsip named {attr.InternalRelationshipName}");
137137

138-
var relationshipName = attr.InternalRelationshipName.Dasherize();
138+
var relationshipName = attr.PublicRelationshipName;
139139

140140
if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData))
141141
{
@@ -168,7 +168,7 @@ private object _setHasManyRelationship(object entity,
168168
if (entityProperty == null)
169169
throw new JsonApiException("400", $"{contextEntity.EntityType.Name} does not contain an relationsip named {attr.InternalRelationshipName}");
170170

171-
var relationshipName = attr.InternalRelationshipName.Dasherize();
171+
var relationshipName = attr.PublicRelationshipName;
172172

173173
if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData))
174174
{

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private async Task<T> GetWithRelationshipsAsync(TId id)
7373
var query = _entities.Get();
7474
_jsonApiContext.QuerySet.IncludedRelationships.ForEach(r =>
7575
{
76-
query = _entities.Include(query, r.ToProperCase());
76+
query = _entities.Include(query, r);
7777
});
7878
return await query.FirstOrDefaultAsync(e => e.Id.Equals(id));
7979
}
@@ -174,9 +174,9 @@ private async Task<IEnumerable<T>> ApplyPageQueryAsync(IQueryable<T> entities)
174174
private IQueryable<T> IncludeRelationships(IQueryable<T> entities, List<string> relationships)
175175
{
176176
_jsonApiContext.IncludedRelationships = relationships;
177-
177+
178178
foreach (var r in relationships)
179-
entities = _entities.Include(entities, r.ToProperCase());
179+
entities = _entities.Include(entities, r);
180180

181181
return entities;
182182
}

src/JsonApiDotNetCoreExample/Controllers/TodoItemCollectionsController.cs renamed to src/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace JsonApiDotNetCoreExample.Controllers
88
{
9-
public class TodoItemCollectionsController : JsonApiController<TodoItemCollection, Guid>
9+
public class TodoCollectionsController : JsonApiController<TodoItemCollection, Guid>
1010
{
11-
public TodoItemCollectionsController(
11+
public TodoCollectionsController(
1212
IJsonApiContext jsonApiContext,
1313
IResourceService<TodoItemCollection, Guid> resourceService,
1414
ILoggerFactory loggerFactory)

src/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using JsonApiDotNetCore.Models;
12
using JsonApiDotNetCoreExample.Models;
23
using Microsoft.EntityFrameworkCore;
34

@@ -24,6 +25,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2425

2526
public DbSet<TodoItem> TodoItems { get; set; }
2627
public DbSet<Person> People { get; set; }
28+
29+
[Resource("todo-collections")]
2730
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
2831
}
2932
}

src/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Person : Identifiable, IHasMeta
1818
[HasMany("assigned-todo-items")]
1919
public virtual List<TodoItem> AssignedTodoItems { get; set; }
2020

21-
[HasMany("todo-item-collections")]
21+
[HasMany("todo-collections")]
2222
public virtual List<TodoItemCollection> TodoItemCollections { get; set; }
2323

2424
public Dictionary<string, object> GetMeta(IJsonApiContext context)

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ public async Task Can_Create_Guid_Identifiable_Entity()
5454
context.People.Add(owner);
5555
await context.SaveChangesAsync();
5656

57-
var route = "/api/v1/todo-item-collections";
57+
var route = "/api/v1/todo-collections";
5858
var request = new HttpRequestMessage(httpMethod, route);
5959
var content = new
6060
{
6161
data = new
6262
{
63-
type = "todo-item-collections",
63+
type = "todo-collections",
6464
relationships = new
6565
{
6666
owner = new
@@ -180,14 +180,14 @@ public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_
180180
context.People.Add(owner);
181181
await context.SaveChangesAsync();
182182

183-
var route = "/api/v1/todo-item-collections";
183+
var route = "/api/v1/todo-collections";
184184
var request = new HttpRequestMessage(httpMethod, route);
185185
var clientDefinedId = Guid.NewGuid();
186186
var content = new
187187
{
188188
data = new
189189
{
190-
type = "todo-item-collections",
190+
type = "todo-collections",
191191
id = $"{clientDefinedId}",
192192
relationships = new
193193
{
@@ -234,13 +234,13 @@ public async Task Can_Create_And_Set_HasMany_Relationships()
234234
context.TodoItems.Add(todoItem);
235235
await context.SaveChangesAsync();
236236

237-
var route = "/api/v1/todo-item-collections";
237+
var route = "/api/v1/todo-collections";
238238
var request = new HttpRequestMessage(httpMethod, route);
239239
var content = new
240240
{
241241
data = new
242242
{
243-
type = "todo-item-collections",
243+
type = "todo-collections",
244244
relationships = new Dictionary<string, dynamic>
245245
{
246246
{ "owner", new {

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Included.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public async Task Can_Include_MultipleRelationships()
227227

228228
var httpMethod = new HttpMethod("GET");
229229

230-
var route = $"/api/v1/people/{person.Id}?include=todo-items,todo-item-collections";
230+
var route = $"/api/v1/people/{person.Id}?include=todo-items,todo-collections";
231231

232232
var server = new TestServer(builder);
233233
var client = server.CreateClient();

0 commit comments

Comments
 (0)