Unexpected nullability for get-only properties for json schema produced by STJ
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
### Description
When a property doesn't have a setter, its info will have `IsSetNullable` set to true, and then the JsonSchemaExporter will make the schema nullable.
### Reproduction Steps
```csharp
#nullable enable
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Schema;
Console.WriteLine(JsonSchemaExporter.GetJsonSchemaAsNode(JsonSerializerOptions.Default, typeof(C)));
class C
{
public IEnumerable Values => [];
public string SingleValueGetOnly { get; }
public string SingleValueGetSet { get; set; }
}
```
### Expected behavior
The `Values` and `SingleValueGetOnly` properties shouldn't be marked nullable.
### Actual behavior
```json
{
"type": [
"object",
"null"
],
"properties": {
"Values": {
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
},
"SingleValueGetOnly": {
"type": [
"string",
"null"
]
},
"SingleValueGetSet": {
"type": "string"
}
}
}
```
### Regression?
_No response_
### Known Workarounds
_No response_
### Configuration
_No response_
### Other information
I think the summary of where the behavior comes from is:
https://github.com/dotnet/dotnet/blob/df95cfb2142d47c72046c09dc049162e588342ed/src/runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs#L897-L898
https://github.com/dotnet/runtime/blob/3965f5ce78a5ff61b3a870031a0d8836f9db2848/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs#L448-L451
A property without a setter will have its info with `IsSetNullable = true` (the nullability info is reported as "unknown" - but `IsSetNullable` will consider unknown as if it's nullable)
Contributor guide
Assessment
This issue has not been assessed yet.