Skip to content

validate value of 'const' properties (helps with discriminated unions) #1024

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 5 commits into from
Apr 19, 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
10 changes: 10 additions & 0 deletions .changeset/const_values_are_now_validated_at_runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
default: major
---

# `const` values in responses are now validated at runtime

Prior to this version, `const` values returned from servers were assumed to always be correct. Now, if a server returns
an unexpected value, the client will raise a `ValueError`. This should enable better usage with `oneOf`.

PR #1024. Thanks @peter-greenatlas!
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def _parse_response(
) -> Optional[Literal["Why have a fixed response? I dunno"]]:
if response.status_code == HTTPStatus.OK:
response_200 = cast(Literal["Why have a fixed response? I dunno"], response.json())
if response_200 != "Why have a fixed response? I dunno":
raise ValueError(
f"response_200 must match const 'Why have a fixed response? I dunno', got '{response_200}'"
)
return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,26 @@ def to_dict(self) -> Dict[str, Any]:
@classmethod
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
required = d.pop("required")
required = cast(Literal["this always goes in the body"], d.pop("required"))
if required != "this always goes in the body":
raise ValueError(f"required must match const 'this always goes in the body', got '{required}'")

def _parse_nullable(data: object) -> Union[Literal["this or null goes in the body"], None]:
if data is None:
return data
nullable_type_1 = cast(Literal["this or null goes in the body"], data)
if nullable_type_1 != "this or null goes in the body":
raise ValueError(
f"nullable_type_1 must match const 'this or null goes in the body', got '{nullable_type_1}'"
)
return nullable_type_1
return cast(Union[Literal["this or null goes in the body"], None], data)

nullable = _parse_nullable(d.pop("nullable"))

optional = d.pop("optional", UNSET)
optional = cast(Union[Literal["this sometimes goes in the body"], Unset], d.pop("optional", UNSET))
if optional != "this sometimes goes in the body" and not isinstance(optional, Unset):
raise ValueError(f"optional must match const 'this sometimes goes in the body', got '{optional}'")

post_const_path_body = cls(
required=required,
Expand Down
5 changes: 3 additions & 2 deletions openapi_python_client/parser/properties/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, overload
from typing import Any, ClassVar, overload

from attr import define

Expand All @@ -12,7 +12,7 @@

@define
class ConstProperty(PropertyProtocol):
"""A property representing a Union (anyOf) of other properties"""
"""A property representing a const value"""

name: str
required: bool
Expand All @@ -21,6 +21,7 @@ class ConstProperty(PropertyProtocol):
python_name: PythonIdentifier
description: str | None
example: None
template: ClassVar[str] = "const_property.py.jinja"

@classmethod
def build(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro construct(property, source) %}
{{ property.python_name }} = cast({{ property.get_type_string() }} , {{ source }})
if {{ property.python_name }} != {{ property.value }}{% if not property.required %}and not isinstance({{ property.python_name }}, Unset){% endif %}:
raise ValueError(f"{{ property.name }} must match const {{ property.value }}, got '{{'{' + property.python_name + '}' }}'")
{%- endmacro %}