Skip to content

Commit 7c20d77

Browse files
committed
refactor(context-graph): the stored entity name should be dasherized once
there is really no reason to store the proper cased entity name this also lays the ground work so that later users can specify different cases for entity type names
1 parent 09cc32a commit 7c20d77

File tree

7 files changed

+13
-15
lines changed

7 files changed

+13
-15
lines changed

src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.EntityFrameworkCore;
55
using JsonApiDotNetCore.Internal;
66
using JsonApiDotNetCore.Models;
7-
using Newtonsoft.Json;
7+
using JsonApiDotNetCore.Extensions;
88

99
namespace JsonApiDotNetCore.Builders
1010
{
@@ -100,7 +100,7 @@ public void AddDbContext<T>() where T : DbContext
100100
var entityType = dbSetType.GetGenericArguments()[0];
101101
entities.Add(new ContextEntity
102102
{
103-
EntityName = property.Name,
103+
EntityName = property.Name.Dasherize(),
104104
EntityType = entityType,
105105
Attributes = GetAttributes(entityType),
106106
Relationships = GetRelationships(entityType)

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
143143
{
144144
Links = new Links
145145
{
146-
Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, r.InternalRelationshipName),
147-
Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.InternalRelationshipName)
146+
Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName),
147+
Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName)
148148
}
149149
};
150150

@@ -227,7 +227,7 @@ private List<Dictionary<string, string>> GetRelationships(IEnumerable<object> en
227227
foreach (var entity in entities)
228228
{
229229
relationships.Add(new Dictionary<string, string> {
230-
{"type", typeName.EntityName.Dasherize() },
230+
{"type", typeName.EntityName },
231231
{"id", ((IIdentifiable)entity).StringId }
232232
});
233233
}
@@ -240,7 +240,7 @@ private Dictionary<string, string> GetRelationship(object entity, string relatio
240240
var typeName = _jsonApiContext.ContextGraph.GetContextEntity(objType);
241241

242242
return new Dictionary<string, string> {
243-
{"type", typeName.EntityName.Dasherize() },
243+
{"type", typeName.EntityName },
244244
{"id", ((IIdentifiable)entity).StringId }
245245
};
246246
}

src/JsonApiDotNetCore/Builders/LinkBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public string GetSelfRelationLink(string parent, string parentId, string child)
4242

4343
public string GetRelatedRelationLink(string parent, string parentId, string child)
4444
{
45-
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}";
45+
return $"{_context.BasePath}/{parent}/{parentId}/{child}";
4646
}
4747

4848
public string GetPageLink(int pageOffset, int pageSize)
4949
{
50-
return $"{_context.BasePath}/{_context.RequestEntity.EntityName.Dasherize()}?page[size]={pageSize}&page[number]={pageOffset}";
50+
return $"{_context.BasePath}/{_context.RequestEntity.EntityName}?page[size]={pageSize}&page[number]={pageOffset}";
5151
}
5252
}
5353
}

src/JsonApiDotNetCore/Internal/ContextGraph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class ContextGraph : IContextGraph
1010
public List<ContextEntity> Entities { get; set; }
1111
public bool UsesDbContext { get; set; }
1212

13-
public ContextEntity GetContextEntity(string dbSetName)
13+
public ContextEntity GetContextEntity(string entityName)
1414
{
1515
return Entities
1616
.FirstOrDefault(e =>
17-
e.EntityName.ToLower() == dbSetName.ToLower());
17+
e.EntityName.ToLower() == entityName.ToLower());
1818
}
1919

2020
public ContextEntity GetContextEntity(Type entityType)

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private List<string> ParseFieldsQuery(string key, string value)
158158

159159
var includedFields = new List<string> { "Id" };
160160

161-
if(typeName != _jsonApiContext.RequestEntity.EntityName.Dasherize())
161+
if(typeName != _jsonApiContext.RequestEntity.EntityName)
162162
return includedFields;
163163

164164
var fields = value.Split(',');

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public List<TEntity> DeserializeList<TEntity>(string requestBody)
6161

6262
private object DataToObject(DocumentData data)
6363
{
64-
var entityTypeName = data.Type.ToProperCase();
65-
66-
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(entityTypeName);
64+
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(data.Type);
6765
_jsonApiContext.RequestEntity = contextEntity;
6866

6967
var entity = Activator.CreateInstance(contextEntity.EntityType);

src/NoEntityFrameworkExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ConfigureServices(IServiceCollection services)
3434
services.AddJsonApi(options => {
3535
options.Namespace = "api/v1";
3636
options.BuildContextGraph((builder) => {
37-
builder.AddResource<MyModel>("MyModels");
37+
builder.AddResource<MyModel>("my-models");
3838
});
3939
}, mvcBuilder);
4040

0 commit comments

Comments
 (0)