Skip to content

Fix INJ0008/INJ0012 to fire for inferred open-generic registrations - #64

Merged
viceroypenguin merged 3 commits into
mainfrom
copilot/fix-open-generic-checks
Jul 28, 2026
Merged

Fix INJ0008/INJ0012 to fire for inferred open-generic registrations#64
viceroypenguin merged 3 commits into
mainfrom
copilot/fix-open-generic-checks

Conversation

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

INJ0008 and INJ0012 only fired when ServiceType = typeof(IService<>) was written explicitly. A generic class registered via RegistrationStrategy = ImplementedInterfaces (or an assembly-wide default strategy) produces the same open-generic descriptor without spelling out the service type — those cases silently dropped the registration with no diagnostic.

Changes

  • RegisterTypeAnalyzer.cs — In AnalyzeRegisterType0Attribute, adds an else if (containerSymbol.IsGenericType) branch for the serviceType is null case: fires INJ0008 when UseProxyFactory = true, INJ0012 when Factory = ... is set. Covers all strategies (Self, ImplementedInterfaces, SelfAndImplementedInterfaces) since any registration strategy on a generic class produces open-generic service types.

  • RegisterTypeAnalyzerTests.INJ0008.cs / INJ0012.cs — New tests for the per-attribute and assembly-default inferred cases.

Example — previously no diagnostic

// Assembly default strategy
[assembly: RegistrationDefaults(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces)]

// INJ0008 now fires; previously silently dropped
[RegisterSingleton(UseProxyFactory = true)]
public class Service<T> : IService<T> { }

// INJ0012 now fires; previously silently dropped
[RegisterSingleton(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces, Factory = nameof(Build))]
public class Service<T> : IService<T>
{
    public static Service<T> Build(IServiceProvider sp) => new();
}

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced diagnostics for unsupported open-generic registrations, including cases where UseProxyFactory or factory methods are specified but the registration is open-generic.
    • Improved reporting consistency for both explicitly configured and assembly-default registration strategies, preserving diagnostic details.
  • Tests
    • Expanded analyzer test coverage for open-generic proxy registration (UseProxyFactory = true) and open-generic factory method scenarios.
    • Added assertions for the expected diagnostic codes and nested diagnostic payloads.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4d06fbb9-b5b9-4f2a-951f-ea27f2a0ffd5

📥 Commits

Reviewing files that changed from the base of the PR and between e4dbc52 and 32f2677.

📒 Files selected for processing (2)
  • tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs
  • tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0012.cs

📝 Walkthrough

Walkthrough

The analyzer validates UseProxyFactory and Factory for inferred open-generic registrations. Tests cover explicit and assembly-level registration strategies across Self, ImplementedInterfaces, and SelfAndImplementedInterfaces.

Changes

Open-generic registration validation

Layer / File(s) Summary
Inferred open-generic option checks
src/Immediate.Injections.Analyzers/RegisterTypeAnalyzer.cs
AnalyzeRegisterType0Attribute reports diagnostics for UseProxyFactory and Factory when a generic container has no explicit service type.
Analyzer diagnostic coverage
tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs, tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0012.cs
Tests cover explicit and assembly-default Self, ImplementedInterfaces, and SelfAndImplementedInterfaces strategies for inferred open-generic registrations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the key fix: inferred open-generic registrations now trigger INJ0008/INJ0012.
Linked Issues check ✅ Passed The code and tests address the linked issue by detecting inferred open-generic registrations for both proxy and factory diagnostics.
Out of Scope Changes check ✅ Passed The PR stays focused on open-generic analyzer fixes and corresponding tests, with no unrelated changes evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch copilot/fix-open-generic-checks

Comment @coderabbitai help to get the list of available commands.

Copilot AI changed the title [WIP] Fix open-generic checks to report inferred registrations Fix INJ0008/INJ0012 to fire for inferred open-generic registrations Jul 28, 2026
Copilot AI requested a review from viceroypenguin July 28, 2026 14:20
@viceroypenguin

Copy link
Copy Markdown
Member

@copilot based on your analysis via this fix, is it possible for either case to exist without a matching INJ0002 being reported?

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot based on your analysis via this fix, is it possible for either case to exist without a matching INJ0002 being reported?

No — both INJ0008 and INJ0012 always co-occur with INJ0002. Every path that fires either diagnostic also sets valid = false, which causes AnalyzeRegisterType0Attribute to return false, and AnalyzeSymbol reports INJ0002 for any false return. This holds for the original explicit-ServiceType cases and the new inferred cases added by this fix, which is consistent with the nested diagnostic markers already used in all related tests (e.g., {|INJ0008:{|INJ0002:...|}|}).

@viceroypenguin
viceroypenguin marked this pull request as ready for review July 28, 2026 14:25
@viceroypenguin

Copy link
Copy Markdown
Member

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs`:
- Around line 20-31: Extend RegisterTypeAnalyzerTests with coverage for inferred
Self and SelfAndImplementedInterfaces strategies, mirroring the existing
open-generic proxy-factory test. Add cases using explicit strategies and
assembly-default configuration variants, and assert each still reports INJ0008
alongside the existing registration diagnostic.
- Around line 20-46: Complete the inferred registration-strategy test matrix by
adding Self and SelfAndImplementedInterfaces cases alongside the existing
ImplementedInterfaces cases in
tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs
lines 20-46, covering both per-attribute and assembly-default strategies; add
the corresponding inferred factory-validation cases in
tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0012.cs
lines 25-61, preserving the existing analyzer test structure and expected
diagnostics.

In
`@tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0012.cs`:
- Around line 25-41: Expand the RegisterTypeAnalyzer tests around
FactoryMethodWithInferredOpenGenericStrategyTriggers to cover inferred
RegistrationStrategy.Self and RegistrationStrategy.SelfAndImplementedInterfaces,
including their assembly-default variants. Keep the existing generic factory
setup and assertions, ensuring each case still reports INJ0012 rather than
losing the diagnostic during strategy resolution.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c48da6f9-5eb0-411a-a7a1-1f45f0ad8a02

📥 Commits

Reviewing files that changed from the base of the PR and between ae65ca6 and e4dbc52.

📒 Files selected for processing (3)
  • src/Immediate.Injections.Analyzers/RegisterTypeAnalyzer.cs
  • tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs
  • tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0012.cs

Comment on lines +20 to +31
[Fact]
public async Task UseProxyFactoryWithInferredOpenGenericStrategyTriggers() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<RegisterTypeAnalyzer>(
"""
using Immediate.Injections.Shared;

public interface IService<T>;

[{|INJ0008:{|INJ0002:RegisterSingleton(RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces, UseProxyFactory = true)|}|}]
public class Class<T> : IService<T>;
"""
).RunAsync(TestContext.Current.CancellationToken);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Cover the remaining inferred registration strategies.

Both new tests use RegistrationStrategy.ImplementedInterfaces; add inferred Self and SelfAndImplementedInterfaces cases, including assembly-default variants, so regressions in strategy resolution cannot silently lose INJ0008.

Also applies to: 33-46

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@tests/Immediate.Injections.Tests/AnalyzerTests/RegisterTypeAnalyzerTests.INJ0008.cs`
around lines 20 - 31, Extend RegisterTypeAnalyzerTests with coverage for
inferred Self and SelfAndImplementedInterfaces strategies, mirroring the
existing open-generic proxy-factory test. Add cases using explicit strategies
and assembly-default configuration variants, and assert each still reports
INJ0008 alongside the existing registration diagnostic.

@viceroypenguin

Copy link
Copy Markdown
Member

@copilot per review comments, please add test cases for Self and SelfAndImplementedInterfaces

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot per review comments, please add test cases for Self and SelfAndImplementedInterfaces

Done in the latest commit. Added tests for all three strategies (per-attribute and assembly-default) for both INJ0008 and INJ0012. Note that Self + generic + UseProxyFactory = true fires both INJ0007 and INJ0008 simultaneously (self-proxy and open-generic proxy are both violations), so those two tests expect both diagnostics.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 30372396405

Coverage increased (+0.06%) to 96.217%

Details

  • Coverage increased (+0.06%) from the base build.
  • Patch coverage: 19 of 19 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1269
Covered Lines: 1221
Line Coverage: 96.22%
Coverage Strength: 3.85 hits per line

💛 - Coveralls

@viceroypenguin
viceroypenguin merged commit c17c28f into main Jul 28, 2026
3 checks passed
@viceroypenguin
viceroypenguin deleted the copilot/fix-open-generic-checks branch July 28, 2026 15:22
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.

Factory and proxy open-generic checks miss inferred registrations

3 participants