Compiler Warning ASPIREEXPORT016
このコンテンツはまだ日本語訳がありません。
DTO property '0' is a get-only mutable collection. Add an init accessor so System.Text.Json replaces the collection during DTO deserialization; otherwise collection values can be merged with initializer defaults.
This diagnostic warning is reported when an [AspireDto] type exposes a mutable collection property with only a getter. During DTO deserialization, System.Text.Json can populate the existing collection instead of replacing it. If the collection has initializer defaults, deserialized values can be merged with those defaults and produce unexpected DTO state.
Example
Section titled “Example”The following code generates ASPIREEXPORT016:
[AspireDto]public class MyDto{ public List<string> Items { get; } = new();}To correct this warning
Section titled “To correct this warning”Add an init accessor so System.Text.Json can replace the mutable collection during deserialization:
[AspireDto]public class MyDto{ public List<string> Items { get; init; } = new();}Suppress the warning
Section titled “Suppress the warning”Suppress the warning with either of the following methods:
-
Set the severity of the rule in the .editorconfig file.
.editorconfig [*.{cs,vb}]dotnet_diagnostic.ASPIREEXPORT016.severity = noneFor more information about editor config files, see Configuration files for code analysis rules.
-
Add the following
PropertyGroupto your project file:C# project file <PropertyGroup><NoWarn>$(NoWarn);ASPIREEXPORT016</NoWarn></PropertyGroup>