[release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers #64401
+673
−9
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.
Backport of #63872 to release/10.0
/cc @captainsafia @desjoerd
OpenAPI: Fix ModelMetadata null reference exception in emitted XMLComment transformers
Description
OpenAPI XML comment support walks
ParameterDescriptionsand previously assumed thatparameterDescription.ModelMetadatawas always non-null. For controller route parameters (including those coming from[Route]on the controller or inherited controllers),ModelMetadatacan benull, which caused aNullReferenceExceptionin the generated XML comment operation transformer while trying to inspectmetadata.MetadataKind.This change:
nullbefore checkingMetadataKindand other members:SupportsRouteParametersFromControllers) that verifies:[Route("[controller]")]and[HttpGet("{userId}")]does not throw during OpenAPI document generation.<summary>on the route parameter is correctly applied to the OpenAPI parameter description in the generated document.Fixes #64185
Customer Impact
Without this fix, apps that:
[Route]on the controller (common in versioned APIs), andAddOpenApi,will see OpenAPI document generation fail with a
NullReferenceExceptionwhen hitting their OpenAPI endpoint or UI. This prevents customers from using the built-in OpenAPI + XML comments experience in affected controller scenarios and can break development and diagnostics workflows that depend on the generated spec.With this fix:
ModelMetadataisnullfor a parameter.ModelMetadata.Regression?
This is a bug in the new XML documentation–backed OpenAPI support introduced in .NET 10; there wasn’t a previously working version of this feature for this specific scenario.
Risk
The change is limited to:
ModelMetadatain the generated XML comment transformers.If
ModelMetadatais non-null, behavior is unchanged. When it is null (previously a hard crash), the transformer now simply skips property-based XML comment lookup for that parameter, which is a safe and expected fallback. The change is localized to OpenAPI XML comment handling and does not affect other MVC or OpenAPI paths.Verification
Packaging changes reviewed?