Skip to content

Commit 188c0a2

Browse files
Merge pull request #42 from MarcoMuellner/41-httpx-client-stub-is-broken-in-version-046
41 httpx client stub is broken in version 046
2 parents 6366b19 + 9bf42ee commit 188c0a2

File tree

13 files changed

+134
-66
lines changed

13 files changed

+134
-66
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ dmypy.json
129129
.pyre/
130130
/.idea/
131131
/testclient/
132+
/tests/test_result/

docs/tutorial/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,10 @@ take a look at the `User.py` and the `Team.py` files:
647647

648648
``` py
649649
from typing import *
650-
650+
651651
from pydantic import BaseModel, Field
652-
653-
652+
653+
654654
class User(BaseModel):
655655
"""
656656
User model
@@ -675,9 +675,9 @@ take a look at the `User.py` and the `Team.py` files:
675675

676676
``` py
677677
from typing import *
678-
678+
679679
from pydantic import BaseModel, Field
680-
680+
681681
from .User import User
682682

683683

src/openapi_python_generator/language_converters/python/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import keyword
22
import re
33

4+
45
_use_orjson: bool = False
56
_symbol_ascii_strip_re = re.compile(r"[^A-Za-z0-9_]")
67

@@ -30,7 +31,7 @@ def normalize_symbol(symbol: str) -> str:
3031
:return: normalized identifier name
3132
"""
3233
symbol = symbol.replace("-", "_")
33-
normalized_symbol = _symbol_ascii_strip_re.sub('', symbol)
34+
normalized_symbol = _symbol_ascii_strip_re.sub("", symbol)
3435
if normalized_symbol in keyword.kwlist:
35-
normalized_symbol = normalized_symbol + '_'
36+
normalized_symbol = normalized_symbol + "_"
3637
return normalized_symbol

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
from openapi_python_generator.models import TypeConversion
2222

2323

24-
def type_converter(schema: Schema, required: bool = False, model_name: Optional[str] = None,) -> TypeConversion:
24+
def type_converter( # noqa: C901
25+
schema: Schema,
26+
required: bool = False,
27+
model_name: Optional[str] = None,
28+
) -> TypeConversion:
2529
"""
2630
Converts an OpenAPI type to a Python type.
2731
:param schema: Schema containing the type to be converted
@@ -119,7 +123,11 @@ def type_converter(schema: Schema, required: bool = False, model_name: Optional
119123
schema.schema_format is None or not common.get_use_orjson()
120124
):
121125
converted_type = pre_type + "str" + post_type
122-
elif schema.type == "string" and schema.schema_format.startswith("uuid") and common.get_use_orjson():
126+
elif (
127+
schema.type == "string"
128+
and schema.schema_format.startswith("uuid")
129+
and common.get_use_orjson()
130+
):
123131
if len(schema.schema_format) > 4 and schema.schema_format[4].isnumeric():
124132
uuid_type = schema.schema_format.upper()
125133
converted_type = pre_type + uuid_type + post_type
@@ -139,7 +147,9 @@ def type_converter(schema: Schema, required: bool = False, model_name: Optional
139147
elif schema.type == "array":
140148
retVal = pre_type + "List["
141149
if isinstance(schema.items, Reference):
142-
converted_reference = _generate_property_from_reference(model_name, "", schema.items, schema, required)
150+
converted_reference = _generate_property_from_reference(
151+
model_name, "", schema.items, schema, required
152+
)
143153
import_types = converted_reference.type.import_types
144154
original_type = "array<" + converted_reference.type.original_type + ">"
145155
retVal += converted_reference.type.converted_type
@@ -166,7 +176,7 @@ def type_converter(schema: Schema, required: bool = False, model_name: Optional
166176

167177

168178
def _generate_property_from_schema(
169-
model_name : str, name: str, schema: Schema, parent_schema: Optional[Schema] = None
179+
model_name: str, name: str, schema: Schema, parent_schema: Optional[Schema] = None
170180
) -> Property:
171181
"""
172182
Generates a property from a schema. It takes the type of the schema and converts it to a python type, and then
@@ -191,7 +201,11 @@ def _generate_property_from_schema(
191201

192202

193203
def _generate_property_from_reference(
194-
model_name: str, name: str, reference: Reference, parent_schema: Optional[Schema] = None, force_required: bool = False
204+
model_name: str,
205+
name: str,
206+
reference: Reference,
207+
parent_schema: Optional[Schema] = None,
208+
force_required: bool = False,
195209
) -> Property:
196210
"""
197211
Generates a property from a reference. It takes the name of the reference as the type, and then
@@ -212,13 +226,17 @@ def _generate_property_from_reference(
212226
if import_model == model_name:
213227
type_conv = TypeConversion(
214228
original_type=reference.ref,
215-
converted_type=import_model if required else 'Optional["' + import_model + '"]',
216-
import_types=None
229+
converted_type=import_model
230+
if required
231+
else 'Optional["' + import_model + '"]',
232+
import_types=None,
217233
)
218234
else:
219235
type_conv = TypeConversion(
220236
original_type=reference.ref,
221-
converted_type=import_model if required else "Optional[" + import_model + "]",
237+
converted_type=import_model
238+
if required
239+
else "Optional[" + import_model + "]",
222240
import_types=[f"from .{import_model} import {import_model}"],
223241
)
224242
return Property(

src/openapi_python_generator/language_converters/python/service_generator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
2-
from typing import Dict, Literal
2+
from typing import Dict
33
from typing import List
4+
from typing import Literal
45
from typing import Tuple
56
from typing import Union
67

@@ -159,24 +160,29 @@ def generate_operation_id(operation: Operation, http_op: str) -> str:
159160
raise Exception(f"OperationId is not defined for {http_op}") # pragma: no cover
160161

161162

162-
def _generate_params(operation: Operation, param_in : Literal["query", "header"] = "query"):
163+
def _generate_params(
164+
operation: Operation, param_in: Literal["query", "header"] = "query"
165+
):
163166
if operation.parameters is None:
164167
return []
165168

166169
params = []
167170
for param in operation.parameters:
168171
if isinstance(param, Parameter) and param.param_in == param_in:
169172
param_name_cleaned = common.normalize_symbol(param.name)
170-
params.append(f"'{param.name}' : {param_name_cleaned}")
173+
params.append(f"{param.name!r} : {param_name_cleaned}")
171174

172175
return params
173176

177+
174178
def generate_query_params(operation: Operation) -> List[str]:
175179
return _generate_params(operation, "query")
176180

181+
177182
def generate_header_params(operation: Operation) -> List[str]:
178183
return _generate_params(operation, "header")
179184

185+
180186
def generate_return_type(operation: Operation) -> OpReturnType:
181187
if operation.responses is None:
182188
return OpReturnType(type=None, status_code=200, complex_type=False)

src/openapi_python_generator/language_converters/python/templates/httpx.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ with httpx.Client(base_url=base_path, verify=api_config.verify) as client:
3232
{% if use_orjson %}
3333
content=orjson.dumps({{ body_param }})
3434
{% else %}
35-
json = json.dumps({{ body_param }})
35+
json = {{ body_param }}
3636
{% endif %}
3737
{% endif %}
3838
)

tests/regression/test_issue_11.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def runner() -> CliRunner:
1212
"""Fixture for invoking command-line interfaces."""
1313
return CliRunner()
1414

15+
1516
@pytest.mark.parametrize(
1617
"library",
1718
[HTTPLibrary.httpx, HTTPLibrary.requests, HTTPLibrary.aiohttp],
@@ -30,4 +31,3 @@ def test_issue_11(runner: CliRunner, model_data_with_cleanup, library) -> None:
3031
],
3132
)
3233
assert result.exit_code == 0
33-

tests/regression/test_issue_17.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def runner() -> CliRunner:
1313
"""Fixture for invoking command-line interfaces."""
1414
return CliRunner()
1515

16+
1617
@pytest.mark.parametrize(
1718
"library",
1819
[HTTPLibrary.httpx, HTTPLibrary.aiohttp, HTTPLibrary.requests],
@@ -21,7 +22,7 @@ def test_issue_11(runner: CliRunner, model_data_with_cleanup, library) -> None:
2122
"""
2223
https://github.com/MarcoMuellner/openapi-python-generator/issues/7
2324
"""
24-
assert generate_data(test_data_folder / "issue_17.json",
25-
test_result_path,
26-
library) is None
27-
25+
assert (
26+
generate_data(test_data_folder / "issue_17.json", test_result_path, library)
27+
is None
28+
)

tests/regression/test_issue_illegal_py_symbols.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def runner() -> CliRunner:
1717
"library",
1818
[HTTPLibrary.httpx, HTTPLibrary.requests, HTTPLibrary.aiohttp],
1919
)
20-
def test_issue_keyword_parameter_name(runner: CliRunner, model_data_with_cleanup, library) -> None:
20+
def test_issue_keyword_parameter_name(
21+
runner: CliRunner, model_data_with_cleanup, library
22+
) -> None:
2123
result = runner.invoke(
2224
main,
2325
[
@@ -34,7 +36,9 @@ def test_issue_keyword_parameter_name(runner: CliRunner, model_data_with_cleanup
3436
"library",
3537
[HTTPLibrary.httpx, HTTPLibrary.requests, HTTPLibrary.aiohttp],
3638
)
37-
def test_issue_illegal_character_in_operation_id(runner: CliRunner, model_data_with_cleanup, library) -> None:
39+
def test_issue_illegal_character_in_operation_id(
40+
runner: CliRunner, model_data_with_cleanup, library
41+
) -> None:
3842
result = runner.invoke(
3943
main,
4044
[
@@ -44,4 +48,4 @@ def test_issue_illegal_character_in_operation_id(runner: CliRunner, model_data_w
4448
library.value,
4549
],
4650
)
47-
assert result.exit_code == 0
51+
assert result.exit_code == 0

tests/test_data/gitea_issue_11.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)