Skip to content

Remove reflection-based non-public method testing#1167

Merged
christianhelle merged 7 commits into
mainfrom
integration-tests
Jun 21, 2026
Merged

Remove reflection-based non-public method testing#1167
christianhelle merged 7 commits into
mainfrom
integration-tests

Conversation

@christianhelle

@christianhelle christianhelle commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary

Removes all tests that use .NET reflection (BindingFlags.NonPublic, RuntimeHelpers.GetUninitializedObject) to access non-public members in Refitter.Core. Replaces them with public-API integration tests or deletes where coverage is already provided.

Changes

Step 1: CustomCSharpTypeResolverTests.cs — Delete 94 redundant reflection tests

Removed IsValueType_Returns_True_For_All_Known_Mappings (47 args) and IsValueType_Returns_False_For_Reference_Types (47 args). Both tested a private static method directly. All three code paths are already covered by the existing public Resolve() tests in the same file.

Step 2: OpenApiDocumentFactoryMergeTests.cs — Delete 3 reflection tests + helper

Removed 3 tests that called MergeIfMissingOrThrowOnConflict via reflection. The same scenarios (equivalent duplicates, collisions, composed schemas) are already covered by tests using the public Merger.Merge() path.

Step 3: FormParameterExtractorTests.cs — Delete entire file

All 10 tests used RuntimeHelpers.GetUninitializedObject + BindingFlags to construct NSwag types. The form-data code paths are exercised by existing scenario integration tests.

Step 4: ParameterExtractorPrivateCoverageTests.cs — Delete reflection-dependent tests

Removed 15 tests that depended on reflection-based CreateParameterModel/CreateOperationModel helpers. Kept 20 tests that test ParameterShared utility methods via InternalsVisibleTo without reflection.

Step 5: IdentifierCorrectnessTests.cs — Replace with integration test

Removed reflection test that manually constructed CSharpParameterModel via RuntimeHelpers.GetUninitializedObject. Replaced with two integration tests using a real OpenAPI spec with parameter name filter#tag (NSwag doesn't sanitize #, triggering the same fallback).

Verification

  • Build: dotnet build -c Release — 0 warnings, 0 errors
  • Tests: dotnet test --project src/Refitter.Tests/Refitter.Tests.csproj -c Release — 2188 passed, 0 failed

Summary by CodeRabbit

  • Tests
    • Removed reflection-based testing and private-method invocation helpers across multiple test suites.
    • Deleted the FormParameterExtractorTests suite and trimmed other parameter and OpenAPI merge test coverage.
    • Updated identifier correctness coverage by replacing direct unit checks with generator-based async scenario tests for unsanitized parameter names, including compilation verification for both OpenAPI 3.0 and Swagger 2.0.

…TypeResolverTests

Delete IsValueType_Returns_True_For_All_Known_Mappings (47 args) and
IsValueType_Returns_False_For_Reference_Types (47 args). Both tested the
private static method directly via reflection. All code paths are already
covered by the existing public Resolve() tests in the same file.
Delete 3 tests that called the private MergeIfMissingOrThrowOnConflict
method via reflection. The same scenarios (equivalent duplicates, collisions,
composed schemas) are already covered by tests using the public InvokeMerge
helper which calls Merger.Merge() internally.
All 10 tests used RuntimeHelpers.GetUninitializedObject + BindingFlags to
construct NSwag type instances. The form-data code paths are exercised by
existing scenario integration tests (IdentifierCorrectnessTests,
MultiPartFormDataTests, FormDataParameterCasingTests).
…ivateCoverageTests

Delete 15 tests that used reflection-based CreateParameterModel/CreateOperationModel
helpers (RuntimeHelpers.GetUninitializedObject + BindingFlags) to construct NSwag types.
Also remove the helper methods and unused reflection imports.

Keep 20 tests that test ParameterShared utility methods directly via InternalsVisibleTo
using only NJsonSchema types and simple objects.
…ration test

Remove GetDefaultValueForParameter_Uses_Sanitized_Variable_Name_Fallback
which used RuntimeHelpers.GetUninitializedObject + BindingFlags to construct
NSwag types. Replace with two integration tests that exercise the same
fallback path via a real OpenAPI spec using parameter name 'filter#tag'
(NSwag doesn't sanitize #, so VariableName mismatch triggers fallback).

Also remove 5 unused imports (System.Reflection, System.Runtime.CompilerServices,
NJsonSchema, NSwag, NSwag.CodeGeneration.CSharp.Models).
@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

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

Run ID: 4b304bee-127c-411e-8c5b-1e4b7a8cb962

📥 Commits

Reviewing files that changed from the base of the PR and between fdafe69 and 5d9fbbb.

📒 Files selected for processing (1)
  • src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs

📝 Walkthrough

Walkthrough

Reflection-based test helpers and all test methods depending on them are deleted from three test files. In CustomCSharpTypeResolverTests, tests that reflected into non-public IsValueType are removed. In OpenApiDocumentFactoryMergeTests, the private InvokeMergeIfMissingOrThrowOnConflict helper and three Swagger2 merge tests are removed. In ParameterExtractorPrivateCoverageTests, reflection-based factory helpers and extensive private-coverage test cases are removed. In IdentifierCorrectnessTests, the reflection-based GetDefaultValueForParameter unit test is replaced with two async generator-invocation tests using a new unsanitized-parameter-name OpenAPI spec constant, with parallel OpenAPI 2.0 tests added.

Changes

Test Suite Reflection Removal and Integration Replacement

Layer / File(s) Summary
Remove CustomCSharpTypeResolverTests reflection tests
src/Refitter.Tests/CustomCSharpTypeResolverTests.cs
System.Reflection import and end-of-file test methods (IsValueType_Returns_True_For_All_Known_Mappings, IsValueType_Returns_False_For_Reference_Types) that reflected into non-public IsValueType are deleted.
Remove OpenApiDocumentFactoryMergeTests reflection helper and Swagger2 tests
src/Refitter.Tests/OpenApi/OpenApiDocumentFactoryMergeTests.cs
System.Reflection import, the private InvokeMergeIfMissingOrThrowOnConflict<TValue> helper, and three Swagger2-focused merge tests (EquivalentDefinitionDuplicate, DefinitionCollision, EquivalentComposedSchemaDuplicate) that depended on that reflection helper are deleted.
Remove ParameterExtractorPrivateCoverageTests reflection tests and helpers
src/Refitter.Tests/Parameters/ParameterExtractorPrivateCoverageTests.cs
System.Reflection and related using directives are removed; reflection-based factory helpers (CreateParameterModel, CreateOperationModel) and all dependent test cases (CSharpParameterModel aliasing tests, ParameterShared attribute/type accessors, QueryParameterExtractor behavior tests) are deleted.
Replace reflection-based unit test with generator-based integration tests
src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs
The reflection-based GetDefaultValueForParameter_Uses_Sanitized_Variable_Name_Fallback test is removed. A SpecWithUnsanitizedParameterName OpenAPI 3.0 spec constant is introduced with two async tests: one asserts the generator emits the sanitized identifier (filter_tag) and default value while excluding the raw filter#tag name; the other asserts the generated code compiles via BuildHelper.BuildCSharp. Equivalent OpenAPI 2.0 spec and tests are also added.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • christianhelle/refitter#1125: Introduces extractor-splitting refactor and OptionalParameterReorderer default-value handling; the updated IdentifierCorrectnessTests replaces direct ParameterShared.GetDefaultValueForParameter unit assertions with integration checks that verify generated code uses sanitized parameter identifiers.
  • christianhelle/refitter#1132: Adds FormParameterExtractorTests and reflection-based parameter-extraction test coverage that overlaps with the testing area this PR trims by removing reflection-dependent test cases.
  • christianhelle/refitter#1144: Refactors OpenApiDocumentFactory to use DocumentMerger/DocumentEquivalenceComparer; the OpenApiDocumentFactoryMergeTests.cs updates in this PR continue that refactor by removing reflection-based invocations of non-public merge conflict helpers.

Suggested labels

enhancement

🐇 No more reflection tricks in the night,
The mirror tests fade out of sight.
A spec with odd names now compiles clean,
filter_tag — the sharpest it's been!
From reflection to generators we hop with glee,
The test suite is lighter, as tests ought to be. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and concisely summarizes the main change: removing reflection-based tests that accessed non-public methods across multiple test files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 integration-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@christianhelle christianhelle self-assigned this Jun 21, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs`:
- Around line 643-675: The test only validates the unsanitized parameter name
scenario for OpenAPI 3.0, but the testing guidelines require coverage for both
OpenAPI 2.0 and 3.0 specifications. Add a Swagger 2.0 spec constant (similar to
SpecWithUnsanitizedParameterName but in Swagger 2.0 format with appropriate
parameter definition structure) and create a corresponding test method that
validates the same unsanitized parameter name behavior for Swagger 2.0,
including compile checks and assertions equivalent to the OpenAPI 3.0 test
variant.
🪄 Autofix (Beta)

✅ Autofix completed


ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c954b891-530e-4a80-be8f-997504a288ff

📥 Commits

Reviewing files that changed from the base of the PR and between ae7c7bd and 8610546.

📒 Files selected for processing (5)
  • src/Refitter.Tests/CustomCSharpTypeResolverTests.cs
  • src/Refitter.Tests/FormParameterExtractorTests.cs
  • src/Refitter.Tests/OpenApi/OpenApiDocumentFactoryMergeTests.cs
  • src/Refitter.Tests/Parameters/ParameterExtractorPrivateCoverageTests.cs
  • src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs
💤 Files with no reviewable changes (4)
  • src/Refitter.Tests/FormParameterExtractorTests.cs
  • src/Refitter.Tests/CustomCSharpTypeResolverTests.cs
  • src/Refitter.Tests/OpenApi/OpenApiDocumentFactoryMergeTests.cs
  • src/Refitter.Tests/Parameters/ParameterExtractorPrivateCoverageTests.cs

Comment thread src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs
@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.82%. Comparing base (753bd40) to head (5d9fbbb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1167      +/-   ##
==========================================
- Coverage   95.04%   94.82%   -0.23%     
==========================================
  Files          80       80              
  Lines        3553     3553              
==========================================
- Hits         3377     3369       -8     
- Misses         68       75       +7     
- Partials      108      109       +1     
Flag Coverage Δ
unittests 94.82% <ø> (-0.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@christianhelle christianhelle changed the title refactor: remove reflection-based non-public method testing Remove reflection-based non-public method testing Jun 21, 2026
@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

Fixes Applied Successfully

Fixed 1 file(s) based on 1 unresolved review comment.

Files modified:

  • src/Refitter.Tests/Scenarios/IdentifierCorrectnessTests.cs

Commit: 5d9fbbb534dd6682300b59afc2c8d4a17ba0cac6

The changes have been pushed to the integration-tests branch.

Time taken: 2m 21s

Fixed 1 file(s) based on 1 unresolved review comment.

Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
@christianhelle
christianhelle merged commit bb22079 into main Jun 21, 2026
10 of 12 checks passed
@christianhelle
christianhelle deleted the integration-tests branch June 21, 2026 12:23
@sonarqubecloud

Copy link
Copy Markdown

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