Skip to content

Restructured eager loading tests and fixed two bugs #882

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 3 commits into from
Nov 19, 2020
Merged

Restructured eager loading tests and fixed two bugs #882

merged 3 commits into from
Nov 19, 2020

Conversation

bart-degreed
Copy link
Contributor

@bart-degreed bart-degreed commented Nov 18, 2020

Restructured eager loading tests and fixed two bugs:

  1. During update, eager loads without sparse fieldsets were not loaded at all places, which would make the resource change tracker incorrectly detect changes and return 200 instead of 204.
  2. Creating a resource with a required EagerLoad (not exposed as relationship) was not possible, as there was no place for user code to create the related entity. Fixed this by making the create logic more similar to update logic. The change tracker scans through the initially created resource, invoking all property getters, enabling them to create related entities on the fly. See usage in Building.cs.

1] During update, eager loads without parse fieldsets were not loaded at all places, which would make the resource change tracker incorrectly detect changes and return 200 instead of 204.
2] Creating a resource with a required EagerLoad (not exposed as relationship) was not possible, as there was no place for user code to create the related entity. Fixed this by making the create logic more similar to update logic. The change tracker scans through the initally created resource, invoking all property getters, enabling them to create related entities on the fly. See usage in Building.cs.
@bart-degreed bart-degreed requested a review from maurei November 18, 2020 14:59
Bart Koelman added 2 commits November 18, 2020 17:55
Yesterday I was trying to solve the wrong problem. The problem is not how to prevent a crash when creating a new resource that has a required EagerLoad relationship. Instead, the problem is that api developers need a way to assign relationships (EagerLoads or not) on resource creation. Whether such a relationship is required or not is irrelevant, it depends on business logic which relationships must be assigned. And that may be an already-existing entity or a new one. If the goal is to assign an already-existing entity, the api developer needs an async method to load that entity and assign it to the resource being created.

This commit adds `IResourceWriteRepository.GetForCreateAsync`, which is analog to `GetForUpdateAsync`. This makes the resource creation logic in resource service very similar to update.

This also fixes the tests, where [NotMapped] was missing on calculated properties. Except for request body deserialization, there is no more need to handle nulls in calculated properties that access a required related EagerLoad entity. And by treating the deserialized resource object just as an input source (which is never saved), we get by with just storing the deserialized data for later use, knowing it will be read and copied into the resource instance we're going to save.

Also reordered repository members in a more logical order (used to be verb-based).
@maurei maurei merged commit 7a8d3eb into json-api-dotnet:master Nov 19, 2020
@bart-degreed bart-degreed deleted the fix-eagerloads branch November 19, 2020 13: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.

2 participants