Skip to content

[release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers#33

Open
tomerqodo wants to merge 5 commits intocoderabbit_full_base_release_100_fix_modelmetadata_null_reference_exception_in_emitted_xmlcomment_transformers_pr8from
coderabbit_full_head_release_100_fix_modelmetadata_null_reference_exception_in_emitted_xmlcomment_transformers_pr8
Open

[release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers#33
tomerqodo wants to merge 5 commits intocoderabbit_full_base_release_100_fix_modelmetadata_null_reference_exception_in_emitted_xmlcomment_transformers_pr8from
coderabbit_full_head_release_100_fix_modelmetadata_null_reference_exception_in_emitted_xmlcomment_transformers_pr8

Conversation

@tomerqodo
Copy link

@tomerqodo tomerqodo commented Jan 28, 2026

Benchmark PR from agentic-review-benchmarks#8

Summary by CodeRabbit

  • New Features

    • Added support for XML documentation comments on controller route parameters, which are now properly included in OpenAPI definitions.
  • Bug Fixes

    • Improved stability by adding null safety checks for parameter metadata throughout the documentation processing pipeline, preventing potential runtime errors when metadata is unavailable.

✏️ Tip: You can customize this high-level summary in your review settings.

desjoerd and others added 5 commits January 25, 2026 11:59
…ment transformers

When a route parameter does not have a bound model it would case a null reference exception because the ModelMetadata on the ParameterDescriptor was null.
This is fixed by adding a null reference check

Fixes dotnet#63757
As I think this was the encoding before my change
@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

Walkthrough

This PR introduces XML comment support for OpenAPI generation by adding a new test for route parameter documentation and applying defensive null-checks across multiple generated snapshot files to prevent null reference exceptions during metadata evaluation.

Changes

Cohort / File(s) Summary
New Test Method
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/OperationTests.Controllers.cs
Adds SupportsRouteParametersFromControllers() test to validate that XML documentation comments on controller action parameters are correctly bound to OpenAPI route parameter descriptions.
XML Comment Infrastructure
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsRouteParametersFromControllers#OpenApiXmlCommentSupport.generated.verified.cs
New generated file implementing XML comment integration for OpenAPI: includes XmlCommentCache, XmlCommentOperationTransformer, XmlCommentSchemaTransformer, DocumentationCommentIdHelper, and GeneratedServiceCollectionExtensions to wire XML summaries, remarks, and examples into OpenAPI definitions.
Metadata Null-Safety Enhancements
snapshots/AddOpenApiTests.CanInterceptAddOpenApi#OpenApiXmlCommentSupport.generated.verified.cs, snapshots/AdditionalTextsTests.CanHandleXmlForSchemasInAdditionalTexts#..., snapshots/CompletenessTests.SupportsAllXmlTagsOnSchemas#..., snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromControllers#..., snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromMinimalApis#..., snapshots/SchemaTests.SupportsXmlCommentsOnSchemas#..., snapshots/SchemaTests.XmlCommentsOnPropertiesShouldApplyToSchemaReferences#..., snapshots/XmlCommentDocumentationIdTests.CanMergeXmlCommentsWithDifferentDocumentationIdFormats#...
Adds null-checks to parameter metadata evaluation by changing conditions from metadata.MetadataKind == to metadata is not null && metadata.MetadataKind ==, preventing potential null reference exceptions across all snapshot test verifications.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A hop, skip, and jump through XML's embrace,
Route parameters find their proper place,
Null-checks like carrots, safe and secure,
OpenAPI comments forever pure! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: addressing a ModelMetadata null reference exception in emitted XMLComment transformers.

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

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89422f4 and 45b170b.

⛔ Files ignored due to path filters (1)
  • src/OpenApi/gen/XmlCommentGenerator.Emitter.cs is excluded by !**/gen/**
📒 Files selected for processing (10)
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/OperationTests.Controllers.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/AddOpenApiTests.CanInterceptAddOpenApi#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/AdditionalTextsTests.CanHandleXmlForSchemasInAdditionalTexts#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/CompletenessTests.SupportsAllXmlTagsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsRouteParametersFromControllers#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromControllers#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromMinimalApis#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.SupportsXmlCommentsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.XmlCommentsOnPropertiesShouldApplyToSchemaReferences#OpenApiXmlCommentSupport.generated.verified.cs
  • src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/XmlCommentDocumentationIdTests.CanMergeXmlCommentsWithDifferentDocumentationIdFormats#OpenApiXmlCommentSupport.generated.verified.cs
🔇 Additional comments (15)
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromMinimalApis#OpenApiXmlCommentSupport.generated.verified.cs (1)

477-480: LGTM! Null-check guard correctly prevents NullReferenceException.

The addition of metadata is not null && before accessing metadata.MetadataKind properly guards against the null reference exception that occurs when route parameters lack a bound model. The short-circuit evaluation ensures all subsequent property accesses are safe.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/CompletenessTests.SupportsAllXmlTagsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs (1)

556-559: LGTM! Consistent null-safety guard applied.

The null-check for metadata is not null before accessing MetadataKind is correctly applied, matching the fix pattern across all related snapshots.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/AdditionalTextsTests.CanHandleXmlForSchemasInAdditionalTexts#OpenApiXmlCommentSupport.generated.verified.cs (1)

458-461: LGTM! Defensive null-check properly guards metadata access.

The null-safety pattern is consistently applied to prevent potential crashes when ModelMetadata is null for route parameters without bound models.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsXmlCommentsOnOperationsFromControllers#OpenApiXmlCommentSupport.generated.verified.cs (1)

433-436: LGTM! Null-check correctly applied for controller-based operations.

The defensive guard ensures the transformer handles controller route parameters that may have null ModelMetadata without throwing exceptions.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.XmlCommentsOnPropertiesShouldApplyToSchemaReferences#OpenApiXmlCommentSupport.generated.verified.cs (1)

438-441: LGTM! Schema reference test snapshot correctly updated with null-safety guard.

The fix is consistently reflected in this schema-focused test snapshot.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/XmlCommentDocumentationIdTests.CanMergeXmlCommentsWithDifferentDocumentationIdFormats#OpenApiXmlCommentSupport.generated.verified.cs (1)

430-433: LGTM! Documentation ID merge test snapshot correctly reflects the null-safety fix.

The null-check guard is consistently applied across all snapshot files, ensuring the generated source code properly handles route parameters where ParameterDescriptor.ModelMetadata may be null.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.SupportsXmlCommentsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs (1)

458-462: Good null-guard before metadata dereference.

Prevents potential NREs while preserving the existing behavior for property metadata.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/OperationTests.Controllers.cs (1)

100-143: Nice regression coverage for route-parameter XML docs.

Covers the scenario the PR is targeting and asserts the parameter description is wired correctly.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsRouteParametersFromControllers#OpenApiXmlCommentSupport.generated.verified.cs (6)

70-75: Cache entry aligns with the new route-parameter test.

The userId parameter comment is captured as expected for the controller method.


80-341: Doc ID helper logic looks consistent for generated IDs.

No issues spotted in the helper methods for type/property/method IDs.


343-480: Operation transformer null-guard is in place.

The metadata check avoids null dereference while still applying parameter comments.


509-565: Schema transformer behavior looks good.

Applies property comments and examples consistently for inline vs referenced schemas.


567-594: Parse helper provides a safe fallback for non-JSON examples.

The quoted-string fallback path is a practical safeguard.


596-607: AddOpenApi wiring correctly registers the XML transformers.

The generated extension hooks both schema and operation transformers.

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/AddOpenApiTests.CanInterceptAddOpenApi#OpenApiXmlCommentSupport.generated.verified.cs (1)

428-431: Null-check prevents metadata dereference faults.

This keeps the parameter loop safe when ModelMetadata is absent.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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

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.

2 participants