-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add JsonIgnore attribute support to Minimal API validation generator #63075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: captainsafia <[email protected]>
Co-authored-by: captainsafia <[email protected]>
@copilot Update the CanValidateComplexTypesWithJsonIgnore test with cases to cover record types. Use the existing test method and don't create a new one. |
Co-authored-by: captainsafia <[email protected]>
Updated the Commit: d038ce7 |
...nsGeneratorTests.CanValidateComplexTypesWithJsonIgnore#ValidatableInfoResolver.g.received.cs
Outdated
Show resolved
Hide resolved
- Remove IsJsonIgnoredParameter method since JsonIgnore can only be applied to properties and fields, not constructor parameters - Update record parameter validation to check corresponding property for JsonIgnore instead - Delete stale received snapshot file - Update verified snapshot to include record type validation results Co-authored-by: captainsafia <[email protected]>
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for respecting [JsonIgnore]
attributes in the Minimal API validation generator to prevent "Maximum validation depth exceeded" exceptions when processing types with circular references. The primary purpose is to exclude properties marked with [JsonIgnore]
from validation traversal, similar to how [FromServices]
attributes are already handled.
Key changes:
- Added
JsonIgnoreAttribute
to well-known types and created helper methods to detect it - Updated validation member extraction logic to skip properties with
[JsonIgnore]
attributes - Added comprehensive test coverage for both class and record types with JsonIgnore functionality
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
WellKnownTypeData.cs | Added System_Text_Json_Serialization_JsonIgnoreAttribute to well-known types |
RequiredSymbols.cs | Added JsonIgnoreAttribute symbol to required symbols record |
ITypeSymbolExtensions.cs | Added IsJsonIgnoredProperty extension method for detecting JsonIgnore attributes |
ValidationsGenerator.TypesParser.cs | Updated member extraction logic to skip JsonIgnore properties |
ValidationsGenerator.ComplexType.cs | Added comprehensive test coverage for JsonIgnore functionality |
ValidatableInfoResolver.g.verified.cs | Generated test snapshot showing correct exclusion of JsonIgnore properties |
@@ -148,6 +150,12 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb | |||
continue; | |||
} | |||
|
|||
// Skip properties that have JsonIgnore attribute | |||
if (correspondingProperty.IsJsonIgnoredProperty(jsonIgnoreAttributeSymbol)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a null check for jsonIgnoreAttributeSymbol
before calling the extension method to handle cases where the symbol might not be available.
Copilot uses AI. Check for mistakes.
@@ -186,6 +194,12 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb | |||
continue; | |||
} | |||
|
|||
// Skip properties that have JsonIgnore attribute | |||
if (member.IsJsonIgnoredProperty(jsonIgnoreAttributeSymbol)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a null check for jsonIgnoreAttributeSymbol
before calling the extension method to handle cases where the symbol might not be available.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Philosophically, do we want this change? We're slowly adding more Json specific features to the general purpose validation feature.
We also just added [SkipValidation]
which is what should be used if you don't want something to be validated.
{ | ||
return property.GetAttributes().Any(attr => | ||
attr.AttributeClass is not null && | ||
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, jsonIgnoreAttributeSymbol)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relevant for this change, but does this comparison work for derived types?
e.g. MyFromKeyedServicesAttribute : FromKeyedServicesAttribute
See code a few lines above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't, but JsonIgnoreAttribute
is sealed so there's not likely to be derived types. FromKeyedServices
is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we should update the keyed service check in a separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep -- we want to probably do an audit across the repo for anywhere we type check on FromKeyedServices
and FromServices
since both of those are unsealed.
The ship has sailed a bit on the source generator not having any STJ-specific logic in the generator since it already has logic for walking I know we were hard core about the philosophical distinction for the property naming policy but this one feels more necessary since being incompatible with the serialziation rules by default could mean that we validate properties that are never serialized/set. |
This PR fixes an issue where Minimal API validation would throw a "Maximum validation depth exceeded" exception when processing types like
JsonPatchDocument
that contain properties with circular references marked with[JsonIgnore]
attributes.Problem
When using the new
JsonPatchDocument<T>
for System.Text.Json in .NET 10 with Minimal API validation enabled, the validation generator would traverse all properties of the type, including those marked with[JsonIgnore]
. This caused infinite recursion through circular references in properties likeSerializerOptions.Default.Default...
, leading to:Solution
The fix adds support for recognizing and skipping properties and parameters marked with
[JsonIgnore]
attributes during validation type discovery, similar to how[FromServices]
attributes are already handled.Key changes:
System.Text.Json.Serialization.JsonIgnoreAttribute
to well-known typesIsJsonIgnoredProperty
andIsJsonIgnoredParameter
extension methodsExtractValidatableMembers
to skip properties/parameters with[JsonIgnore]
attributesExample
With this fix, the following endpoint will work correctly:
The validation generator will now properly exclude properties marked with
[JsonIgnore]
from validation traversal, preventing circular reference exceptions while still validating all other properties with validation attributes.Fixes #63047.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.