[WIP] Fix unexpected nullability in get-only properties for JSON schema - #131603
[WIP] Fix unexpected nullability in get-only properties for JSON schema#131603Youssef1313 with Copilot wants to merge 16 commits into
Conversation
|
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. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
|
@copilot Isn't it better that JsonPropertyInfo gets |
| } | ||
|
|
||
| [Fact] | ||
| public void GetOnlyProperties_DoNotUseSetterNullabilityInSchema() |
There was a problem hiding this comment.
@copilot There existing tests that assert the full JSON output. This test should do the same. See JsonSchemaExporterTests.TestTypes.cs
There was a problem hiding this comment.
Updated in 11fcebc2: the test now validates the full schema output (using AssertValidJsonSchema) instead of checking individual nodes.
Not in general. |
|
@copilot But |
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
…etNullable, and source gen coverage Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Done in the latest commit. The changes cover: JSON schema tests (
Because |
|
@copilot There are failing tests. Run all System.Text.Json tests and make sure they pass. |
- In DefaultJsonTypeInfoResolver.Helpers.cs (reflection path): guard IsSetNullable with SetMethod is not null for properties, and !IsInitOnly for fields. This ensures get-only/readonly members report IsSetNullable=false. - In JsonSourceGenerator.Emitter.cs (source gen path): add (property.PropertyType.CanBeNull && property.IsReadOnly) condition to emit IsSetNullable = false for get-only reference-type properties. - Update source gen baselines for ConstructorWithDefaultValues and UnsafeAccessors_InaccessibleConstructor to reflect new IsSetNullable = false emission for string get-only properties. - Add NullableAnnotationsTests for IsSetNullable/IsGetNullable on readonly members. - Add JsonSchemaExporterTests for get-only property schema correctness. - Add source gen context registrations for new test types. Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
| writer.WriteLine($"properties[{i}].IsGetNullable = false;"); | ||
| } | ||
| if (property.IsSetterNonNullableAnnotation || property.IsReadOnly) | ||
| if (property.IsSetterNonNullableAnnotation || (property.PropertyType.CanBeNull && property.IsReadOnly)) |
There was a problem hiding this comment.
@copilot Why is the property.PropertyType.CanBeNull check needed?
Shouldn't it be always valid to set IsSetNullable to false whenever we have a readonly property?
…perties in source gen Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
The concern was moot — As for the fix approach: I've now implemented the cleaner path you suggested. The source gen emitter now sets |
|
@eiriktsarpalis Would you be able to take a look at this PR please? I haven't reviewed Copilot changes in much detail. |
|
Replacing with #132271 for the time being. |
./build.sh clr+libs -rc release)