Skip to content

Add documenting tests for KeyFilter + class interception limitation (#56)#65

Merged
tillig merged 1 commit into
developfrom
feature/issue-56
Jun 24, 2026
Merged

Add documenting tests for KeyFilter + class interception limitation (#56)#65
tillig merged 1 commit into
developfrom
feature/issue-56

Conversation

@tillig

@tillig tillig commented Jun 24, 2026

Copy link
Copy Markdown
Member

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 both WithAttributeFiltering() and EnableClassInterceptors().

After investigation, the root cause is an upstream Castle DynamicProxy bug, not something in this library or in Autofac core:

  • EnableClassInterceptors rewrites the implementation type to a Castle proxy subclass, replicating the original constructor parameter attributes onto the proxy constructor.
  • KeyFilterAttribute's constructor parameter is typed object (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.
  • The replicated KeyFilterAttribute.Key ends up as boxed (int)0 instead of e.g. LoggerKey.First, so core's WithAttributeFiltering looks up new 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 object losslessly, 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:

  • Pins the current (incorrect) behavior for an enum key with class interception. If Castle fixes the upstream bug, this test will start failing — signaling that the limitation (and its docs/comments) can be removed.
  • Establishes the baseline (enum filtering works without interception).
  • Demonstrates the two supported workarounds: use a string key, or use EnableInterfaceInterceptors instead of EnableClassInterceptors.

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

  • 46/46 tests pass on net8.0 and net10.0.
  • Clean Release build (warnings-as-errors + analyzers) and dotnet format clean.

No production code changes — tests and documentation only. Suggest leaving #56 open to track the upstream fix.

)

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

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (f79fa27) to head (1cfeacd).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tillig tillig merged commit e5c2102 into develop Jun 24, 2026
12 checks passed
@tillig tillig deleted the feature/issue-56 branch June 24, 2026 16:06
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.

1 participant