OpenAPI equivalent to Swagger's NonNullableReferenceTypesAsRequired
- 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
### Is your feature request related to a problem? Please describe the problem.
I have APIs that are mostly providing data to frontends written in TypeScript (mostly GETs). I generate TypeScript types from the OpenAPI specs. Properties that aren't nullable I'd expect to be modeled as required in the spec since the fields will always be present. When using Swashbuckle.AspNetCore's generator with its NonNullableReferenceTypesAsRequired option set to true that happened, but there's no equivalent setting using the Microsoft.OpenAPI generator here.
As an example, I'd like both properties here to be listed as required:
```csharp
public class Foo
{
public int Bar { get; set; }
public List MyList { get; set; } = [];
}
```
Presently both are optional, and additionally MyList is nullable (due to a different issue, documented in #58192)
### Describe the solution you'd like
It would be helpful to have an equivalent setting in OpenAPI to Swashbuckle.AspNetCore's NonNullableReferenceTypesAsRequired. See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036 for details about the problem and solution done there.
Of course an alternative solution is marking everything as required/[JsonRequired]/[Required], which I've done in some places, but that's a lot of work when you have a lot of existing DTOs.
### Additional context
_No response_
Contributor guide
Research direction
Start by locating the Microsoft.OpenAPI schema-generation entry point that determines required and nullable properties, then compare its behavior with Swashbuckle.AspNetCore's NonNullableReferenceTypesAsRequired implementation referenced in the issue. Done means an equivalent opt-in setting makes non-nullable properties such as Bar and MyList required without requiring annotations, with coverage in the relevant generator tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, openapi
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100