Skip to content

Commit 491a6cb

Browse files
authored
Merge pull request #403 from btecu/services
Remove `DocumentData`, instead use `ResourceObject`
2 parents b6ce80d + 1a9fd65 commit 491a6cb

File tree

21 files changed

+61
-74
lines changed

21 files changed

+61
-74
lines changed

Diff for: benchmarks/Serialization/JsonApiDeserializer_Benchmarks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Benchmarks.Serialization {
1717
public class JsonApiDeserializer_Benchmarks {
1818
private const string TYPE_NAME = "simple-types";
1919
private static readonly string Content = JsonConvert.SerializeObject(new Document {
20-
Data = new DocumentData {
20+
Data = new ResourceObject {
2121
Type = TYPE_NAME,
2222
Id = "1",
2323
Attributes = new Dictionary<string, object> {

Diff for: src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsCustomController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
126126
}
127127

128128
[HttpPatch("{id}/relationships/{relationshipName}")]
129-
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
129+
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<ResourceObject> relationships)
130130
{
131131
await _resourceService.UpdateRelationshipsAsync(id, relationshipName, relationships);
132132
return Ok();

Diff for: src/Examples/NoEntityFrameworkExample/Services/TodoItemService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Task<TodoItem> UpdateAsync(int id, TodoItem entity)
8484
throw new NotImplementedException();
8585
}
8686

87-
public Task UpdateRelationshipsAsync(int id, string relationshipName, List<DocumentData> relationships)
87+
public Task UpdateRelationshipsAsync(int id, string relationshipName, List<ResourceObject> relationships)
8888
{
8989
throw new NotImplementedException();
9090
}

Diff for: src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+16-17
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5858
var enumeratedEntities = entities as IList<IIdentifiable> ?? entities.ToList();
5959
var documents = new Documents
6060
{
61-
Data = new List<DocumentData>(),
61+
Data = new List<ResourceObject>(),
6262
Meta = GetMeta(enumeratedEntities.FirstOrDefault())
6363
};
6464

@@ -95,7 +95,7 @@ private Dictionary<string, object> GetMeta(IIdentifiable entity)
9595

9696
private bool ShouldIncludePageLinks(ContextEntity entity) => entity.Links.HasFlag(Link.Paging);
9797

98-
private List<DocumentData> AppendIncludedObject(List<DocumentData> includedObject, ContextEntity contextEntity, IIdentifiable entity)
98+
private List<ResourceObject> AppendIncludedObject(List<ResourceObject> includedObject, ContextEntity contextEntity, IIdentifiable entity)
9999
{
100100
var includedEntities = GetIncludedEntities(includedObject, contextEntity, entity);
101101
if (includedEntities?.Count > 0)
@@ -107,13 +107,12 @@ private List<DocumentData> AppendIncludedObject(List<DocumentData> includedObjec
107107
}
108108

109109
[Obsolete("You should specify an IResourceDefinition implementation using the GetData/3 overload.")]
110-
public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
110+
public ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity)
111111
=> GetData(contextEntity, entity, resourceDefinition: null);
112112

113-
public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity, IResourceDefinition resourceDefinition = null)
113+
public ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity, IResourceDefinition resourceDefinition = null)
114114
{
115-
var data = new DocumentData
116-
{
115+
var data = new ResourceObject {
117116
Type = contextEntity.EntityName,
118117
Id = entity.StringId
119118
};
@@ -151,7 +150,7 @@ private bool OmitNullValuedAttribute(AttrAttribute attr, object attributeValue)
151150
return attributeValue == null && _documentBuilderOptions.OmitNullValuedAttributes;
152151
}
153152

154-
private void AddRelationships(DocumentData data, ContextEntity contextEntity, IIdentifiable entity)
153+
private void AddRelationships(ResourceObject data, ContextEntity contextEntity, IIdentifiable entity)
155154
{
156155
data.Relationships = new Dictionary<string, RelationshipData>();
157156
contextEntity.Relationships.ForEach(r =>
@@ -192,9 +191,9 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context
192191
return relationshipData;
193192
}
194193

195-
private List<DocumentData> GetIncludedEntities(List<DocumentData> included, ContextEntity rootContextEntity, IIdentifiable rootResource)
194+
private List<ResourceObject> GetIncludedEntities(List<ResourceObject> included, ContextEntity rootContextEntity, IIdentifiable rootResource)
196195
{
197-
if(_jsonApiContext.IncludedRelationships != null)
196+
if (_jsonApiContext.IncludedRelationships != null)
198197
{
199198
foreach(var relationshipName in _jsonApiContext.IncludedRelationships)
200199
{
@@ -209,8 +208,8 @@ private List<DocumentData> GetIncludedEntities(List<DocumentData> included, Cont
209208
return included;
210209
}
211210

212-
private List<DocumentData> IncludeRelationshipChain(
213-
List<DocumentData> included, ContextEntity parentEntity, IIdentifiable parentResource, string[] relationshipChain, int relationshipChainIndex)
211+
private List<ResourceObject> IncludeRelationshipChain(
212+
List<ResourceObject> included, ContextEntity parentEntity, IIdentifiable parentResource, string[] relationshipChain, int relationshipChainIndex)
214213
{
215214
var requestedRelationship = relationshipChain[relationshipChainIndex];
216215
var relationship = parentEntity.Relationships.FirstOrDefault(r => r.PublicRelationshipName == requestedRelationship);
@@ -232,10 +231,10 @@ private List<DocumentData> IncludeRelationshipChain(
232231
return included;
233232
}
234233

235-
private List<DocumentData> IncludeSingleResourceRelationships(
236-
List<DocumentData> included, IIdentifiable navigationEntity, RelationshipAttribute relationship, string[] relationshipChain, int relationshipChainIndex)
234+
private List<ResourceObject> IncludeSingleResourceRelationships(
235+
List<ResourceObject> included, IIdentifiable navigationEntity, RelationshipAttribute relationship, string[] relationshipChain, int relationshipChainIndex)
237236
{
238-
if(relationshipChainIndex < relationshipChain.Length)
237+
if (relationshipChainIndex < relationshipChain.Length)
239238
{
240239
var nextContextEntity = _jsonApiContext.ContextGraph.GetContextEntity(relationship.Type);
241240
var resource = (IIdentifiable)navigationEntity;
@@ -248,12 +247,12 @@ private List<DocumentData> IncludeSingleResourceRelationships(
248247
}
249248

250249

251-
private List<DocumentData> AddIncludedEntity(List<DocumentData> entities, IIdentifiable entity)
250+
private List<ResourceObject> AddIncludedEntity(List<ResourceObject> entities, IIdentifiable entity)
252251
{
253252
var includedEntity = GetIncludedEntity(entity);
254253

255254
if (entities == null)
256-
entities = new List<DocumentData>();
255+
entities = new List<ResourceObject>();
257256

258257
if (includedEntity != null && entities.Any(doc =>
259258
string.Equals(doc.Id, includedEntity.Id) && string.Equals(doc.Type, includedEntity.Type)) == false)
@@ -264,7 +263,7 @@ private List<DocumentData> AddIncludedEntity(List<DocumentData> entities, IIdent
264263
return entities;
265264
}
266265

267-
private DocumentData GetIncludedEntity(IIdentifiable entity)
266+
private ResourceObject GetIncludedEntity(IIdentifiable entity)
268267
{
269268
if (entity == null) return null;
270269

Diff for: src/JsonApiDotNetCore/Builders/IDocumentBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IDocumentBuilder
1111
Documents Build(IEnumerable<IIdentifiable> entities);
1212

1313
[Obsolete("You should specify an IResourceDefinition implementation using the GetData/3 overload.")]
14-
DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity);
15-
DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity, IResourceDefinition resourceDefinition = null);
14+
ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity);
15+
ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity, IResourceDefinition resourceDefinition = null);
1616
}
1717
}

Diff for: src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
181181
return Ok(updatedEntity);
182182
}
183183

184-
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
184+
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<ResourceObject> relationships)
185185
{
186186
if (_updateRelationships == null) throw Exceptions.UnSupportedRequestMethod;
187187

Diff for: src/JsonApiDotNetCore/Controllers/JsonApiCmdController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity
3535

3636
[HttpPatch("{id}/relationships/{relationshipName}")]
3737
public override async Task<IActionResult> PatchRelationshipsAsync(
38-
TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
38+
TId id, string relationshipName, [FromBody] List<ResourceObject> relationships)
3939
=> await base.PatchRelationshipsAsync(id, relationshipName, relationships);
4040

4141
[HttpDelete("{id}")]

Diff for: src/JsonApiDotNetCore/Controllers/JsonApiController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public override async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity
8888

8989
[HttpPatch("{id}/relationships/{relationshipName}")]
9090
public override async Task<IActionResult> PatchRelationshipsAsync(
91-
TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
91+
TId id, string relationshipName, [FromBody] List<ResourceObject> relationships)
9292
=> await base.PatchRelationshipsAsync(id, relationshipName, relationships);
9393

9494
[HttpDelete("{id}")]

Diff for: src/JsonApiDotNetCore/Models/Document.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace JsonApiDotNetCore.Models
55
public class Document : DocumentBase
66
{
77
[JsonProperty("data")]
8-
public DocumentData Data { get; set; }
8+
public ResourceObject Data { get; set; }
99
}
1010
}

Diff for: src/JsonApiDotNetCore/Models/DocumentBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class DocumentBase
99
public RootLinks Links { get; set; }
1010

1111
[JsonProperty("included", NullValueHandling = NullValueHandling.Ignore)]
12-
public List<DocumentData> Included { get; set; }
12+
public List<ResourceObject> Included { get; set; }
1313

1414
[JsonProperty("meta", NullValueHandling = NullValueHandling.Ignore)]
1515
public Dictionary<string, object> Meta { get; set; }

Diff for: src/JsonApiDotNetCore/Models/DocumentData.cs

-5
This file was deleted.

Diff for: src/JsonApiDotNetCore/Models/Documents.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace JsonApiDotNetCore.Models
66
public class Documents : DocumentBase
77
{
88
[JsonProperty("data")]
9-
public List<DocumentData> Data { get; set; }
9+
public List<ResourceObject> Data { get; set; }
1010
}
1111
}

Diff for: src/JsonApiDotNetCore/Models/Operations/Operation.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ private void SetData(object data)
3232
if (data is JArray jArray)
3333
{
3434
DataIsList = true;
35-
DataList = jArray.ToObject<List<DocumentData>>();
35+
DataList = jArray.ToObject<List<ResourceObject>>();
3636
}
37-
else if (data is List<DocumentData> dataList)
37+
else if (data is List<ResourceObject> dataList)
3838
{
3939
DataIsList = true;
4040
DataList = dataList;
4141
}
4242
else if (data is JObject jObject)
4343
{
44-
DataObject = jObject.ToObject<DocumentData>();
44+
DataObject = jObject.ToObject<ResourceObject>();
4545
}
46-
else if (data is DocumentData dataObject)
46+
else if (data is ResourceObject dataObject)
4747
{
4848
DataObject = dataObject;
4949
}
@@ -53,10 +53,10 @@ private void SetData(object data)
5353
public bool DataIsList { get; private set; }
5454

5555
[JsonIgnore]
56-
public List<DocumentData> DataList { get; private set; }
56+
public List<ResourceObject> DataList { get; private set; }
5757

5858
[JsonIgnore]
59-
public DocumentData DataObject { get; private set; }
59+
public ResourceObject DataObject { get; private set; }
6060

6161
public string GetResourceTypeName()
6262
{

Diff for: src/JsonApiDotNetCore/Serialization/IJsonApiDeSerializer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public interface IJsonApiDeSerializer
99
TEntity Deserialize<TEntity>(string requestBody);
1010
object DeserializeRelationship(string requestBody);
1111
List<TEntity> DeserializeList<TEntity>(string requestBody);
12-
object DocumentToObject(DocumentData data, List<DocumentData> included = null);
12+
object DocumentToObject(ResourceObject data, List<ResourceObject> included = null);
1313
}
1414
}

Diff for: src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public object DeserializeRelationship(string requestBody)
8080
var data = JToken.Parse(requestBody)["data"];
8181

8282
if (data is JArray)
83-
return data.ToObject<List<DocumentData>>();
83+
return data.ToObject<List<ResourceObject>>();
8484

85-
return new List<DocumentData> { data.ToObject<DocumentData>() };
85+
return new List<ResourceObject> { data.ToObject<ResourceObject>() };
8686
}
8787
catch (Exception e)
8888
{
@@ -111,7 +111,7 @@ public List<TEntity> DeserializeList<TEntity>(string requestBody)
111111
}
112112
}
113113

114-
public object DocumentToObject(DocumentData data, List<DocumentData> included = null)
114+
public object DocumentToObject(ResourceObject data, List<ResourceObject> included = null)
115115
{
116116
if (data == null)
117117
throw new JsonApiException(422, "Failed to deserialize document as json:api.");
@@ -175,7 +175,7 @@ private object SetRelationships(
175175
object entity,
176176
ContextEntity contextEntity,
177177
Dictionary<string, RelationshipData> relationships,
178-
List<DocumentData> included = null)
178+
List<ResourceObject> included = null)
179179
{
180180
if (relationships == null || relationships.Count == 0)
181181
return entity;
@@ -197,7 +197,7 @@ private object SetHasOneRelationship(object entity,
197197
HasOneAttribute attr,
198198
ContextEntity contextEntity,
199199
Dictionary<string, RelationshipData> relationships,
200-
List<DocumentData> included = null)
200+
List<ResourceObject> included = null)
201201
{
202202
var relationshipName = attr.PublicRelationshipName;
203203

@@ -254,7 +254,7 @@ private void SetHasOneForeignKeyValue(object entity, HasOneAttribute hasOneAttr,
254254
/// If the resource has been included, all attributes will be set.
255255
/// If the resource has not been included, only the id will be set.
256256
/// </summary>
257-
private void SetHasOneNavigationPropertyValue(object entity, HasOneAttribute hasOneAttr, ResourceIdentifierObject rio, List<DocumentData> included)
257+
private void SetHasOneNavigationPropertyValue(object entity, HasOneAttribute hasOneAttr, ResourceIdentifierObject rio, List<ResourceObject> included)
258258
{
259259
// if the resource identifier is null, there should be no reason to instantiate an instance
260260
if (rio != null && rio.Id != null)
@@ -277,7 +277,7 @@ private object SetHasManyRelationship(object entity,
277277
RelationshipAttribute attr,
278278
ContextEntity contextEntity,
279279
Dictionary<string, RelationshipData> relationships,
280-
List<DocumentData> included = null)
280+
List<ResourceObject> included = null)
281281
{
282282
var relationshipName = attr.PublicRelationshipName;
283283

@@ -303,7 +303,7 @@ private object SetHasManyRelationship(object entity,
303303
return entity;
304304
}
305305

306-
private IIdentifiable GetIncludedRelationship(ResourceIdentifierObject relatedResourceIdentifier, List<DocumentData> includedResources, RelationshipAttribute relationshipAttr)
306+
private IIdentifiable GetIncludedRelationship(ResourceIdentifierObject relatedResourceIdentifier, List<ResourceObject> includedResources, RelationshipAttribute relationshipAttr)
307307
{
308308
// at this point we can be sure the relationshipAttr.Type is IIdentifiable because we were able to successfully build the ContextGraph
309309
var relatedInstance = relationshipAttr.Type.New<IIdentifiable>();
@@ -326,7 +326,7 @@ private IIdentifiable GetIncludedRelationship(ResourceIdentifierObject relatedRe
326326
return relatedInstance;
327327
}
328328

329-
private DocumentData GetLinkedResource(ResourceIdentifierObject relatedResourceIdentifier, List<DocumentData> includedResources)
329+
private ResourceObject GetLinkedResource(ResourceIdentifierObject relatedResourceIdentifier, List<ResourceObject> includedResources)
330330
{
331331
try
332332
{

Diff for: src/JsonApiDotNetCore/Services/Contract/IUpdateRelationshipService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public interface IUpdateRelationshipService<T> : IUpdateRelationshipService<T, i
1111
public interface IUpdateRelationshipService<T, in TId>
1212
where T : class, IIdentifiable<TId>
1313
{
14-
Task UpdateRelationshipsAsync(TId id, string relationshipName, List<DocumentData> relationships);
14+
Task UpdateRelationshipsAsync(TId id, string relationshipName, List<ResourceObject> relationships);
1515
}
1616
}

Diff for: src/JsonApiDotNetCore/Services/EntityResourceService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource)
157157
return MapOut(entity);
158158
}
159159

160-
public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipName, List<DocumentData> relationships)
160+
public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipName, List<ResourceObject> relationships)
161161
{
162162
var entity = await _entities.GetAndIncludeAsync(id, relationshipName);
163163
if (entity == null)

Diff for: src/JsonApiDotNetCore/Services/Operations/Processors/GetOpProcessor.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private async Task<object> GetAllAsync(Operation operation)
9696
{
9797
var result = await _getAll.GetAsync();
9898

99-
var operations = new List<DocumentData>();
99+
var operations = new List<ResourceObject>();
100100
foreach (var resource in result)
101101
{
102102
var doc = _documentBuilder.GetData(
@@ -153,14 +153,14 @@ private async Task<object> GetRelationshipAsync(Operation operation)
153153
detail: $"Type '{result.GetType()} does not implement {nameof(IIdentifiable)} nor {nameof(IEnumerable<IIdentifiable>)}'");
154154
}
155155

156-
private DocumentData GetData(ContextEntity contextEntity, IIdentifiable singleResource)
156+
private ResourceObject GetData(ContextEntity contextEntity, IIdentifiable singleResource)
157157
{
158158
return _documentBuilder.GetData(contextEntity, singleResource);
159159
}
160160

161-
private List<DocumentData> GetData(ContextEntity contextEntity, IEnumerable multipleResults)
161+
private List<ResourceObject> GetData(ContextEntity contextEntity, IEnumerable multipleResults)
162162
{
163-
var resources = new List<DocumentData>();
163+
var resources = new List<ResourceObject>();
164164
foreach (var singleResult in multipleResults)
165165
{
166166
if (singleResult is IIdentifiable resource)

Diff for: test/JsonApiDotNetCoreExampleTests/Helpers/Extensions/DocumentExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace JsonApiDotNetCoreExampleTests.Helpers.Extensions
1313
{
1414
public static class DocumentExtensions
1515
{
16-
public static DocumentData FindResource<TId>(this List<DocumentData> included, string type, TId id)
16+
public static ResourceObject FindResource<TId>(this List<ResourceObject> included, string type, TId id)
1717
{
1818
var document = included.Where(documentData => (
1919
documentData.Type == type
@@ -23,8 +23,8 @@ public static DocumentData FindResource<TId>(this List<DocumentData> included, s
2323
return document;
2424
}
2525

26-
public static int CountOfType(this List<DocumentData> included, string type) {
26+
public static int CountOfType(this List<ResourceObject> included, string type) {
2727
return included.Where(documentData => documentData.Type == type).Count();
2828
}
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)