Skip to content

JsonIncludeAttribute should override global JsonSerializerOptions.IgnoreReadOnlyProperties configuration. #88716

@konrad-jamrozik

Description

@konrad-jamrozik

Description

Expected behavior: If a read-only property has [JsonInclude] attribute, it should get serialized via JsonSerializer.Serialize even if following serialization options are used:

new JsonSerializerOptions { IgnoreReadOnlyProperties = true })

but this is not the case.

Actual behavior: the property is ignored by serialization.

Note that [JsonIgnore] will correctly override options that have IgnoreReadOnlyProperties = false and ignore a property. Hence I would expect an analogous "mirrored" behavior from [JsonInclude].

As a side note, I was unable to find any documentation explaining the precedence of attributes vs serializer options.

Reproduction Steps

Given in this gist:

https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e

To repro, run JsonIncludeReproTests.JsonIgnoreOverridesOptionsButJsonIncludeDoesNot

Expected behavior

See the description above.

Actual behavior

See the description above.

Regression?

I don't know.

Known Workarounds

None

Configuration

.NET 8 preview. Details in the gist:

https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions