Add documenting tests for KeyFilter + class interception limitation (#56)#65
Merged
Conversation
) EnableClassInterceptors rewrites the implementation type to a Castle DynamicProxy subclass. Castle reproduces an enum argument passed to an attribute parameter typed as object (as KeyFilterAttribute(object key) is) using the enum's underlying integer type, so the replicated [KeyFilter] key no longer matches the registered keyed service and WithAttributeFiltering silently fails. This is an upstream Castle bug (castleproject/Core#748). Rather than a fragile workaround, add tests that document the behavior: - pin the current (incorrect) behavior with an enum key so we get a signal if Castle fixes it upstream - demonstrate the two supported workarounds: use a string key, or use interface interception instead of class interception Tracked by issue #56.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #65 +/- ##
========================================
Coverage 90.71% 90.71%
========================================
Files 2 2
Lines 140 140
Branches 23 23
========================================
Hits 127 127
Misses 6 6
Partials 7 7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to #56. Filed upstream as castleproject/Core#748.
Background
Issue #56 reports that
[KeyFilter]with an enum key is ignored when a type is registered with bothWithAttributeFiltering()andEnableClassInterceptors().After investigation, the root cause is an upstream Castle DynamicProxy bug, not something in this library or in Autofac core:
EnableClassInterceptorsrewrites the implementation type to a Castle proxy subclass, replicating the original constructor parameter attributes onto the proxy constructor.KeyFilterAttribute's constructor parameter is typedobject(KeyFilterAttribute(object key)), so an enum key is boxed. Castle reproduces that boxed enum argument using the enum's underlying integer type rather than the original enum type.KeyFilterAttribute.Keyends up as boxed(int)0instead of e.g.LoggerKey.First, so core'sWithAttributeFilteringlooks upnew KeyedService((int)0, typeof(string)), which doesn't match the registered keyed service. Filtering silently misses.This only affects non-string keys under class interception. String keys round-trip through
objectlosslessly, and interface interception does not rewrite the constructor, so both are unaffected.Filed upstream with a minimal, Autofac-free repro: castleproject/Core#748 (not fixed in the latest released Castle.Core 5.2.1, nor on their unreleased 6.0.0 branch).
This PR
Rather than ship a fragile re-injection workaround that duplicates core's filtering logic, this adds a documenting test fixture (
ClassInterceptorsWithAttributeFilteringFixture) that:EnableInterfaceInterceptorsinstead ofEnableClassInterceptors.The class-level XML doc captures the root cause and links both #56 and castleproject/Core#748 so the rationale travels with the code.
Validation
dotnet formatclean.No production code changes — tests and documentation only. Suggest leaving #56 open to track the upstream fix.