-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[WIP] Fix unexpected nullability in get-only properties for JSON schema #131603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f23bb31
c18be4c
1053f18
6d138ac
11fcebc
734fde8
de51772
1e8d64f
b1235a0
01771f6
f30973d
9eefdc8
1c8b9bc
50d7485
45d7d78
8b63d44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,30 @@ public void TreatNullObliviousAsNonNullable_True_MarksReferenceTypesAsNonNullabl | |
| Assert.Equal(expectedType, (string)schema["type"]!); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetOnlyProperties_DoNotUseSetterNullabilityInSchema() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: I need to make sure we have tests to exercise both the reflection path and the source generator path.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in the latest commit. |
||
| { | ||
| JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(typeof(PocoWithGetOnlyProperties)); | ||
| const string ExpectedJsonSchema = """ | ||
| { | ||
| "type": ["object", "null"], | ||
| "properties": { | ||
| "Values": { | ||
| "type": "array", | ||
| "items": { "type": ["string", "null"] } | ||
| }, | ||
| "SingleValueGetOnly": { "type": "string" }, | ||
| "NullableGetOnly": { "type": ["string", "null"] }, | ||
| "SingleValueGetSet": { "type": "string" }, | ||
| "NonNullableReadonlyField": { "type": "string" }, | ||
| "NullableReadonlyField": { "type": ["string", "null"] } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| AssertValidJsonSchema(typeof(PocoWithGetOnlyProperties), ExpectedJsonSchema, schema); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(typeof(Type))] | ||
| [InlineData(typeof(MethodInfo))] | ||
|
|
@@ -262,6 +286,18 @@ public void LegacySchemaExporter_CanAccessReflectedMembers() | |
|
|
||
| record PocoWithProperty(int Value); | ||
|
|
||
| public sealed class PocoWithGetOnlyProperties | ||
| { | ||
| public IEnumerable<string> Values => []; | ||
| public string SingleValueGetOnly { get; } = "value"; | ||
| public string? NullableGetOnly { get; } | ||
| public string SingleValueGetSet { get; set; } = "value"; | ||
| [JsonInclude] | ||
| public readonly string NonNullableReadonlyField = "value"; | ||
| [JsonInclude] | ||
| public readonly string? NullableReadonlyField; | ||
| } | ||
|
|
||
| [JsonSerializable(typeof(PocoWithProperty))] | ||
| partial class PocoWithPropertyContext : JsonSerializerContext; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot There existing tests that assert the full JSON output. This test should do the same. See
JsonSchemaExporterTests.TestTypes.csThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in
11fcebc2: the test now validates the full schema output (usingAssertValidJsonSchema) instead of checking individual nodes.