Add MA0224 and MA0225 to require the Respect* options on JsonSerializerOptions - #1433
Merged
Merged
Conversation
…erOptions MA0222 and MA0223 only cover the [JsonSourceGenerationOptions] attribute of a JsonSerializerContext, so the options built at runtime with new JsonSerializerOptions() are left unchecked while they default to the same lenient behavior. MA0224 and MA0225 report the creation of a JsonSerializerOptions that configures neither RespectNullableAnnotations nor RespectRequiredConstructorParameters, whatever value the option is set to. The option is considered configured when it is set in the object initializer, or on the local the creation is assigned to. new JsonSerializerOptions(other) and JsonSerializerDefaults.Strict are not reported, as the options they start from are configured elsewhere. The diagnostic is reported on the creation without its object initializer, which can span many lines.
meziantou
enabled auto-merge (squash)
September 7, 2026 17:55
This was referenced Sep 7, 2026
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.228
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#565
Closed
This was referenced Sep 17, 2026
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Two new rules, disabled by default, that extend MA0222/MA0223 to the options built at runtime:
JsonSerializerOptionsshould setRespectNullableAnnotationsJsonSerializerOptionsshould setRespectRequiredConstructorParametersWhy
MA0222 and MA0223 only look at the
[JsonSourceGenerationOptions]attribute of aJsonSerializerContext. An application that serializes withnew JsonSerializerOptions()gets the same silent legacy behavior —nullread into a non-nullable member, and missing constructor parameters filled with their default value — with nothing reporting it. Like MA0222 and MA0223, the rules do not require a particular value: setting the option tofalseis a deliberate choice and satisfies them.How
JsonSerializerOptionsAnalyzerregisters an operation action onOperationKind.ObjectCreationand considers the option configured when:var o = new JsonSerializerOptions(); o.RespectNullableAnnotations = true;), including a local assigned after its declaration, andNot reported:
new JsonSerializerOptions(other)— the copy constructor starts from an instance configured elsewherenew JsonSerializerOptions(JsonSerializerDefaults.Strict)— sets both options (.NET 10)new JsonSerializerOptions(JsonSerializerDefaults.Web)andGeneralare reported.JsonSerializerOptionsFixeradds<Option> = trueto the object initializer, creating the initializer when the creation has none.Notes for the reviewer
{ ... }block is not squiggled:new JsonSerializerOptions,new(),new JsonSerializerOptions(...).new(ConfigureHttpJsonOptions,AddJsonOptionsin ASP.NET Core), and a local configured from another method, are not detected. A rule for the configure-lambda case could be a follow-up.Validation
Meziantou.Analyzer.Test.roslyn5.9suite: 4070/4070 passingdotnet run --project src/DocumentationGeneratorrun until it reported no change