From 43210cd931134d619d765ac12acc58af723faffa Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 22 Jul 2026 09:34:18 +0200 Subject: [PATCH] Add additional test for STJ Schema deprecated property --- .../JsonSchemaExporterTests.TestTypes.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs index 18037517183e95..80991c45ebf03e 100644 --- a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs +++ b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs @@ -1230,6 +1230,57 @@ of the type which points to the first occurrence. */ "deprecated": true } """); + + // The same type used by both a deprecated and a non-deprecated property is + // duplicated rather than shared, so the "deprecated" keyword is only applied + // to the schema of the obsolete property. + yield return new TestData( + Value: new() { NotDeprecated = new(), Deprecated = new() }, + ExpectedJsonSchema: """ + { + "type": ["object","null"], + "properties": { + "NotDeprecated": { + "type": ["object","null"], + "properties": { "Value": { "type": "integer" } } + }, + "Deprecated": { + "type": ["object","null"], + "properties": { "Value": { "type": "integer" } }, + "deprecated": true + } + } + } + """); + + // A type shared via "$ref" across a deprecated and a non-deprecated collection + // property. The "deprecated" keyword is applied to the obsolete property and never + // leaks into the shared element definition referenced by both "$ref" occurrences. + yield return new TestData( + Value: new() { First = [], Deprecated = [], NotDeprecated = [] }, + ExpectedJsonSchema: """ + { + "type": ["object","null"], + "properties": { + "First": { + "type": ["array","null"], + "items": { + "type": ["object","null"], + "properties": { "Value": { "type": "integer" } } + } + }, + "Deprecated": { + "type": ["array","null"], + "items": { "$ref": "#/properties/First/items" }, + "deprecated": true + }, + "NotDeprecated": { + "type": ["array","null"], + "items": { "$ref": "#/properties/First/items" } + } + } + } + """); #pragma warning restore CS0612 // Type or member is obsolete // Collection types @@ -1699,6 +1750,29 @@ public sealed class MyInnerObsoleteType } } + public sealed class PocoWithObsoletePropertiesSharingType + { + public PocoSharedByObsoleteMembers? NotDeprecated { get; set; } + + [Obsolete] + public PocoSharedByObsoleteMembers? Deprecated { get; set; } + } + + public sealed class PocoWithObsoleteCollectionProperties + { + public List? First { get; set; } + + [Obsolete] + public List? Deprecated { get; set; } + + public List? NotDeprecated { get; set; } + } + + public sealed class PocoSharedByObsoleteMembers + { + public int Value { get; set; } + } + public record TestData( T? Value, string ExpectedJsonSchema,