Skip to content

Fix OnReturn secondary resource pipeline for HasOne relationships #929

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 2 commits into from
Jan 27, 2021

Conversation

maurei
Copy link
Member

@maurei maurei commented Jan 21, 2021

Additional fix for #924 that covers the case of HasOne relationships

@maurei maurei requested a review from bart-degreed January 21, 2021 13:45
@woofi7
Copy link

woofi7 commented Jan 22, 2021

I've pulled the branch and tested on my side with the project.

When tested it crashes on the call to OnReturn.

The var resources is of type List<object> which is not TResource

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The type 'object' cannot be used as type parameter 'TResource' in the generic type or method 'JsonApiDotNetCore.Hooks.Internal.IOnReturnHookExecutor.OnReturn<TResource>(System.Collections.Generic.IEnumerable<TResource>, JsonApiDotNetCore.Hooks.Internal.Execution.ResourcePipeline)'. There is no implicit reference conversion from 'object' to 'JsonApiDotNetCore.Resources.IIdentifiable'.
  at CallSite.Target
  at System.Dynamic.UpdateDelegates.UpdateAndExecute3
  at JsonApiDotNetCore.Hooks.Internal.ResourceHookExecutorFacade.OnReturnRelationship
    D:\workspace\JsonApiDotNetCore\src\JsonApiDotNetCore\Hooks\Internal\ResourceHookExecutorFacade.cs:131
  at JsonApiDotNetCore.Services.JsonApiResourceService`2+<GetSecondaryAsync>d__11.MoveNext
    D:\workspace\JsonApiDotNetCore\src\JsonApiDotNetCore\Services\JsonApiResourceService.cs:137

I have tried with this and it works:

if (resourceOrResources is IIdentifiable)
{
    var resources = ToList((dynamic) resourceOrResources);
    object[] array = _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray();
    return array.SingleOrDefault();
}

This do not work, we need to store the result in a var and then do the SingleOrDefault.
_resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray().SingleOrDefault()

@woofi7
Copy link

woofi7 commented Jan 26, 2021

The fix work with the first problem (image loading).

@bart-degreed bart-degreed merged commit 3ee6e88 into master Jan 27, 2021
@bart-degreed bart-degreed deleted the hooks-hotfix-2 branch January 27, 2021 08:54
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.

3 participants