Skip to content

Add MA0224 and MA0225 to require the Respect* options on JsonSerializerOptions - #1433

Merged
meziantou merged 1 commit into
mainfrom
feature/jsonserializeroptions-detection-35ef3b
Sep 7, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/jsonserializeroptions-detection-35ef3b

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

Two new rules, disabled by default, that extend MA0222/MA0223 to the options built at runtime:

  • MA0224JsonSerializerOptions should set RespectNullableAnnotations
  • MA0225JsonSerializerOptions should set RespectRequiredConstructorParameters

Why

MA0222 and MA0223 only look at the [JsonSourceGenerationOptions] attribute of a JsonSerializerContext. An application that serializes with new JsonSerializerOptions() gets the same silent legacy behavior — null read 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 to false is a deliberate choice and satisfies them.

How

JsonSerializerOptionsAnalyzer registers an operation action on OperationKind.ObjectCreation and considers the option configured when:

  • it is set in the object initializer, or
  • it is set on the local the creation is assigned to (var o = new JsonSerializerOptions(); o.RespectNullableAnnotations = true;), including a local assigned after its declaration, and

Not reported:

  • new JsonSerializerOptions(other) — the copy constructor starts from an instance configured elsewhere
  • new JsonSerializerOptions(JsonSerializerDefaults.Strict) — sets both options (.NET 10)
  • anything, when the target framework predates the properties (.NET 9), same gate as MA0222/MA0223

new JsonSerializerOptions(JsonSerializerDefaults.Web) and General are reported.

JsonSerializerOptionsFixer adds <Option> = true to the object initializer, creating the initializer when the creation has none.

Notes for the reviewer

  • The diagnostic is reported on the creation without its object initializer, so a long { ... } block is not squiggled: new JsonSerializerOptions, new(), new JsonSerializerOptions(...).
  • Documented limitations: options configured without a new (ConfigureHttpJsonOptions, AddJsonOptions in 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

  • New tests: 22, passing on all five Roslyn versions (4.8, 4.14, 5.0, 5.6, 5.9)
  • Full Meziantou.Analyzer.Test.roslyn5.9 suite: 4070/4070 passing
  • dotnet run --project src/DocumentationGenerator run until it reported no change

…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
meziantou enabled auto-merge (squash) September 7, 2026 17:55
@meziantou
meziantou merged commit e7091e0 into main Sep 7, 2026
13 checks passed
@meziantou
meziantou deleted the feature/jsonserializeroptions-detection-35ef3b branch September 7, 2026 17:57
This was referenced Sep 7, 2026
This was referenced Sep 17, 2026
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.

1 participant