Commit e4128ac
Add support for booleans and floats in OpenAPI Schema
We encountered the problem that the Pydantic Schemas for the OpenAPI
spec, do not support the `const` keyword if it is of type `boolean`.
## Why do I think it should be supported?
The [OpenAPI
Specification](https://swagger.io/docs/specification/data-models/keywords/)
explicitly marks the `const` keyword as unsupported. Nevertheless it
should not result in exceptions if the keyword is present.
I furthermore did not find any evidence in [JSON
Schema](https://json-schema.org/understanding-json-schema/reference/const)
that would limit the types valid for the `const` keyword to be `string`
or `integer` only.
Thus I suggest allowing `boolean` as well as `floats` as constant
values.
## Example
The following is minimal example in FastAPI that would produce such a
schema:
```python
from typing import Literal
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field
class ResponseWithConstantBool(BaseModel):
message: str = Field(...)
is_welcome: Literal[True] = Field(True)
app = FastAPI()
@app.get("/", response_model=ResponseWithConstantBool)
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run(app)
```
### Generated OpenAPI Specification
```json
{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/": {
"get": {
"summary": "Root",
"operationId": "root__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ResponseWithConstantBool"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ResponseWithConstantBool": {
"properties": {
"message": {
"type": "string",
"title": "Message"
},
"is_welcome": {
"type": "boolean",
"enum": [true],
"const": true,
"title": "Is Welcome",
"default": true
}
},
"type": "object",
"required": [
"message"
],
"title": "ResponseWithConstantBool"
}
}
}
}
```
---------
Co-authored-by: Felix Fanghaenel <[email protected]>
Co-authored-by: Dylan Anthony <[email protected]>
Co-authored-by: Dylan Anthony <[email protected]>const (#1086)1 parent b20d240 commit e4128ac
File tree
4 files changed
+15
-3
lines changed- .changeset
- openapi_python_client
- parser/properties
- schema/openapi_schema_pydantic
- tests/test_schema
4 files changed
+15
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
15 | 20 | | |
16 | 21 | | |
17 | 22 | | |
| |||
0 commit comments