Skip to content

develop → master: operations #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static void AddJsonApiInternals(

services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
services.AddScoped(typeof(IResourceService<,>), typeof(EntityResourceService<,>));

services.AddSingleton<JsonApiOptions>(jsonApiOptions);
services.AddSingleton<IContextGraph>(jsonApiOptions.ContextGraph);
services.AddScoped<IJsonApiContext, JsonApiContext>();
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.3.4</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<TargetFrameworks>$(NetStandardVersion)</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
16 changes: 12 additions & 4 deletions src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

Expand All @@ -9,16 +10,23 @@ namespace JsonApiDotNetCore.Middleware
public class RequestMiddleware
{
private readonly RequestDelegate _next;

public RequestMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
public async Task Invoke(HttpContext context, IJsonApiContext jsonApiContext)
{
if (IsValid(context))
{
// HACK: this currently results in allocation of
// objects that may or may not be used and even double allocation
// since the JsonApiContext is using field initializers
// Need to work on finding a better solution.
jsonApiContext.BeginOperation();
await _next(context);
}
}

private static bool IsValid(HttpContext context)
Expand Down Expand Up @@ -58,11 +66,11 @@ internal static bool ContainsMediaTypeParameters(string mediaType)
var incomingMediaTypeSpan = mediaType.AsSpan();

// if the content type is not application/vnd.api+json then continue on
if(incomingMediaTypeSpan.Length < Constants.ContentType.Length)
if (incomingMediaTypeSpan.Length < Constants.ContentType.Length)
return false;

var incomingContentType = incomingMediaTypeSpan.Slice(0, Constants.ContentType.Length);
if(incomingContentType.SequenceEqual(Constants.ContentType.AsSpan()) == false)
if (incomingContentType.SequenceEqual(Constants.ContentType.AsSpan()) == false)
return false;

// anything appended to "application/vnd.api+json;" will be considered a media type param
Expand Down
10 changes: 5 additions & 5 deletions src/JsonApiDotNetCore/Request/HasManyRelationshipPointers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using JsonApiDotNetCore.Models;

namespace JsonApiDotNetCore.Request
{
Expand Down Expand Up @@ -32,18 +32,18 @@ namespace JsonApiDotNetCore.Request
/// </summary>
public class HasManyRelationshipPointers
{
private Dictionary<Type, IList> _hasManyRelationships = new Dictionary<Type, IList>();
private Dictionary<RelationshipAttribute, IList> _hasManyRelationships = new Dictionary<RelationshipAttribute, IList>();

/// <summary>
/// Add the relationship to the list of relationships that should be
/// set in the repository layer.
/// </summary>
public void Add(Type dependentType, IList entities)
=> _hasManyRelationships[dependentType] = entities;
public void Add(RelationshipAttribute relationship, IList entities)
=> _hasManyRelationships[relationship] = entities;

/// <summary>
/// Get all the models that should be associated
/// </summary>
public Dictionary<Type, IList> Get() => _hasManyRelationships;
public Dictionary<RelationshipAttribute, IList> Get() => _hasManyRelationships;
}
}
9 changes: 4 additions & 5 deletions src/JsonApiDotNetCore/Request/HasOneRelationshipPointers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JsonApiDotNetCore.Models;
using System;
using System.Collections.Generic;

namespace JsonApiDotNetCore.Request
Expand Down Expand Up @@ -29,18 +28,18 @@ namespace JsonApiDotNetCore.Request
/// </summary>
public class HasOneRelationshipPointers
{
private Dictionary<Type, IIdentifiable> _hasOneRelationships = new Dictionary<Type, IIdentifiable>();
private Dictionary<RelationshipAttribute, IIdentifiable> _hasOneRelationships = new Dictionary<RelationshipAttribute, IIdentifiable>();

/// <summary>
/// Add the relationship to the list of relationships that should be
/// set in the repository layer.
/// </summary>
public void Add(Type dependentType, IIdentifiable entity)
=> _hasOneRelationships[dependentType] = entity;
public void Add(RelationshipAttribute relationship, IIdentifiable entity)
=> _hasOneRelationships[relationship] = entity;

/// <summary>
/// Get all the models that should be associated
/// </summary>
public Dictionary<Type, IIdentifiable> Get() => _hasOneRelationships;
public Dictionary<RelationshipAttribute, IIdentifiable> Get() => _hasOneRelationships;
}
}
16 changes: 8 additions & 8 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public object DocumentToObject(DocumentData data, List<DocumentData> included =
+ "If you have manually registered the resource, check that the call to AddResource correctly sets the public name."); ;

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

entity = SetEntityAttributes(entity, contextEntity, data.Attributes);
entity = SetRelationships(entity, contextEntity, data.Relationships, included);

Expand All @@ -141,7 +141,7 @@ private object SetEntityAttributes(
{
if (attributeValues == null || attributeValues.Count == 0)
return entity;

foreach (var attr in contextEntity.Attributes)
{
if (attributeValues.TryGetValue(attr.PublicAttributeName, out object newValue))
Expand Down Expand Up @@ -174,7 +174,7 @@ private object DeserializeComplexType(JContainer obj, Type targetType)
private object SetRelationships(
object entity,
ContextEntity contextEntity,
Dictionary<string, RelationshipData> relationships,
Dictionary<string, RelationshipData> relationships,
List<DocumentData> included = null)
{
if (relationships == null || relationships.Count == 0)
Expand Down Expand Up @@ -203,7 +203,7 @@ private object SetHasOneRelationship(object entity,

if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData) == false)
return entity;

var relationshipAttr = _jsonApiContext.RequestEntity.Relationships
.SingleOrDefault(r => r.PublicRelationshipName == relationshipName);

Expand Down Expand Up @@ -234,7 +234,7 @@ private object SetHasOneRelationship(object entity,
foreignKeyProperty.SetValue(entity, convertedValue);


if(rio != null
if (rio != null
// if the resource identifier is null, there should be no reason to instantiate an instance
&& rio.Id != null)
{
Expand All @@ -247,7 +247,7 @@ private object SetHasOneRelationship(object entity,
// we need to store the fact that this relationship was included in the payload
// for EF, the repository will use these pointers to make ensure we don't try to
// create resources if they already exist, we just need to create the relationship
_jsonApiContext.HasOneRelationshipPointers.Add(attr.Type, includedRelationshipObject);
_jsonApiContext.HasOneRelationshipPointers.Add(attr, includedRelationshipObject);
}

return entity;
Expand Down Expand Up @@ -278,7 +278,7 @@ private object SetHasManyRelationship(object entity,

attr.SetValue(entity, convertedCollection);

_jsonApiContext.HasManyRelationshipPointers.Add(attr.Type, convertedCollection);
_jsonApiContext.HasManyRelationshipPointers.Add(attr, convertedCollection);
}

return entity;
Expand All @@ -301,7 +301,7 @@ private IIdentifiable GetIncludedRelationship(ResourceIdentifierObject relatedRe
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(relationshipAttr.Type);
if (contextEntity == null)
throw new JsonApiException(400, $"Included type '{relationshipAttr.Type}' is not a registered json:api resource.");

SetEntityAttributes(relatedInstance, contextEntity, includedResource.Attributes);

return relatedInstance;
Expand Down
7 changes: 7 additions & 0 deletions src/JsonApiDotNetCore/Services/IJsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,12 @@ public interface IJsonApiContext : IJsonApiRequest

[Obsolete("Use the proxied method IControllerContext.GetControllerAttribute instead.")]
TAttribute GetControllerAttribute<TAttribute>() where TAttribute : Attribute;

/// <summary>
/// **_Experimental_**: do not use. It is likely to change in the future.
///
/// Resets operational state information.
/// </summary>
void BeginOperation();
}
}
18 changes: 14 additions & 4 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public JsonApiContext(
public PageManager PageManager { get; set; }
public IMetaBuilder MetaBuilder { get; set; }
public IGenericProcessorFactory GenericProcessorFactory { get; set; }
public Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; } = new Dictionary<AttrAttribute, object>();
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; } = new Dictionary<RelationshipAttribute, object>();
public Type ControllerType { get; set; }
public Dictionary<string, object> DocumentMeta { get; set; }
public bool IsBulkOperationRequest { get; set; }
public HasManyRelationshipPointers HasManyRelationshipPointers { get; } = new HasManyRelationshipPointers();
public HasOneRelationshipPointers HasOneRelationshipPointers { get; } = new HasOneRelationshipPointers();

public Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; } = new Dictionary<AttrAttribute, object>();
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; } = new Dictionary<RelationshipAttribute, object>();
public HasManyRelationshipPointers HasManyRelationshipPointers { get; private set; } = new HasManyRelationshipPointers();
public HasOneRelationshipPointers HasOneRelationshipPointers { get; private set; } = new HasOneRelationshipPointers();

public IJsonApiContext ApplyContext<T>(object controller)
{
Expand Down Expand Up @@ -132,5 +133,14 @@ private PageManager GetPageManager()
[Obsolete("Use the proxied method IControllerContext.GetControllerAttribute instead.")]
public TAttribute GetControllerAttribute<TAttribute>() where TAttribute : Attribute
=> _controllerContext.GetControllerAttribute<TAttribute>();

public void BeginOperation()
{
IncludedRelationships = new List<string>();
AttributesToUpdate = new Dictionary<AttrAttribute, object>();
RelationshipsToUpdate = new Dictionary<RelationshipAttribute, object>();
HasManyRelationshipPointers = new HasManyRelationshipPointers();
HasOneRelationshipPointers = new HasOneRelationshipPointers();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Models.Operations;
using JsonApiDotNetCore.Services.Operations.Processors;
Expand Down Expand Up @@ -90,6 +91,9 @@ public IOpProcessor LocateUpdateService(Operation operation)
var resource = operation.GetResourceTypeName();

var contextEntity = _context.ContextGraph.GetContextEntity(resource);
if (contextEntity == null)
throw new JsonApiException(400, $"This API does not expose a resource of type '{resource}'.");

var processor = _processorFactory.GetProcessor<IOpProcessor>(
typeof(IUpdateOpProcessor<,>), contextEntity.EntityType, contextEntity.IdentityType
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ public class OperationsProcessor : IOperationsProcessor
{
private readonly IOperationProcessorResolver _processorResolver;
private readonly DbContext _dbContext;
private readonly IJsonApiContext _jsonApiContext;

public OperationsProcessor(
IOperationProcessorResolver processorResolver,
IDbContextResolver dbContextResolver)
IDbContextResolver dbContextResolver,
IJsonApiContext jsonApiContext)
{
_processorResolver = processorResolver;
_dbContext = dbContextResolver.GetContext();
_jsonApiContext = jsonApiContext;
}

public async Task<List<Operation>> ProcessAsync(List<Operation> inputOps)
Expand All @@ -40,6 +43,7 @@ public async Task<List<Operation>> ProcessAsync(List<Operation> inputOps)
{
foreach (var op in inputOps)
{
_jsonApiContext.BeginOperation();
lastAttemptedOperation = op.Op;
await ProcessOperation(op, outputOps);
opIndex++;
Expand Down Expand Up @@ -75,7 +79,8 @@ private async Task ProcessOperation(Operation op, List<Operation> outputOps)

private void ReplaceLocalIdsInResourceObject(ResourceObject resourceObject, List<Operation> outputOps)
{
if (resourceObject == null) return;
if (resourceObject == null)
return;

// it is strange to me that a top level resource object might use a lid.
// by not replacing it, we avoid a case where the first operation is an 'add' with an 'lid'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

namespace JsonApiDotNetCore.Services.Operations.Processors
{
public interface ICreateOpProcessor<T> : IOpProcessor
public interface ICreateOpProcessor<T> : ICreateOpProcessor<T, int>
where T : class, IIdentifiable<int>
{ }

public interface ICreateOpProcessor<T, TId> : IOpProcessor
where T : class, IIdentifiable<TId>
{ }

public class CreateOpProcessor<T> : CreateOpProcessor<T, int>
public class CreateOpProcessor<T>
: CreateOpProcessor<T, int>, ICreateOpProcessor<T>
where T : class, IIdentifiable<int>
{
public CreateOpProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace JsonApiDotNetCore.Services.Operations.Processors
/// Handles all "<see cref="OperationCode.get"/>" operations
/// </summary>
/// <typeparam name="T">The resource type</typeparam>
public interface IGetOpProcessor<T> : IOpProcessor
public interface IGetOpProcessor<T> : IGetOpProcessor<T, int>
where T : class, IIdentifiable<int>
{ }

Expand All @@ -27,7 +27,7 @@ public interface IGetOpProcessor<T, TId> : IOpProcessor
{ }

/// <inheritdoc />
public class GetOpProcessor<T> : GetOpProcessor<T, int>
public class GetOpProcessor<T> : GetOpProcessor<T, int>, IGetOpProcessor<T>
where T : class, IIdentifiable<int>
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace JsonApiDotNetCore.Services.Operations.Processors
{
public interface IRemoveOpProcessor<T> : IOpProcessor
public interface IRemoveOpProcessor<T> : IRemoveOpProcessor<T, int>
where T : class, IIdentifiable<int>
{ }

public interface IRemoveOpProcessor<T, TId> : IOpProcessor
where T : class, IIdentifiable<TId>
{ }

public class RemoveOpProcessor<T> : RemoveOpProcessor<T, int>
public class RemoveOpProcessor<T> : RemoveOpProcessor<T, int>, IRemoveOpProcessor<T>
where T : class, IIdentifiable<int>
{
public RemoveOpProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace JsonApiDotNetCore.Services.Operations.Processors
{
public interface IUpdateOpProcessor<T> : IOpProcessor
public interface IUpdateOpProcessor<T> : IUpdateOpProcessor<T, int>
where T : class, IIdentifiable<int>
{ }

public interface IUpdateOpProcessor<T, TId> : IOpProcessor
where T : class, IIdentifiable<TId>
{ }

public class UpdateOpProcessor<T> : UpdateOpProcessor<T, int>
public class UpdateOpProcessor<T> : UpdateOpProcessor<T, int>, IUpdateOpProcessor<T>
where T : class, IIdentifiable<int>
{
public UpdateOpProcessor(
Expand All @@ -27,7 +27,7 @@ IContextGraph contextGraph
{ }
}

public class UpdateOpProcessor<T, TId> : ICreateOpProcessor<T, TId>
public class UpdateOpProcessor<T, TId> : IUpdateOpProcessor<T, TId>
where T : class, IIdentifiable<TId>
{
private readonly IUpdateService<T, TId> _service;
Expand All @@ -53,16 +53,17 @@ public async Task<Operation> ProcessAsync(Operation operation)
throw new JsonApiException(400, "The data.id parameter is required for replace operations");

var model = (T)_deSerializer.DocumentToObject(operation.DataObject);

var result = await _service.UpdateAsync(model.Id, model);
if (result == null)
throw new JsonApiException(404, $"Could not find an instance of '{operation.DataObject.Type}' with id {operation.DataObject.Id}");

var operationResult = new Operation
{
Op = OperationCode.update
};

operationResult.Data = _documentBuilder.GetData(
_contextGraph.GetContextEntity(operation.GetResourceTypeName()),
result);
operationResult.Data = _documentBuilder.GetData(_contextGraph.GetContextEntity(operation.GetResourceTypeName()), result);

return operationResult;
}
Expand Down
Loading