[release/10.0] Avoid referencing types of JsonIgnore'd properties. #120569
+220
−33
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 #120543 to release/10.0
/cc @jeffhandley @eiriktsarpalis
Customer Impact
The Microsoft.Extensions.AI and Agent Framework libraries use
[Experimental]attributes on APIs related to emerging AI concepts. We have found that System.Text.Json source-generated serialization code results references members and types that result in diagnostics--[Experimental],[Obsolete], and other such attributes can cause that behavior. Because the references are in generated code, suppressions cannot be applied at the call site--leading to the need for project-wide suppressions.Project-wide suppressions on these diagnostics hide the fact that the customer is exposing non-stable APIs on their serialization model. Before this change, applying
[JsonIgnore]to the types or members does not eliminate all references to the annotated APIs in the source-generated code, which is an unexpected behavior, and this still leaves the customer needing to apply project-wide suppressions.With those project-wide suppressions in place, we cannot be confident that
[Experimental]API names/shapes can be changed without catching consumers by surprise.A customer encountered this issue while implementing a community contribution that involved the
[Experimental]attribute in add support for background responses (dotnet/extensions#6854).Fix
If the property is ignored and its type is not used anywhere else in the type graph, emit a
JsonPropertyInfoof type 'object' to avoid unnecessarily referencing the type. STJ requires that all ignored properties be included so that it can perform necessary run-time validations using configuration not known at compile time such as the property naming policy and case sensitivity.Regression
Testing
New tests were added to exercise the scenarios of where we expect diagnostics to be avoided, and testing was added to ensure diagnostics are still raised when an API is not marked as
[JsonIgnore]. The tests illustrate the end-to-end AI scenarios are unblocked.Risk
Medium. Any changes to System.Text.Json incur risk of affecting serialization, and changes to the source generator carry additional risk. These risks are mitigated by this being a very surgical fix that did not affect any existing tests.