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

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

wants to merge 3 commits into from

Conversation

bart-degreed
Copy link
Contributor

I'm not sure if this is a good solution, but at least this removes the crash when trying to create an instance of abstract base class (see #696). There are likely more places we need to change.

@@ -168,15 +168,19 @@ private IIdentifiable ParseResourceObject(ResourceObject data)
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.

@@ -232,6 +237,7 @@ private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string
{ // 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

@@ -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

@@ -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

@@ -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

@maurei
Copy link
Member

maurei commented Sep 18, 2020

Superceded by #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants