Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/support_const_booleans_and_floats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
default: patch
---

# Support `const` booleans and floats

Fixed in PR #1086. Thanks @flxdot!
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConstProperty(PropertyProtocol):
def build(
cls,
*,
const: str | int,
const: str | int | float | bool,
default: Any,
name: str,
python_name: PythonIdentifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr, model_validator
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr, model_validator

from ..data_type import DataType
from .discriminator import Discriminator
Expand Down Expand Up @@ -36,7 +36,7 @@ class Schema(BaseModel):
minProperties: Optional[int] = Field(default=None, ge=0)
required: Optional[List[str]] = Field(default=None, min_length=1)
enum: Union[None, List[Any]] = Field(default=None, min_length=1)
const: Union[None, StrictStr, StrictInt] = None
const: Union[None, StrictStr, StrictInt, StrictFloat, StrictBool] = None
type: Union[DataType, List[DataType], None] = Field(default=None)
allOf: List[Union[Reference, "Schema"]] = Field(default_factory=list)
oneOf: List[Union[Reference, "Schema"]] = Field(default_factory=list)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_schema/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def test_nullable_with_allof():
assert schema.allOf == []


def test_constant_bool():
schema = Schema.model_validate_json('{"type":"boolean", "enum":[true], "const":true, "default":true}')
assert schema.const is True


def test_nullable_with_type_list():
schema = Schema.model_validate_json('{"type": ["string", "number"], "nullable": true}')
assert schema.type == [DataType.STRING, DataType.NUMBER, DataType.NULL]
Expand Down
Loading