Skip to content

Respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields in JsonSchemaExporter - #132179

Draft
steveisok with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-jsonschemaexporter-options
Draft

Respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields in JsonSchemaExporter#132179
steveisok with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-jsonschemaexporter-options

Conversation

Copilot AI commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

JsonSchemaExporter emitted read-only members that the serializer never writes when IgnoreReadOnlyProperties/IgnoreReadOnlyFields are enabled, so the schema disagreed with the actual serialized payload.

var options = new JsonSerializerOptions(JsonSerializerOptions.Default) { IgnoreReadOnlyProperties = true };

JsonSerializer.Serialize(new C(), options);       // {}
options.GetJsonSchemaAsNode(typeof(C));           // before: { "type": [...], "properties": { "S": ... } }
                                                  // after:  { "type": [...] }
class C { public string S => "Hello"; }

Changes

  • JsonSchemaExporter: the property filter previously skipped only members with both Get and Set null. It now uses the flags the serializer itself computes:

    if (property is { CanSerialize: false, CanDeserializeOrPopulate: false } or { IsExtensionData: true })

    CanSerialize/CanDeserializeOrPopulate already account for the ignore-read-only policy, [JsonIgnore], and ShouldSerialize overrides, so no new option plumbing is needed. Read-only collection properties are unaffected — they remain serializable and stay in the schema.

  • Tests: shared TestData entries for a POCO with read-only properties, with and without IgnoreReadOnlyProperties (exercised under both reflection and source gen), plus a reflection-only theory for IgnoreReadOnlyFields. Fields are kept out of the shared data because source gen drops non-[JsonInclude] fields at generation time and gives [JsonInclude] readonly fields an UnsafeAccessor setter, making them non-read-only at runtime.

Copilot AI lite review requested due to automatic review settings August 11, 2026 23:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
16 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.

Co-authored-by: steveisok <471438+steveisok@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 11, 2026 23:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

Suppressed comments (1)

src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs:851

  • The comment says read-only members are omitted when IgnoreReadOnlyProperties/IgnoreReadOnlyFields are enabled, but this test still expects the read-only collection property to be present. Please clarify that only members actually ignored by those settings (e.g., non-collection read-only properties/fields) are omitted, while collection properties can still be serialized/populated.
            // Read-only members are omitted when IgnoreReadOnlyProperties/IgnoreReadOnlyFields are enabled.
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Co-authored-by: steveisok <471438+steveisok@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 11, 2026 23:44
Copilot AI changed the title [WIP] Fix JsonSchemaExporter to respect IgnoreReadOnlyProperties and IgnoreReadOnlyFields Respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields in JsonSchemaExporter Aug 11, 2026
Copilot AI requested a review from steveisok August 11, 2026 23:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

Suppressed comments (2)

src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs:859

  • ReadOnlyCollectionProperty is declared as a non-nullable List<int>, so the schema for the property should not include "null" in its type union. This expected JSON currently marks it as nullable.
                    "properties": {
                        "Name": { "type": ["string", "null"] },
                        "ReadOnlyCollectionProperty": { "type": ["array", "null"], "items": { "type": "integer" } }
                    }

src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs:844

  • ReadOnlyCollectionProperty is declared as a non-nullable List<int>, so the schema for the property should not include "null" in its type union. In this file, non-nullable collection properties (e.g. PocoWithRecursiveCollectionElement.Children) are expected to emit "type": "array" without null.

This issue also appears on line 856 of the same file.

                        "Name": { "type": ["string", "null"] },
                        "ReadOnlyProperty": { "type": "integer" },
                        "ReadOnlyCollectionProperty": { "type": ["array", "null"], "items": { "type": "integer" } }
                    }
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonSchemaExporter doesn't respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields

3 participants