Skip to content

Determine Future of RelationshipsToUpdate #331

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

Closed
Fireblade954 opened this issue Jul 5, 2018 · 4 comments
Closed

Determine Future of RelationshipsToUpdate #331

Fireblade954 opened this issue Jul 5, 2018 · 4 comments

Comments

@Fireblade954
Copy link

_jsonApiContext.RelationshipsToUpdate is not set for HasMany relations on PATCH requests?

in function SetHasOneRelationship _jsonApiContext.RelationshipsToUpdate is set.

_jsonApiContext.RelationshipsToUpdate[relationshipAttr] = convertedValue;

in function SetHasManyRelationship _jsonApiContext.RelationshipsToUpdate is NOT set.

is this by design? or forgotten?

@jaredcnance
Copy link
Contributor

The RelationshipsToUpdate dictionary is used to set the FK properties on HasOne relationships:

var newValue = rio?.Id ?? null;
var convertedValue = TypeHelper.ConvertType(newValue, foreignKeyProperty.PropertyType);
_jsonApiContext.RelationshipsToUpdate[relationshipAttr] = convertedValue;

In the case of HasMany, there are no FK properties to update since the HasMany attribute should exist on the independent side of the relationship. Instead, we track pointers to the relationship through HasManyRelationshipPointers/HasOneRelationshipPointers

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

protected virtual void AttachRelationships()
{
AttachHasManyPointers();
AttachHasOnePointers();
}
/// <summary>
/// This is used to allow creation of HasMany relationships when the
/// dependent side of the relationship already exists.
/// </summary>
private void AttachHasManyPointers()
{
var relationships = _jsonApiContext.HasManyRelationshipPointers.Get();
foreach(var relationship in relationships)
{
foreach(var pointer in relationship.Value)
{
_context.Entry(pointer).State = EntityState.Unchanged;
}
}
}

/// <summary>
/// Stores information to set relationships for the request resource.
/// These relationships must already exist and should not be re-created.
/// By default, it is the responsibility of the repository to use the
/// relationship pointers to persist the relationship.
///
/// The expected use case is POST-ing or PATCH-ing an entity with HasMany
/// relaitonships:
/// <code>
/// {
/// "data": {
/// "type": "photos",
/// "attributes": {
/// "title": "Ember Hamster",
/// "src": "http://example.com/images/productivity.png"
/// },
/// "relationships": {
/// "tags": {
/// "data": [
/// { "type": "tags", "id": "2" },
/// { "type": "tags", "id": "3" }
/// ]
/// }
/// }
/// }
/// }
/// </code>
/// </summary>
HasManyRelationshipPointers HasManyRelationshipPointers { get; }
/// <summary>
/// Stores information to set relationships for the request resource.
/// These relationships must already exist and should not be re-created.
///
/// The expected use case is POST-ing or PATCH-ing
/// an entity with HasOne relationships:
/// <code>
/// {
/// "data": {
/// "type": "photos",
/// "attributes": {
/// "title": "Ember Hamster",
/// "src": "http://example.com/images/productivity.png"
/// },
/// "relationships": {
/// "photographer": {
/// "data": { "type": "people", "id": "2" }
/// }
/// }
/// }
/// }
/// </code>
/// </summary>
HasOneRelationshipPointers HasOneRelationshipPointers { get; }

Perhaps the oversight is not deprecating the RelationshipsToUpdate dictionary. It looks like we should be able to use HasOneRelationshipPointers only.

This change was made in v2.3.2 via #314. Was this a breaking change for you?

@Fireblade954
Copy link
Author

Not necessarily a breaking change just did the assumption RelationshipsToUpdate contained all relations send in a PATCH request. We are not directly connected to the dbContext so i need to process the relations send in the patch request manually.

Ill use HasOneRelationshipPointers and HasManyRelationshipPointers to update my entities.

@jaredcnance jaredcnance changed the title RelationshipsToUpdate not set for HasMany relations? Determine Future of RelationshipsToUpdate Jul 5, 2018
@jaredcnance
Copy link
Contributor

@Fireblade954 I understand the confusion. I'm going to leave this issue open so we can track plans on improving this experience. I've also created json-api-dotnet/json-api-dotnet.github.io#6 to track documentation improvements.

@maurei
Copy link
Member

maurei commented Oct 10, 2019

Outdated

@maurei maurei closed this as completed Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants