diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py b/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py index cb2e09d79..14cd97158 100644 --- a/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py @@ -1,5 +1,6 @@ from typing import Literal +import httpx2 import pytest from end_to_end_tests.functional_tests.helpers import ( @@ -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. diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py index e33a2cc1a..0c432cd19 100644 --- a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_enums_and_consts.py @@ -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 diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index c16f03f2f..7e1b094a6 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -15,6 +15,7 @@ from .errors import GeneratorError, ParseError, PropertyError from .properties import ( Class, + ConstProperty, EnumProperty, LiteralEnumProperty, ModelProperty, @@ -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