Skip to content

Produce deprecated property in JsonSchema for obsolete types#130665

Open
Youssef1313 wants to merge 3 commits into
mainfrom
dev/ygerges/deprecated-schema
Open

Produce deprecated property in JsonSchema for obsolete types#130665
Youssef1313 wants to merge 3 commits into
mainfrom
dev/ygerges/deprecated-schema

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

Copilot AI review requested due to automatic review settings July 14, 2026 06:41
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:41 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:41 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:43 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:43 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:44 — with GitHub Actions Inactive

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.

Pull request overview

This PR updates the System.Text.Json JSON Schema exporter to emit the JSON Schema deprecated keyword when exporting schemas for [Obsolete] .NET types, and adds a regression test covering the new behavior.

Changes:

  • Detect [Obsolete] on exported types and set deprecated: true in the generated schema.
  • Add Deprecated support to the internal JsonSchema model and include it in JSON output.
  • Add a unit test validating deprecated is emitted for an obsolete type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs Adds a test case validating deprecated is produced for an [Obsolete] type.
src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Marks exported schemas as deprecated when the associated .NET type has [Obsolete].
src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs Adds a Deprecated keyword to the schema model, counts it, and emits it during JSON serialization.

Copilot AI review requested due to automatic review settings July 14, 2026 06:50
@Youssef1313 Youssef1313 force-pushed the dev/ygerges/deprecated-schema branch from 2e895bf to a5d6c2d Compare July 14, 2026 06:50
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:50 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:50 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 force-pushed the dev/ygerges/deprecated-schema branch from a5d6c2d to 0460d8b Compare July 14, 2026 06:51
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:51 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:52 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 06:54 — with GitHub Actions Inactive

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Outdated
Comment thread src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs Outdated
Copilot AI review requested due to automatic review settings July 14, 2026 06:54
@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.

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Outdated
Copilot AI review requested due to automatic review settings July 14, 2026 07:01
@Youssef1313 Youssef1313 force-pushed the dev/ygerges/deprecated-schema branch from 0460d8b to 76a2de7 Compare July 14, 2026 07:01
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:01 — with GitHub Actions Inactive

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Outdated
Copilot AI review requested due to automatic review settings July 14, 2026 07:42
@Youssef1313 Youssef1313 force-pushed the dev/ygerges/deprecated-schema branch from 76a2de7 to 3c313d5 Compare July 14, 2026 07:42
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:42 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:43 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:44 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:45 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 07:45 — with GitHub Actions Inactive

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs Outdated
public int? MaxLength { get => _maxLength; set { VerifyMutable(); _maxLength = value; } }
private int? _maxLength;

public bool Deprecated { get => _deprecated; set { VerifyMutable(); _deprecated = value; } }

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.

Other keywords model both all possible values plus the absense of a value. Correspondingly, this should be bool? and we should include it in the generated schema document if non-null.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd argue it should only be serialized when explicitly set to true, and ignored otherwise, based on the keyword definition

Omitting this keyword has the same behavior as a value of false.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's an internal model. So we could make it nullable bool, but I think we will never set it to false. So it will either be true (serialized with value true) or will be null (not serialized at all).

This will be behaviourly identical to what I have now with it being a bool that's only serialized when true.

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.

I don't disagree, but that should be the decision of the mapping layer. The model itself should be neutral in representing all possible states.

Copilot AI review requested due to automatic review settings July 14, 2026 13:17
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 13:17 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 13:17 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 13:19 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 13:20 — with GitHub Actions Inactive
@Youssef1313 Youssef1313 temporarily deployed to copilot-pat-pool July 14, 2026 13:21 — with GitHub Actions Inactive

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines 481 to +502

private static bool HasObsoleteAttribute(ICustomAttributeProvider? attributeProvider)
{
if (attributeProvider is null)
{
return false;
}

// Identify ObsoleteAttribute using its full type name rather than typeof(ObsoleteAttribute).
// On downlevel targets System.Text.Json compiles in an internal ObsoleteAttribute polyfill
// that would otherwise shadow the framework type, causing the typeof comparison to never match
// the ObsoleteAttribute applied by user code.
foreach (object attribute in attributeProvider.GetCustomAttributes(inherit: true))
{
if (attribute.GetType().FullName == "System.ObsoleteAttribute")
{
return true;
}
}

return false;
}
AssertDeprecated(properties["MyString"], expectedDeprecated: false);

// Property annotated with [Obsolete] reports deprecated.
AssertDeprecated(properties["MyObsoleteString"], expectedDeprecated: true);

@eiriktsarpalis eiriktsarpalis Jul 14, 2026

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.

I would recommend having the test asserting the exact shape of the generated schema, rather than interrogating individual aspects of it. There's a built-in theory you can use to drive this comparison.


JsonSchema CompleteSchema(ref GenerationState state, JsonSchema schema)
{
if (HasObsoleteAttribute(typeInfo.Type) ||

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.

If the type itself is marked obsolete, wouldn't this result in every single contained property also being marked obsolete?

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.

4 participants