Skip to content

Commit c55672b

Browse files
committed
chore: finishing touches comments serialization
1 parent b13f55e commit c55672b

24 files changed

+47
-97
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using JsonApiDotNetCore.Serialization;
1414
using Microsoft.EntityFrameworkCore;
1515
using Microsoft.Extensions.Logging;
16+
1617
namespace JsonApiDotNetCore.Data
1718
{
1819
/// <summary>

src/JsonApiDotNetCore/DependencyInjection/ServiceLocator.cs

-18
This file was deleted.

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public static void AddJsonApiInternals(
218218
services.AddScoped<IFieldsToSerialize, FieldsToSerialize>();
219219

220220
AddServerSerialization(services);
221-
AddClientSerialization(services);
221+
222222
if (jsonApiOptions.EnableResourceHooks)
223223
AddResourceHooks(services);
224224

@@ -244,11 +244,15 @@ private static void AddServerSerialization(IServiceCollection services)
244244
services.AddScoped(typeof(IMetaBuilder<>), typeof(MetaBuilder<>));
245245
services.AddScoped(typeof(ResponseSerializer<>));
246246
services.AddScoped(sp => sp.GetRequiredService<IJsonApiSerializerFactory>().GetSerializer());
247-
248247
services.AddScoped<IResourceObjectBuilder, ResponseResourceObjectBuilder>();
249248
}
250249

251-
private static void AddClientSerialization(IServiceCollection services)
250+
/// <summary>
251+
/// Enables client serializers for sending requests and receiving responses
252+
/// in json:api format. Internally only used for testing.
253+
/// Will be extended in the future to be part of a JsonApiClientDotNetCore package.
254+
/// </summary>
255+
public static void AddClientSerialization(this IServiceCollection services)
252256
{
253257
services.AddScoped<IResponseDeserializer, ResponseDeserializer>();
254258

src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace JsonApiDotNetCore.Formatters
1212
/// <summary>
1313
/// Formats the response data used https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.0.
1414
/// It was intended to have as little dependencies as possible in formatting layer for greater extensibility.
15-
/// It onls depends on <see cref="IJsonApiSerializerFactory"/>.
15+
/// It onls depends on <see cref="IJsonApiSerializer"/>.
1616
/// </summary>
1717
public class JsonApiWriter : IJsonApiWriter
1818
{

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

+1-6
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
<Folder Include="Serialization\Server\Builders\" />
5656
<Folder Include="Query\Contracts\" />
5757
<Folder Include="Query\Common\" />
58-
</ItemGroup>
59-
<ItemGroup>
60-
<Compile Remove="Serialization\Client\RequestResourceObjectBuilder.cs" />
61-
</ItemGroup>
62-
<ItemGroup>
63-
<None Include="Serialization\Client\RequestResourceObjectBuilder.cs" Condition=" '$(EnableDefaultCompileItems)' == 'true' " />
58+
<Folder Include="Serialization\Server\Contracts\" />
6459
</ItemGroup>
6560
</Project>

src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Threading.Tasks;
44
using JsonApiDotNetCore.Internal;
55
using JsonApiDotNetCore.Managers.Contracts;
6-
using JsonApiDotNetCore.Services;
76
using Microsoft.AspNetCore.Http;
87
using Microsoft.AspNetCore.Routing;
98
using Microsoft.Extensions.Primitives;

src/JsonApiDotNetCore/Serialization/Client/IRequestResourceObjectBuilder.cs

-7
This file was deleted.

src/JsonApiDotNetCore/Serialization/Client/RequestResourceObjectBuilder.cs

-15
This file was deleted.

src/JsonApiDotNetCore/Serialization/Client/RequestSerializer.cs

-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99

1010
namespace JsonApiDotNetCore.Serialization.Client
1111
{
12-
1312
/// <summary>
1413
/// Client serializer implementation of <see cref="BaseDocumentBuilder"/>
15-
/// Note that this implementation does not override the default implementation
16-
/// of <see cref="ResourceObjectBuilder.GetRelationshipData"/>.
1714
/// </summary>
1815
public class RequestSerializer : BaseDocumentBuilder, IRequestSerializer
1916
{

src/JsonApiDotNetCore/Serialization/Common/DocumentBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace JsonApiDotNetCore.Serialization
77
{
88
/// <summary>
9-
/// Abstract base class for serialization that extends <see cref="ResourceObjectBuilder"/>.
10-
/// Converts entities in to <see cref="ResourceObject"/>s and wraps them in a <see cref="Document"/>.
9+
/// Abstract base class for serialization.
10+
/// Uses <see cref="IResourceObjectBuilder"/> to convert entities in to <see cref="ResourceObject"/>s and wraps them in a <see cref="Document"/>.
1111
/// </summary>
1212
public abstract class BaseDocumentBuilder
1313
{

src/JsonApiDotNetCore/Serialization/Common/IResourceObjectBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace JsonApiDotNetCore.Serialization
55
{
66
/// <summary>
7-
/// Abstract base class for serialization. Converts entities in to <see cref="ResourceObject"/>s
7+
/// Responsible for converting entities in to <see cref="ResourceObject"/>s
88
/// given a list of attributes and relationships.
99
/// </summary>
1010
public interface IResourceObjectBuilder

src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs

+32-36
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
namespace JsonApiDotNetCore.Serialization
1010
{
1111

12-
/// <summary>
13-
/// Abstract base class for serialization. Converts entities in to <see cref="ResourceObject"/>s
14-
/// given a list of attributes and relationships.
15-
/// </summary>
12+
/// <inheritdoc/>
1613
public class ResourceObjectBuilder : IResourceObjectBuilder
1714
{
1815
protected readonly IResourceGraph _resourceGraph;
@@ -27,14 +24,7 @@ public ResourceObjectBuilder(IResourceGraph resourceGraph, IContextEntityProvide
2724
_settings = settings;
2825
}
2926

30-
/// <summary>
31-
/// Converts <paramref name="entity"/> into a <see cref="ResourceObject"/>.
32-
/// Adds the attributes and relationships that are enlisted in <paramref name="attributes"/> and <paramref name="relationships"/>
33-
/// </summary>
34-
/// <param name="entity">Entity to build a Resource Object for</param>
35-
/// <param name="attributes">Attributes to include in the building process</param>
36-
/// <param name="relationships">Relationships to include in the building process</param>
37-
/// <returns>The resource object that was built</returns>
27+
/// <inheritdoc/>
3828
public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> attributes = null, IEnumerable<RelationshipAttribute> relationships = null)
3929
{
4030
var resourceContext = _provider.GetContextEntity(entity.GetType());
@@ -53,30 +43,6 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> att
5343
return ro;
5444
}
5545

56-
private void ProcessRelationships(IIdentifiable entity, IEnumerable<RelationshipAttribute> relationships, ResourceObject ro)
57-
{
58-
foreach (var rel in relationships)
59-
{
60-
var relData = GetRelationshipData(rel, entity);
61-
if (relData != null)
62-
(ro.Relationships = ro.Relationships ?? new Dictionary<string, RelationshipEntry>()).Add(rel.PublicRelationshipName, relData);
63-
}
64-
}
65-
66-
private void ProcessAttributes(IIdentifiable entity, IEnumerable<AttrAttribute> attributes, ResourceObject ro)
67-
{
68-
ro.Attributes = new Dictionary<string, object>();
69-
foreach (var attr in attributes)
70-
AddAttribute(entity, ro, attr);
71-
}
72-
73-
private void AddAttribute(IIdentifiable entity, ResourceObject ro, AttrAttribute attr)
74-
{
75-
var value = attr.GetValue(entity);
76-
if (!(value == default && _settings.OmitDefaultValuedAttributes) && !(value == null && _settings.OmitDefaultValuedAttributes))
77-
ro.Attributes.Add(attr.PublicAttributeName, value);
78-
}
79-
8046
/// <summary>
8147
/// Builds the <see cref="RelationshipEntry"/> entries of the "relationships
8248
/// objects" The default behaviour is to just construct a resource linkage
@@ -89,6 +55,9 @@ protected virtual RelationshipEntry GetRelationshipData(RelationshipAttribute re
8955
return new RelationshipEntry { Data = GetRelatedResourceLinkage(relationship, entity) };
9056
}
9157

58+
/// <summary>
59+
/// Gets the value for the <see cref="RelationshipEntry.Data"/> property.
60+
/// </summary>
9261
protected object GetRelatedResourceLinkage(RelationshipAttribute relationship, IIdentifiable entity)
9362
{
9463
if (relationship is HasOneAttribute hasOne)
@@ -150,5 +119,32 @@ private bool IsRequiredToOneRelationship(HasOneAttribute attr, IIdentifiable ent
150119

151120
return false;
152121
}
122+
123+
/// <summary>
124+
/// Puts the relationships of the entity into the resource object.
125+
/// </summary>
126+
private void ProcessRelationships(IIdentifiable entity, IEnumerable<RelationshipAttribute> relationships, ResourceObject ro)
127+
{
128+
foreach (var rel in relationships)
129+
{
130+
var relData = GetRelationshipData(rel, entity);
131+
if (relData != null)
132+
(ro.Relationships = ro.Relationships ?? new Dictionary<string, RelationshipEntry>()).Add(rel.PublicRelationshipName, relData);
133+
}
134+
}
135+
136+
/// <summary>
137+
/// Puts the attributes of the entity into the resource object.
138+
/// </summary>
139+
private void ProcessAttributes(IIdentifiable entity, IEnumerable<AttrAttribute> attributes, ResourceObject ro)
140+
{
141+
ro.Attributes = new Dictionary<string, object>();
142+
foreach (var attr in attributes)
143+
{
144+
var value = attr.GetValue(entity);
145+
if (!(value == default && _settings.OmitDefaultValuedAttributes) && !(value == null && _settings.OmitDefaultValuedAttributes))
146+
ro.Attributes.Add(attr.PublicAttributeName, value);
147+
}
148+
}
153149
}
154150
}

src/JsonApiDotNetCore/Serialization/Server/IResponseResourceObjectBuilder.cs

-4
This file was deleted.

test/JsonApiDotNetCoreExampleTests/TestStartup.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using JsonApiDotNetCore.Extensions;
12
using JsonApiDotNetCore.Services;
23
using JsonApiDotNetCoreExample;
34
using Microsoft.AspNetCore.Hosting;
@@ -15,6 +16,7 @@ public TestStartup(IHostingEnvironment env) : base(env)
1516
public override IServiceProvider ConfigureServices(IServiceCollection services)
1617
{
1718
base.ConfigureServices(services);
19+
services.AddClientSerialization();
1820
services.AddScoped<IScopedServiceProvider, TestScopedServiceProvider>();
1921
return services.BuildServiceProvider();
2022
}

0 commit comments

Comments
 (0)