Skip to content

Add support for booleans and floats in OpenAPI Schema const - #1086

Merged
dbanty merged 6 commits into
openapi-generators:mainfrom
flxdot:feat/support-bool-on-const
Aug 25, 2024
Merged

Add support for booleans and floats in OpenAPI Schema const#1086
dbanty merged 6 commits into
openapi-generators:mainfrom
flxdot:feat/support-bool-on-const

Conversation

@flxdot

Copy link
Copy Markdown
Contributor

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 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 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:

fromtypingimportLiteralimportuvicornfromfastapiimportFastAPIfrompydanticimportBaseModel, FieldclassResponseWithConstantBool(BaseModel):
message: str=Field(...)
is_welcome: Literal[True] =Field(True)
app=FastAPI()
@app.get("/", response_model=ResponseWithConstantBool)asyncdefroot():
return {"message": "Hello World"}
if__name__=="__main__":
uvicorn.run(app)

Generated OpenAPI Specification

{
"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"
}
}
}
}

@flxdotflxdot changed the title feat: Add support for booleans and floats in OpenAPI Schema constfix: Add support for booleans and floats in OpenAPI Schema constAug 1, 2024
@dbantydbanty changed the title fix: Add support for booleans and floats in OpenAPI Schema constAdd support for booleans and floats in OpenAPI Schema constAug 25, 2024

@dbantydbanty left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe OpenAPI 3.1 is what added support for const, but you're right, there's no reason it shouldn't support all the primitives. Thanks for the PR!

@dbanty
dbanty enabled auto-merge August 25, 2024 06:10
@dbanty
dbanty added this pull request to the merge queueAug 25, 2024
Merged via the queue into openapi-generators:main with commit e4128acAug 25, 2024
@knope-botknope-botBot mentioned this pull request Aug 25, 2024
github-merge-queueBot pushed a commit that referenced this pull request Aug 25, 2024
> [!IMPORTANT]
> Merging this pull request will create this release
## Fixes
### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum`
Fixed by PR #1092. Thanks @mikkelam!
### Add missing `cast` import when using `const`
Fixed by PR #1072. Thanks @dorcohe!
### Correctly resolve references to a type that is itself just a single
allOf reference
PR #1103 fixed issue #1091. Thanks @eli-bl!
### Support `const` booleans and floats
Fixed in PR #1086. Thanks @flxdot!
Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
micha91 pushed a commit to micha91/openapi-python-client that referenced this pull request May 13, 2025
…i-generators#1086)
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 <felix.fanghaenel@nitrex.com>
Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
Co-authored-by: Dylan Anthony <43723790+dbanty@users.noreply.github.com>
micha91 pushed a commit to micha91/openapi-python-client that referenced this pull request May 13, 2025
> [!IMPORTANT]
> Merging this pull request will create this release
## Fixes
### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum`
Fixed by PR openapi-generators#1092. Thanks @mikkelam!
### Add missing `cast` import when using `const`
Fixed by PR openapi-generators#1072. Thanks @dorcohe!
### Correctly resolve references to a type that is itself just a single
allOf reference
PR openapi-generators#1103 fixed issue openapi-generators#1091. Thanks @eli-bl!
### Support `const` booleans and floats
Fixed in PR openapi-generators#1086. Thanks @flxdot!
Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@flxdot@Dozer74@dbanty