8000 Implement ResolverHierarchyAccess configuration by bradfol · Pull Request #567 · Swinject/Swinject · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement ResolverHierarchyAccess configuration #567

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bradfol
Copy link
Contributor
@bradfol bradfol commented May 29, 2024

There is currently the potential for unexpected behavior when resolving entries from parent containers where a parent registration can access and store objects resolved from a child container (see unit tests). This introduces a configuration to modify this access.

There is currently the potential for unexpected behavior when resolving entries from parent containers where a parent registration can access and store objects resolved from a child container (see unit tests). This introduces a configuration to modify this access.
@bradfol bradfol force-pushed the bradfol/container-capture branch from 8efecbe to da9ee0f Compare May 29, 2024 18:06
@bradfol bradfol changed the title Implement ContainerCapture configuration Implement ResolverHierarchyAccess configuration May 29, 2024
@bradfol bradfol marked this pull request as ready for review May 29, 2024 18:07
@@ -58,9 +71,10 @@ public final class Container {
parent: Container? = nil,
defaultObjectScope: ObjectScope = .graph,
behaviors: [Behavior] = [],
registeringClosure: (Container) -> Void = { _ in }
registeringClosure: (Container) -> Void = { _ in },
resolverHierarchyAccess: ResolverHierarchyAccess = .receiver
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left the default here as the current behavior, but it would be nice to discuss changing the default in a future version.

Comment on lines +120 to +122
XCTAssert(child.resolve(Animal.self, name: "Spot") is Cat)
// The instance provided by the child leaks into the parent
XCTAssert(parent.resolve(Animal.self, name: "Spot") is Cat)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also want to call attention to this test case as this behavior is the most unexpected/problematic of the way things currently work

@maxim-chipeev
Copy link
Contributor

Will look at this soon to see if I can merge this as part of a new release cut.

bradfol added a commit to cashapp/knit that referenced this pull request Jan 29, 2025
There is a potential bug with resolution when using parent-child containers and shadowed registrations. This fix ensures that when performing resolution that a parent container can never have access to registrations in a child container. Previously, the container that received the `resolve()` call was captured and used for later factory invocations, and if that receiver was a child that also had a shadowed registration of the parent then the bug could emerge.

This implements the same change as Swinject/Swinject#567 but does not retain an option to use the old behavior. Instead, we are considering the old Swinject behavior to be a bug, and are directly fixing for Knit.
@bradfol bradfol marked this pull request as draft January 31, 2025 23:47
@bradfol
Copy link
Contributor Author
bradfol commented Jan 31, 2025

When testing this change @skorulis-ap discovered that it does not work properly with the way that synchronized containers are set up. This is due to the part where the synchronized container contains no service entries, all entries are on the unsynchronized container, which means the modified getEntry() method will find the unsynchronized container when it does its look up, which is problematic.

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

Successfully merging this pull request may close these issues.

3 participants
0