dotnet / dotnet/runtime

JsonSchemaExporter doesn't respect IgnoreReadOnlyProperties/IgnoreReadOnlyFields

Open
#131,632 1 comment 1 reaction 2 assignees Claimed by @steveisok View on GitHub
area-System.Text.Json
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

The `IgnoreReadOnlyProperties` and `IgnoreReadOnlyFields` options are not taken into consideration by the `JsonSchemaExporter` today.

A property that is never serialized or deserialized is still emitting as part of the JSON schema.

### Reproduction Steps

```csharp
using System;
using System.Text.Json;
using System.Text.Json.Schema;

var options = new JsonSerializerOptions(JsonSerializerOptions.Default)
{
IgnoreReadOnlyProperties = true,
IgnoreReadOnlyFields = true,
};

Console.WriteLine("Serialized object:");
Console.WriteLine(JsonSerializer.Serialize(new C(), options));

Console.WriteLine();
Console.WriteLine("======");
Console.WriteLine();

Console.WriteLine("Json schema:");
Console.WriteLine(options.GetJsonSchemaAsNode(typeof(C)));

class C
{
public string S => "Hello";
}
```

### Expected behavior

Output is:

```
Serialized object:
{}

======

Json schema:
{
"type": [
"object",
"null"
]
}
```

### Actual behavior

Output is:

```
Serialized object:
{}

======

Json schema:
{
"type": [
"object",
"null"
],
"properties": {
"S": {
"type": [
"string",
"null"
]
}
}
}
```

### Regression?

_No response_

### Known Workarounds

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.