dotnet / dotnet/aspnetcore

Custom JsonConverter causes OpenAPI request schema to be empty

Open
#63,051 4 comments 1 reaction 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

In a .NET 9 (also happens in .NET 10 preview) web app (minimal API), when implementing a custom JsonConverter for a request model, the generated OpenAPI spec contains an empty object for the request body schema. If the converter is not registered, the request body schema is defined correctly.

### Expected Behavior

The OpenAPI spec should contain a correctly defined schema for the request model in cases where a custom JsonConverter is defined.

### Steps To Reproduce

Simple repro project repository can be found [here](https://github.com/helo-email/open-api-converter-issue):. Running this app will generate the following OpenAPI spec (note the empty schema for `CreateChannelRequest`):
```
{
"openapi": "3.0.1",
"info": {
"title": "open-api-test | v1",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:5161/"
}
],
"paths": {
"/channels": {
"post": {
"tags": [
"open-api-test"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateChannelRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"CreateChannelRequest": { }
}
},
"tags": [
{
"name": "open-api-test"
}
]
}
```
Removing the JsonConverter registration results in the following (correct) schema for `CreateChannelRequest`:
```
{
"openapi": "3.0.1",
"info": {
"title": "open-api-test | v1",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:5161/"
}
],
"paths": {
"/channels": {
"post": {
"tags": [
"open-api-test"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateChannelRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"CreateChannelRequest": {
"required": [
"name",
"enabled"
],
"type": "object",
"properties": {
"name": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
}
}
}
},
"tags": [
{
"name": "open-api-test"
}
]
}
```

### Exceptions (if any)

_No response_

### .NET Version

9.0.7 (but also fails in 10.0.100-preview.6.25358.103)

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the linked simple repro project and run the minimal API with the custom JsonConverter registered and unregistered. Trace the OpenAPI schema generation for CreateChannelRequest and compare the two generated specifications. Done means the registered-converter case produces the same defined properties and required fields as the unregistered case, with regression coverage added in the relevant OpenAPI tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.