Skip to content

Commit e4128ac

Browse files
flxdotFelix Fanghaeneldbanty
authored
Add support for booleans and floats in OpenAPI Schema const (#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 <[email protected]> Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
1 parent b20d240 commit e4128ac

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Support `const` booleans and floats
6+
7+
Fixed in PR #1086. Thanks @flxdot!

openapi_python_client/parser/properties/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConstProperty(PropertyProtocol):
2727
def build(
2828
cls,
2929
*,
30-
const: str | int,
30+
const: str | int | float | bool,
3131
default: Any,
3232
name: str,
3333
python_name: PythonIdentifier,

openapi_python_client/schema/openapi_schema_pydantic/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Dict, List, Optional, Union
22

3-
from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr, model_validator
3+
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr, model_validator
44

55
from ..data_type import DataType
66
from .discriminator import Discriminator
@@ -36,7 +36,7 @@ class Schema(BaseModel):
3636
minProperties: Optional[int] = Field(default=None, ge=0)
3737
required: Optional[List[str]] = Field(default=None, min_length=1)
3838
enum: Union[None, List[Any]] = Field(default=None, min_length=1)
39-
const: Union[None, StrictStr, StrictInt] = None
39+
const: Union[None, StrictStr, StrictInt, StrictFloat, StrictBool] = None
4040
type: Union[DataType, List[DataType], None] = Field(default=None)
4141
allOf: List[Union[Reference, "Schema"]] = Field(default_factory=list)
4242
oneOf: List[Union[Reference, "Schema"]] = Field(default_factory=list)

tests/test_schema/test_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def test_nullable_with_allof():
1212
assert schema.allOf == []
1313

1414

15+
def test_constant_bool():
16+
schema = Schema.model_validate_json('{"type":"boolean", "enum":[true], "const":true, "default":true}')
17+
assert schema.const is True
18+
19+
1520
def test_nullable_with_type_list():
1621
schema = Schema.model_validate_json('{"type": ["string", "number"], "nullable": true}')
1722
assert schema.type == [DataType.STRING, DataType.NUMBER, DataType.NULL]

0 commit comments

Comments
 (0)