microsoft / microsoft/typespec
Allow `explicit nullable: false` for openapi 3 types
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Clear and concise description of the problem
I have an explicit-nullability requirement for my OpenAPI schema emitted such that schema properties must explicitly specify if they allow null values. Currently if I create an optional or required type without null, the compiled schema does not explicitly set `nullable: false` which is allowed per [openapi 3 spec](https://swagger.io/docs/specification/v3_0/data-models/data-types/#null).
It would help if there were a toggle on the openapi emitter like `explicitNullability` that would make something like:
```
model Test {
requiredNotNullable: string;
optionalNotNullable?: string;
}
```
would compile to
```
openapi: 3.0.0
info:
title: (title)
version: 0.0.0
tags: []
paths: {}
components:
schemas:
Test:
type: object
nullable: false
required:
- requiredNotNullable
properties:
requiredNotNullable:
type: string
nullable: false
optionalNotNullable:
type: string
nullable: false
```
See in the [playground](https://cadlplayground.z22.web.core.windows.net/?e=%40typespec%2Fopenapi3&c=bW9kZWwgVGVzdCB7DQogIHJlcXVpcmVkTm90TnVsbGFibGU6IHN0cmluZzvEIG9wdGlvbmFsyyA%2FyyF9&options=%7B%7D&vs=%7B%7D)
### Checklist
- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
Assessment
This issue has not been assessed yet.