|
| 1 | +import enum |
| 2 | + |
| 3 | +import pytest |
| 4 | +from jsonschema_path import SchemaPath |
| 5 | + |
| 6 | +from openapi_core import V30RequestUnmarshaller |
| 7 | +from openapi_core import V31RequestUnmarshaller |
| 8 | +from openapi_core.datatypes import Parameters |
| 9 | +from openapi_core.testing import MockRequest |
| 10 | + |
| 11 | + |
| 12 | +class Colors(enum.Enum): |
| 13 | + |
| 14 | + YELLOW = "yellow" |
| 15 | + BLUE = "blue" |
| 16 | + RED = "red" |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def of(cls, v: str): |
| 20 | + for it in cls: |
| 21 | + if it.value == v: |
| 22 | + return it |
| 23 | + raise ValueError(f"Invalid value: {v}") |
| 24 | + |
| 25 | + |
| 26 | +class TestRequestUnmarshaller: |
| 27 | + |
| 28 | + @pytest.fixture(scope="session") |
| 29 | + def spec_dict(self): |
| 30 | + return { |
| 31 | + "openapi": "3.1.0", |
| 32 | + "info": { |
| 33 | + "title": "Test request body unmarshaller", |
| 34 | + "version": "0.1", |
| 35 | + }, |
| 36 | + "paths": { |
| 37 | + "/resources": { |
| 38 | + "post": { |
| 39 | + "description": "POST resources test request", |
| 40 | + "requestBody": { |
| 41 | + "description": "", |
| 42 | + "content": { |
| 43 | + "application/json": { |
| 44 | + "schema": { |
| 45 | + "$ref": "#/components/schemas/createResource" |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + }, |
| 50 | + "responses": { |
| 51 | + "201": {"description": "Resource was created."} |
| 52 | + }, |
| 53 | + }, |
| 54 | + "get": { |
| 55 | + "description": "POST resources test request", |
| 56 | + "parameters": [ |
| 57 | + { |
| 58 | + "name": "color", |
| 59 | + "in": "query", |
| 60 | + "required": False, |
| 61 | + "schema": { |
| 62 | + "$ref": "#/components/schemas/colors" |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + "responses": { |
| 67 | + "default": { |
| 68 | + "description": "Returned resources matching request." |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + }, |
| 74 | + "components": { |
| 75 | + "schemas": { |
| 76 | + "colors": { |
| 77 | + "type": "string", |
| 78 | + "enum": ["yellow", "blue", "red"], |
| 79 | + "format": "enum_Colors", |
| 80 | + }, |
| 81 | + "createResource": { |
| 82 | + "type": "object", |
| 83 | + "properties": { |
| 84 | + "resId": {"type": "integer"}, |
| 85 | + "color": {"$ref": "#/components/schemas/colors"}, |
| 86 | + }, |
| 87 | + "required": ["resId", "color"], |
| 88 | + }, |
| 89 | + } |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + @pytest.fixture(scope="session") |
| 94 | + def spec(self, spec_dict): |
| 95 | + return SchemaPath.from_dict(spec_dict) |
| 96 | + |
| 97 | + @pytest.mark.parametrize( |
| 98 | + "req_unmarshaller_cls", |
| 99 | + [V30RequestUnmarshaller, V31RequestUnmarshaller], |
| 100 | + ) |
| 101 | + def test_request_body_extra_unmarshaller(self, spec, req_unmarshaller_cls): |
| 102 | + ru = req_unmarshaller_cls( |
| 103 | + spec=spec, extra_format_unmarshallers={"enum_Colors": Colors.of} |
| 104 | + ) |
| 105 | + request = MockRequest( |
| 106 | + host_url="http://example.com", |
| 107 | + method="post", |
| 108 | + path="/resources", |
| 109 | + data=b'{"resId": 23498572, "color": "blue"}', |
| 110 | + ) |
| 111 | + result = ru.unmarshal(request) |
| 112 | + |
| 113 | + assert not result.errors |
| 114 | + assert result.body == {"resId": 23498572, "color": Colors.BLUE} |
| 115 | + assert result.parameters == Parameters() |
| 116 | + |
| 117 | + @pytest.mark.parametrize( |
| 118 | + "req_unmarshaller_cls", |
| 119 | + [V30RequestUnmarshaller, V31RequestUnmarshaller], |
| 120 | + ) |
| 121 | + def test_request_param_extra_unmarshaller( |
| 122 | + self, spec, req_unmarshaller_cls |
| 123 | + ): |
| 124 | + ru = req_unmarshaller_cls( |
| 125 | + spec=spec, extra_format_unmarshallers={"enum_Colors": Colors.of} |
| 126 | + ) |
| 127 | + request = MockRequest( |
| 128 | + host_url="http://example.com", |
| 129 | + method="get", |
| 130 | + path="/resources", |
| 131 | + args={"color": "blue"}, |
| 132 | + ) |
| 133 | + result = ru.unmarshal(request) |
| 134 | + |
| 135 | + assert not result.errors |
| 136 | + assert result.parameters == Parameters(query=dict(color=Colors.BLUE)) |
0 commit comments