Skip to content

Respect IgnoreReadOnlyProperties and IgnoreReadOnlyFields in JsonSchemaExporter - #136878

Merged
eiriktsarpalis merged 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/stj-jsonschemaexporter-ignore-readonly
Nov 5, 2026
Merged

eiriktsarpalis merged 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/stj-jsonschemaexporter-ignore-readonly

Conversation

@Manish-1kumar

Copy link
Copy Markdown
Contributor

Fixes #131632

Description

When generating JSON schemas using JsonSchemaExporter, properties that are neither serialized nor deserialized (such as read-only properties when IgnoreReadOnlyProperties = true, or read-only fields when IgnoreReadOnlyFields = true) were previously still emitted into the schema because the filter was checking solely property is { Get: null, Set: null }.

Changes

  • Updated property filtering in JsonSchemaExporter to check { CanSerialize: false, CanDeserializeOrPopulate: false }.
    • Non-collection read-only properties are omitted when IgnoreReadOnlyProperties is enabled.
    • Read-only fields are omitted when IgnoreReadOnlyFields is enabled.
    • Read-only collection properties continue to be emitted since they are populated and serialized.
  • Added tests in JsonSchemaExporterTests_Reflection verifying that IgnoreReadOnlyProperties and IgnoreReadOnlyFields correctly omit ignored members.

…maExporter

When exporting JSON schemas for object types, skip properties that cannot be serialized and cannot be deserialized or populated (such as read-only properties when IgnoreReadOnlyProperties is enabled, or read-only fields when IgnoreReadOnlyFields is enabled).

Fixes dotnet#131632
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Cover a customizer that removes both the getter and setter (property stays
excluded from the schema and the serializer output) and a customizer that
removes only the setter (IgnoreReadOnlyProperties is respected).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonSchemaExporter doesn't respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields

2 participants