You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example above, the object initializer for MyRecord, generated by the json source generation machinery, would override the initialized value of the MyOtherRecord property to null -- despite having a JsonIgnore applied to the property.
Reproduction Steps
Apply the JsonIgnore attribute to an init-only property and observe that the init is invoked upon object construction. As illustrated below, the behavior seems to diverge from reflection mode.
The JsonIgnore attribute should, when applied to an init-only property, cause the property initializer to be omitted entirely from object construction.
From the docs:
[JsonIgnore] Prevents a property or field from being serialized or deserialized.
Actual behavior
The init-only property is effectively behaving as if the ignore condition is set to JsonIgnoreCondition.Never, thus causing any value set in the constructor to subsequently be overridden.
Regression?
No response
Known Workarounds
Changing the accessor from init to set updates ObjectWithParameterizedConstructorCreator from static args => new global::JsonClass(){ Property = (int)args[0] } to static args => new global::JsonClass().
Configuration
.NET 8
SDK 8.0.204
Other information
No response
The text was updated successfully, but these errors were encountered:
Description
The
JsonIgnore
attribute is ignored for init-only properties in source generation mode.A motivating example is when I have an outer record wrapping some inner record to perform some serialization transformation.
In the example above, the object initializer for
MyRecord
, generated by the json source generation machinery, would override the initialized value of theMyOtherRecord
property tonull
-- despite having aJsonIgnore
applied to the property.Reproduction Steps
Apply the
JsonIgnore
attribute to an init-only property and observe that the init is invoked upon object construction. As illustrated below, the behavior seems to diverge from reflection mode.Expected behavior
The
JsonIgnore
attribute should, when applied to an init-only property, cause the property initializer to be omitted entirely from object construction.From the docs:
Actual behavior
The init-only property is effectively behaving as if the ignore condition is set to
JsonIgnoreCondition.Never
, thus causing any value set in the constructor to subsequently be overridden.Regression?
No response
Known Workarounds
Changing the accessor from
init
toset
updatesObjectWithParameterizedConstructorCreator
fromstatic args => new global::JsonClass(){ Property = (int)args[0] }
tostatic args => new global::JsonClass()
.Configuration
.NET 8
SDK 8.0.204
Other information
No response
The text was updated successfully, but these errors were encountered: