Skip to content

Fix: crash when deserializing post with relationship to abstract base class #828

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
wants to merge 3 commits into from
Closed
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 @@ -114,6 +114,7 @@ private async Task UpdateManyToManyAsync(IIdentifiable parent, HasManyThroughAtt

var newLinks = relationshipIds.Select(x =>
{
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. This one seems a bit more difficult to resolve

var link = _resourceFactory.CreateInstance(relationship.ThroughType);
relationship.LeftIdProperty.SetValue(link, TypeHelper.ConvertType(parentId, relationship.LeftIdProperty.PropertyType));
relationship.RightIdProperty.SetValue(link, TypeHelper.ConvertType(x, relationship.RightIdProperty.PropertyType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public override void SetValue(object resource, object newValue, IResourceFactory
List<object> throughResources = new List<object>();
foreach (IIdentifiable identifiable in (IEnumerable)newValue)
{
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one

object throughResource = resourceFactory.CreateInstance(ThroughType);
LeftProperty.SetValue(throughResource, resource);
RightProperty.SetValue(throughResource, identifiable);
Expand Down
18 changes: 12 additions & 6 deletions src/JsonApiDotNetCore/Serialization/BaseDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,19 @@ private void SetHasOneRelationship(IIdentifiable resource,
var rio = (ResourceIdentifierObject)relationshipData.Data;
var relatedId = rio?.Id;

var resourceContext = relationshipData.SingleData == null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified?

var relatedResourceType = relationshipData.SingleData == null
    ? attr.RightType
    : ResourceContextProvider.GetResourceContext(relationshipData.SingleData.Type).ResourceType;

// this does not make sense in the following case: if we're setting the dependent of a one-to-one relationship, IdentifiablePropertyName should be null.
var foreignKeyProperty = resourceProperties.FirstOrDefault(p => p.Name == attr.IdentifiablePropertyName);

if (foreignKeyProperty != null)
    // there is a FK from the current resource pointing to the related object,
    // i.e. we're populating the relationship from the dependent side.
    SetForeignKey(resource, foreignKeyProperty, attr, relatedId, relatedResourceType);

SetNavigation(resource, attr, relatedId, relatedResourceType);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: isn't ResourceContextProvider.GetResourceContext(relationshipData.SingleData.Type).ResourceType; always just equal to relationshipData.SingleData.Type? Because relationshipData.SingleData.Type is parsed from the request body, so if I'm not mistaken this will never equal the abstract base type.

? ResourceContextProvider.GetResourceContext(attr.RightType)
: ResourceContextProvider.GetResourceContext(relationshipData.SingleData.Type);

// this does not make sense in the following case: if we're setting the dependent of a one-to-one relationship, IdentifiablePropertyName should be null.
var foreignKeyProperty = resourceProperties.FirstOrDefault(p => p.Name == attr.IdentifiablePropertyName);

if (foreignKeyProperty != null)
// there is a FK from the current resource pointing to the related object,
// i.e. we're populating the relationship from the dependent side.
SetForeignKey(resource, foreignKeyProperty, attr, relatedId);
SetForeignKey(resource, foreignKeyProperty, attr, relatedId, resourceContext.ResourceType);

SetNavigation(resource, attr, relatedId);
SetNavigation(resource, attr, relatedId, resourceContext.ResourceType);

// depending on if this base parser is used client-side or server-side,
// different additional processing per field needs to be executed.
Expand All @@ -187,7 +191,7 @@ private void SetHasOneRelationship(IIdentifiable resource,
/// Sets the dependent side of a HasOne relationship, which means that a
/// foreign key also will to be populated.
/// </summary>
private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasOneAttribute attr, string id)
private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasOneAttribute attr, string id, Type relationshipType)
{
bool foreignKeyPropertyIsNullableType = Nullable.GetUnderlyingType(foreignKey.PropertyType) != null
|| foreignKey.PropertyType == typeof(string);
Expand All @@ -198,23 +202,24 @@ private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasO
throw new FormatException($"Cannot set required relationship identifier '{attr.IdentifiablePropertyName}' to null because it is a non-nullable type.");
}

var typedId = TypeHelper.ConvertStringIdToTypedId(attr.Property.PropertyType, id, ResourceFactory);
var typedId = TypeHelper.ConvertStringIdToTypedId(relationshipType /* was: attr.Property.PropertyType */, id, ResourceFactory);
foreignKey.SetValue(resource, typedId);
}

/// <summary>
/// Sets the principal side of a HasOne relationship, which means no
/// foreign key is involved.
/// </summary>
private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string relatedId)
private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string relatedId,
Type relationshipType)
{
if (relatedId == null)
{
attr.SetValue(resource, null, ResourceFactory);
}
else
{
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(attr.RightType);
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(relationshipType /* was: attr.RightType */);
relatedInstance.StringId = relatedId;
attr.SetValue(resource, relatedInstance, ResourceFactory);
}
Expand All @@ -232,6 +237,7 @@ private void SetHasManyRelationship(
{ // if the relationship is set to null, no need to set the navigation property to null: this is the default value.
var relatedResources = relationshipData.ManyData.Select(rio =>
{
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is valid. I think we'll have to introduce the equivalent approach as for SetHasOneRelationship

var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(attr.RightType);
relatedInstance.StringId = rio.Id;
return relatedInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected override void AfterProcessField(IIdentifiable resource, ResourceFieldA
/// </summary>
private IIdentifiable ParseIncludedRelationship(RelationshipAttribute relationshipAttr, ResourceIdentifierObject relatedResourceIdentifier)
{
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(relationshipAttr.RightType);
relatedInstance.StringId = relatedResourceIdentifier.Id;

Expand Down