Skip to content

Commit 1ac713b

Browse files
author
Doug Borg
committed
feat: generalize generator & api_config for 3.0+ (multi-version OpenAPI types, model_dump) and update templates
1 parent c528b86 commit 1ac713b

7 files changed

Lines changed: 25 additions & 14 deletions

File tree

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from typing import Optional
22

3-
from openapi_pydantic.v3.v3_0 import OpenAPI
3+
from openapi_pydantic.v3 import OpenAPI
44

55
from openapi_python_generator.common import PydanticVersion
66
from openapi_python_generator.language_converters.python.jinja_config import (
7-
API_CONFIG_TEMPLATE, API_CONFIG_TEMPLATE_PYDANTIC_V2,
7+
API_CONFIG_TEMPLATE,
8+
API_CONFIG_TEMPLATE_PYDANTIC_V2,
89
)
910
from openapi_python_generator.language_converters.python.jinja_config import (
1011
create_jinja_env,
@@ -13,19 +14,24 @@
1314

1415

1516
def generate_api_config(
16-
data: OpenAPI, env_token_name: Optional[str] = None,
17-
pydantic_version: PydanticVersion = PydanticVersion.V2,
17+
data: OpenAPI,
18+
env_token_name: Optional[str] = None,
19+
pydantic_version: PydanticVersion = PydanticVersion.V2,
1820
) -> APIConfig:
1921
"""
2022
Generate the API model.
2123
"""
2224

23-
template_name = API_CONFIG_TEMPLATE_PYDANTIC_V2 if pydantic_version == PydanticVersion.V2 else API_CONFIG_TEMPLATE
25+
template_name = (
26+
API_CONFIG_TEMPLATE_PYDANTIC_V2
27+
if pydantic_version == PydanticVersion.V2
28+
else API_CONFIG_TEMPLATE
29+
)
2430
jinja_env = create_jinja_env()
2531
return APIConfig(
2632
file_name="api_config",
2733
content=jinja_env.get_template(template_name).render(
28-
env_token_name=env_token_name, **data.dict()
34+
env_token_name=env_token_name, **data.model_dump()
2935
),
3036
base_url=data.servers[0].url if len(data.servers) > 0 else "NO SERVER",
3137
)

src/openapi_python_generator/language_converters/python/generator.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

3-
from openapi_pydantic.v3.v3_0 import OpenAPI
3+
from openapi_pydantic.v3.v3_0 import OpenAPI as OpenAPI30
4+
from openapi_pydantic.v3.v3_1 import OpenAPI as OpenAPI31
45

56
from openapi_python_generator.common import PydanticVersion
67
from openapi_python_generator.language_converters.python import common
@@ -16,17 +17,20 @@
1617
from openapi_python_generator.models import ConversionResult
1718
from openapi_python_generator.models import LibraryConfig
1819

20+
# Type alias for both OpenAPI versions
21+
OpenAPISpec = Union[OpenAPI30, OpenAPI31]
22+
1923

2024
def generator(
21-
data: OpenAPI,
25+
data: OpenAPISpec,
2226
library_config: LibraryConfig,
2327
env_token_name: Optional[str] = None,
2428
use_orjson: bool = False,
2529
custom_template_path: Optional[str] = None,
2630
pydantic_version: PydanticVersion = PydanticVersion.V2,
2731
) -> ConversionResult:
2832
"""
29-
Generate Python code from an OpenAPI 3.0 specification.
33+
Generate Python code from an OpenAPI 3.0+ specification.
3034
"""
3135

3236
common.set_use_orjson(use_orjson)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
async def {{ operation_id }}({{ params }} api_config_override : Optional[APIConfig] = None) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
1+
async def {{ operation_id }}(api_config_override : Optional[APIConfig] = None{% if params.strip() %}, *, {{ params.rstrip(', ') }}{% endif %}) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
22
api_config = api_config_override if api_config_override else APIConfig()
33

44
base_path = api_config.base_path

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% if async_client %}async {% endif %}def {{ operation_id }}({{ params }} api_config_override : Optional[APIConfig] = None) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
1+
{% if async_client %}async {% endif %}def {{ operation_id }}(api_config_override : Optional[APIConfig] = None{% if params.strip() %}, *, {{ params.rstrip(', ') }}{% endif %}) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
22
api_config = api_config_override if api_config_override else APIConfig()
33

44
base_path = api_config.base_path

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def {{ operation_id }}({{ params }} api_config_override : Optional[APIConfig] = None) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
1+
def {{ operation_id }}(api_config_override : Optional[APIConfig] = None{% if params.strip() %}, *, {{ params.rstrip(', ') }}{% endif %}) -> {% if return_type.type is none or return_type.type.converted_type is none %}None{% else %}{{ return_type.type.converted_type}}{% endif %}:
22
api_config = api_config_override if api_config_override else APIConfig()
33

44
base_path = api_config.base_path

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {{ library_import }}
33
import json
44
{% if use_orjson %}
55
import orjson
6+
from uuid import UUID
67
{% endif %}
78

89
from ..models import *

tests/test_api_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from openapi_pydantic.v3.v3_0 import OpenAPI
1+
from openapi_pydantic.v3 import OpenAPI
22

33
from openapi_python_generator.language_converters.python.api_config_generator import (
44
generate_api_config,

0 commit comments

Comments
 (0)