Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Literal

import httpx2
import pytest

from end_to_end_tests.functional_tests.helpers import (
Expand Down Expand Up @@ -273,6 +274,58 @@ def test_invalid_int(self, MyModel):
MyModel.from_dict({"mustBeThirty": 29})


@with_generated_client_fixture(
"""
paths:
/items:
get:
operationId: listItems
parameters:
- name: X-Version
in: header
required: true
schema: {type: string, const: "1.0"}
responses:
"204": {description: No content}
/orders:
get:
operationId: listOrders
parameters:
- $ref: "#/components/parameters/VersionHeader"
responses:
"204": {description: No content}
components:
parameters:
VersionHeader:
name: X-Version
in: header
schema: {type: string, const: "2.0"}
""",
extra_args=["--fail-on-warning"],
)
@with_generated_code_imports(".client.Client")
class TestConstHeaders:
@pytest.mark.parametrize(
("operation", "path", "version"),
[
("list_items", "/items", "1.0"),
("list_orders", "/orders", "2.0"),
("list_orders", "/orders", None),
],
)
def test_sends_string_const_headers(self, generated_client, Client, operation, path, version):
def handler(request: httpx2.Request) -> httpx2.Response:
assert request.url.path == path
assert request.headers.get("X-Version") == version
return httpx2.Response(204)

endpoint = generated_client.import_module(f".api.default.{operation}")
kwargs = {"x_version": version} if version is not None else {}
with Client(base_url="http://test", httpx_args={"transport": httpx2.MockTransport(handler)}) as client:
response = endpoint.sync_detailed(client=client, **kwargs)
assert response.status_code == 204


# The following tests of literal enums use basically the same specs as the tests above, but
# the "literal_enums" option is enabled in the test configuration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,24 @@ def test_literal_enum_duplicate_values(self):
"""
)
assert "Duplicate key X in enum" in str(result.exception)


def test_string_const_in_mixed_header_union_warns():
result = inline_spec_should_fail(
"""
paths:
/items:
get:
parameters:
- name: X-Version
in: header
schema:
oneOf:
- {type: string, const: stable}
- {type: integer}
responses:
"204": {description: No content}
""",
extra_args=["--fail-on-warning"],
)
assert "is not allowed in header" in result.stderr
9 changes: 8 additions & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .errors import GeneratorError, ParseError, PropertyError
from .properties import (
Class,
ConstProperty,
EnumProperty,
LiteralEnumProperty,
ModelProperty,
Expand Down Expand Up @@ -302,7 +303,13 @@ def add_parameters(

schemas = new_schemas

location_error = prop.validate_location(param.param_in)
# The header renderer can send a direct string constant as-is.
string_const_header = (
param.param_in == oai.ParameterLocation.HEADER
and isinstance(prop, ConstProperty)
and isinstance(prop.value.raw_value, str)
)
location_error = None if string_const_header else prop.validate_location(param.param_in)
if location_error is not None:
location_error.data = param
return location_error, schemas, parameters
Expand Down
Loading