dotnet / dotnet/aspnetcore

OpenAPI Specification generation adds null value to enum based on parameter order

Open
#65,017 12 comments 1 reaction 1 assignee Claimed by @Youssef1313 View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Depending on the order of parameters in the MinimalAPI `.MapGet` a null value gets added as valid enum value in the OpenAPI Specification.

```c#
app.MapGet("/query", (XYZ xyz, XYZ? optionalXyz) => "TEST");

public enum XYZ { X, Y, Z }
```

This results in the generation of:
```json
"components": {
"schemas": {
"XYZ": {
"enum": [
"X",
"Y",
"Z",
null
]
}
}
}
```

swapping the order of parameters:

```c#
app.MapGet("/query", (XYZ? optionalXyz, XYZ xyz) => "TEST");

public enum XYZ { X, Y, Z }
```

generates the enum schema without null:

```json
"components": {
"schemas": {
"XYZ": {
"enum": [
"X",
"Y",
"Z"
]
}
}
}
```

### Expected Behavior

The ordering of the parameters/properties should not have an impact on what type of enum is generated in the open api document.

The `null` value in the enum should not be added.

### Steps To Reproduce

Here is the repository I used to reproduce the bug:
https://github.com/stopinet/net10-openapi-null-enum-bug

Running `dotnet build` generates the OpenAPI json

### Exceptions (if any)

_No response_

### .NET Version

10.0.101

### Anything else?

.NET SDK:
Version: 10.0.101
Commit: fad253f51b
Workload version: 10.0.100-manifests.c57ac48b
MSBuild version: 18.0.6+fad253f51

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.101\

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.1
Architecture: x64
Commit: fad253f51b

.NET SDKs installed:
9.0.306 [C:\Program Files\dotnet\sdk]
9.0.308 [C:\Program Files\dotnet\sdk]
10.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
None

Environment variables:
DOTNET_CLI_UI_LANGUAGE [en]

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

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.