ab-openapi-python-generator 2.1.4.dev1768280320__py3-none-any.whl → 2.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ab_openapi_python_generator/__init__.py +14 -10
- ab_openapi_python_generator/__main__.py +85 -0
- ab_openapi_python_generator/common.py +58 -0
- ab_openapi_python_generator/generate_data.py +235 -0
- ab_openapi_python_generator/language_converters/__init__.py +0 -0
- ab_openapi_python_generator/language_converters/python/__init__.py +0 -0
- ab_openapi_python_generator/language_converters/python/client_generator.py +450 -0
- ab_openapi_python_generator/language_converters/python/common.py +58 -0
- ab_openapi_python_generator/language_converters/python/exception_generator.py +23 -0
- ab_openapi_python_generator/language_converters/python/generator.py +52 -0
- ab_openapi_python_generator/language_converters/python/jinja_config.py +38 -0
- ab_openapi_python_generator/language_converters/python/model_generator.py +838 -0
- ab_openapi_python_generator/language_converters/python/templates/alias_union.jinja2 +17 -0
- ab_openapi_python_generator/language_converters/python/templates/async_client_httpx_pydantic_2.jinja2 +80 -0
- ab_openapi_python_generator/language_converters/python/templates/discriminator_enum.jinja2 +7 -0
- ab_openapi_python_generator/language_converters/python/templates/enum.jinja2 +11 -0
- ab_openapi_python_generator/language_converters/python/templates/http_exception.jinja2 +8 -0
- ab_openapi_python_generator/language_converters/python/templates/models.jinja2 +24 -0
- ab_openapi_python_generator/language_converters/python/templates/models_pydantic_2.jinja2 +28 -0
- ab_openapi_python_generator/language_converters/python/templates/sync_client_httpx_pydantic_2.jinja2 +80 -0
- ab_openapi_python_generator/models.py +101 -0
- ab_openapi_python_generator/parsers/__init__.py +13 -0
- ab_openapi_python_generator/parsers/openapi_30.py +65 -0
- ab_openapi_python_generator/parsers/openapi_31.py +65 -0
- ab_openapi_python_generator/py.typed +0 -0
- ab_openapi_python_generator/version_detector.py +67 -0
- {ab_openapi_python_generator-2.1.4.dev1768280320.dist-info → ab_openapi_python_generator-2.2.1.dist-info}/METADATA +21 -27
- ab_openapi_python_generator-2.2.1.dist-info/RECORD +31 -0
- {ab_openapi_python_generator-2.1.4.dev1768280320.dist-info → ab_openapi_python_generator-2.2.1.dist-info}/WHEEL +1 -1
- ab_openapi_python_generator-2.2.1.dist-info/entry_points.txt +2 -0
- ab_openapi_python_generator-2.1.4.dev1768280320.dist-info/RECORD +0 -6
- ab_openapi_python_generator-2.1.4.dev1768280320.dist-info/entry_points.txt +0 -3
- {ab_openapi_python_generator-2.1.4.dev1768280320.dist-info → ab_openapi_python_generator-2.2.1.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{# Renders ONE standalone python module that defines a named Union/Annotated alias #}
|
|
2
|
+
{% if discriminator_key -%}
|
|
3
|
+
from typing import Annotated, Union
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
{% else -%}
|
|
6
|
+
from typing import Union
|
|
7
|
+
{% endif %}
|
|
8
|
+
|
|
9
|
+
{% for imp in member_imports -%}
|
|
10
|
+
{{ imp }}
|
|
11
|
+
{% endfor %}
|
|
12
|
+
|
|
13
|
+
{% if discriminator_key -%}
|
|
14
|
+
{{ alias_name }} = Annotated[{{ union_type }}, Field(discriminator="{{ discriminator_key }}")]
|
|
15
|
+
{% else -%}
|
|
16
|
+
{{ alias_name }} = {{ union_type }}
|
|
17
|
+
{% endif -%}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{% if env_token_name is not none %}import os{% endif %}
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, Optional, Union
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from ..models import *
|
|
9
|
+
{% set _base_path = servers[0].url if servers|length > 0 else "/" %}
|
|
10
|
+
from ..exceptions import HTTPException
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AsyncClient(BaseModel):
|
|
14
|
+
model_config = {"validate_assignment": True}
|
|
15
|
+
|
|
16
|
+
base_path: str = "{{ _base_path }}"
|
|
17
|
+
verify: Union[bool, str] = True
|
|
18
|
+
{% if env_token_name is none %}
|
|
19
|
+
access_token: Optional[str] = None
|
|
20
|
+
{% endif %}
|
|
21
|
+
|
|
22
|
+
def get_access_token(self) -> Optional[str]:
|
|
23
|
+
{% if env_token_name is not none %}
|
|
24
|
+
try:
|
|
25
|
+
return os.environ["{{ env_token_name }}"]
|
|
26
|
+
except KeyError:
|
|
27
|
+
return None
|
|
28
|
+
{% else %}
|
|
29
|
+
return self.access_token
|
|
30
|
+
{% endif %}
|
|
31
|
+
|
|
32
|
+
def set_access_token(self, value: str) -> None:
|
|
33
|
+
{% if env_token_name is not none %}
|
|
34
|
+
raise Exception(
|
|
35
|
+
"This client was generated with an environment variable for the access token. "
|
|
36
|
+
"Please set '{{ env_token_name }}'."
|
|
37
|
+
)
|
|
38
|
+
{% else %}
|
|
39
|
+
self.access_token = value
|
|
40
|
+
{% endif %}
|
|
41
|
+
|
|
42
|
+
{% for op in operations %}
|
|
43
|
+
async def {{ op.operation_id }}(self{% if op.params %}, {{ op.params }}{% endif %}) -> Any:
|
|
44
|
+
base_path = self.base_path
|
|
45
|
+
path = f"{{ op.path_name }}"
|
|
46
|
+
|
|
47
|
+
headers = {
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
"Accept": "application/json",
|
|
50
|
+
"Authorization": f"Bearer { self.get_access_token() }",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
query_params: Dict[str, Any] = {
|
|
54
|
+
{% for qp in op.query_params %}
|
|
55
|
+
{{ qp }},
|
|
56
|
+
{% endfor %}
|
|
57
|
+
}
|
|
58
|
+
query_params = {k: v for (k, v) in query_params.items() if v is not None}
|
|
59
|
+
|
|
60
|
+
async with httpx.AsyncClient(base_url=base_path, verify=self.verify) as client:
|
|
61
|
+
response = await client.request(
|
|
62
|
+
"{{ op.method }}",
|
|
63
|
+
httpx.URL(path),
|
|
64
|
+
headers=headers,
|
|
65
|
+
params=query_params,
|
|
66
|
+
{% if op.body_param %}
|
|
67
|
+
json={{ op.body_param }},
|
|
68
|
+
{% endif %}
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
if response.status_code != {{ op.return_type.status_code }}:
|
|
72
|
+
raise HTTPException(
|
|
73
|
+
response.status_code,
|
|
74
|
+
f"{{ op.operation_id }} failed with status code: {response.status_code}",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
body = None if {{ op.return_type.status_code }} == 204 else response.json()
|
|
78
|
+
return body
|
|
79
|
+
|
|
80
|
+
{% endfor %}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
class HTTPException(Exception):
|
|
2
|
+
def __init__(self, status_code: int, message: str):
|
|
3
|
+
self.status_code = status_code
|
|
4
|
+
self.message = message
|
|
5
|
+
super().__init__(f"{status_code} {message}")
|
|
6
|
+
|
|
7
|
+
def __str__(self) -> str:
|
|
8
|
+
return f"{self.status_code} {self.message}"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import *
|
|
2
|
+
from pydantic import BaseModel, Field
|
|
3
|
+
{% for property in properties %}
|
|
4
|
+
{% if property.type.import_types is not none %}
|
|
5
|
+
{% for import_type in property.type.import_types %}
|
|
6
|
+
{{ import_type }}
|
|
7
|
+
{% endfor %}
|
|
8
|
+
{% endif %}
|
|
9
|
+
{% endfor %}
|
|
10
|
+
|
|
11
|
+
class {{ schema_name }}(BaseModel):
|
|
12
|
+
"""
|
|
13
|
+
{% if schema.title %}{{ schema.title }}{% else %}{{ schema_name }}{% endif %} model
|
|
14
|
+
{% if schema.description %}
|
|
15
|
+
{{ schema.description }}
|
|
16
|
+
{% endif %}
|
|
17
|
+
{% if parent_comment %}
|
|
18
|
+
{{ parent_comment }}
|
|
19
|
+
{% endif %}
|
|
20
|
+
"""
|
|
21
|
+
{% for property in properties %}
|
|
22
|
+
|
|
23
|
+
{{ property.name | normalize_symbol }} : {{ property.type.converted_type | safe }} = Field(alias="{{ property.name }}"{% if property.default is not none %}, default = {{ property.default }}{% endif %})
|
|
24
|
+
{% endfor %}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from typing import *
|
|
2
|
+
from pydantic import BaseModel, Field
|
|
3
|
+
{% for property in properties %}
|
|
4
|
+
{% if property.type.import_types is not none %}
|
|
5
|
+
{% for import_type in property.type.import_types %}
|
|
6
|
+
{{ import_type }}
|
|
7
|
+
{% endfor %}
|
|
8
|
+
{% endif %}
|
|
9
|
+
{% endfor %}
|
|
10
|
+
|
|
11
|
+
class {{ schema_name }}(BaseModel):
|
|
12
|
+
"""
|
|
13
|
+
{% if schema.title %}{{ schema.title }}{% else %}{{ schema_name }}{% endif %} model
|
|
14
|
+
{% if schema.description %}
|
|
15
|
+
{{ schema.description }}
|
|
16
|
+
{% endif %}
|
|
17
|
+
{% if parent_comment %}
|
|
18
|
+
{{ parent_comment }}
|
|
19
|
+
{% endif %}
|
|
20
|
+
"""
|
|
21
|
+
model_config = {
|
|
22
|
+
"populate_by_name": True,
|
|
23
|
+
"validate_assignment": True
|
|
24
|
+
}
|
|
25
|
+
{% for property in properties %}
|
|
26
|
+
|
|
27
|
+
{{ property.name | normalize_symbol }} : {{ property.type.converted_type | safe }} = Field(validation_alias="{{ property.name }}"{% if property.default is not none %}, default = {{ property.default }}{% endif %})
|
|
28
|
+
{% endfor %}
|
ab_openapi_python_generator/language_converters/python/templates/sync_client_httpx_pydantic_2.jinja2
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{% if env_token_name is not none %}import os{% endif %}
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, Optional, Union
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from ..models import *
|
|
9
|
+
{% set _base_path = servers[0].url if servers|length > 0 else "/" %}
|
|
10
|
+
from ..exceptions import HTTPException
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SyncClient(BaseModel):
|
|
14
|
+
model_config = {"validate_assignment": True}
|
|
15
|
+
|
|
16
|
+
base_path: str = "{{ _base_path }}"
|
|
17
|
+
verify: Union[bool, str] = True
|
|
18
|
+
{% if env_token_name is none %}
|
|
19
|
+
access_token: Optional[str] = None
|
|
20
|
+
{% endif %}
|
|
21
|
+
|
|
22
|
+
def get_access_token(self) -> Optional[str]:
|
|
23
|
+
{% if env_token_name is not none %}
|
|
24
|
+
try:
|
|
25
|
+
return os.environ["{{ env_token_name }}"]
|
|
26
|
+
except KeyError:
|
|
27
|
+
return None
|
|
28
|
+
{% else %}
|
|
29
|
+
return self.access_token
|
|
30
|
+
{% endif %}
|
|
31
|
+
|
|
32
|
+
def set_access_token(self, value: str) -> None:
|
|
33
|
+
{% if env_token_name is not none %}
|
|
34
|
+
raise Exception(
|
|
35
|
+
"This client was generated with an environment variable for the access token. "
|
|
36
|
+
"Please set '{{ env_token_name }}'."
|
|
37
|
+
)
|
|
38
|
+
{% else %}
|
|
39
|
+
self.access_token = value
|
|
40
|
+
{% endif %}
|
|
41
|
+
|
|
42
|
+
{% for op in operations %}
|
|
43
|
+
def {{ op.operation_id }}(self{% if op.params %}, {{ op.params }}{% endif %}) -> Any:
|
|
44
|
+
base_path = self.base_path
|
|
45
|
+
path = f"{{ op.path_name }}"
|
|
46
|
+
|
|
47
|
+
headers = {
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
"Accept": "application/json",
|
|
50
|
+
"Authorization": f"Bearer { self.get_access_token() }",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
query_params: Dict[str, Any] = {
|
|
54
|
+
{% for qp in op.query_params %}
|
|
55
|
+
{{ qp }},
|
|
56
|
+
{% endfor %}
|
|
57
|
+
}
|
|
58
|
+
query_params = {k: v for (k, v) in query_params.items() if v is not None}
|
|
59
|
+
|
|
60
|
+
with httpx.Client(base_url=base_path, verify=self.verify) as client:
|
|
61
|
+
response = client.request(
|
|
62
|
+
"{{ op.method }}",
|
|
63
|
+
httpx.URL(path),
|
|
64
|
+
headers=headers,
|
|
65
|
+
params=query_params,
|
|
66
|
+
{% if op.body_param %}
|
|
67
|
+
json={{ op.body_param }},
|
|
68
|
+
{% endif %}
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
if response.status_code != {{ op.return_type.status_code }}:
|
|
72
|
+
raise HTTPException(
|
|
73
|
+
response.status_code,
|
|
74
|
+
f"{{ op.operation_id }} failed with status code: {response.status_code}",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
body = None if {{ op.return_type.status_code }} == 204 else response.json()
|
|
78
|
+
return body
|
|
79
|
+
|
|
80
|
+
{% endfor %}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from typing import List, Optional, Union
|
|
2
|
+
|
|
3
|
+
from openapi_pydantic.v3.v3_0 import (
|
|
4
|
+
Operation as Operation30,
|
|
5
|
+
)
|
|
6
|
+
from openapi_pydantic.v3.v3_0 import (
|
|
7
|
+
PathItem as PathItem30,
|
|
8
|
+
)
|
|
9
|
+
from openapi_pydantic.v3.v3_0 import (
|
|
10
|
+
Schema as Schema30,
|
|
11
|
+
)
|
|
12
|
+
from openapi_pydantic.v3.v3_1 import (
|
|
13
|
+
Operation as Operation31,
|
|
14
|
+
)
|
|
15
|
+
from openapi_pydantic.v3.v3_1 import (
|
|
16
|
+
PathItem as PathItem31,
|
|
17
|
+
)
|
|
18
|
+
from openapi_pydantic.v3.v3_1 import (
|
|
19
|
+
Schema as Schema31,
|
|
20
|
+
)
|
|
21
|
+
from pydantic import BaseModel, Field
|
|
22
|
+
|
|
23
|
+
# Type unions for compatibility with both OpenAPI 3.0 and 3.1
|
|
24
|
+
Operation = Union[Operation30, Operation31]
|
|
25
|
+
PathItem = Union[PathItem30, PathItem31]
|
|
26
|
+
Schema = Union[Schema30, Schema31]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class LibraryConfig(BaseModel):
|
|
30
|
+
name: str
|
|
31
|
+
library_name: str
|
|
32
|
+
template_name: str
|
|
33
|
+
include_async: bool
|
|
34
|
+
include_sync: bool
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class TypeConversion(BaseModel):
|
|
38
|
+
original_type: str
|
|
39
|
+
converted_type: str
|
|
40
|
+
import_types: Optional[List[str]] = None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class OpReturnType(BaseModel):
|
|
44
|
+
type: Optional[TypeConversion] = None
|
|
45
|
+
status_code: int
|
|
46
|
+
complex_type: bool = False
|
|
47
|
+
list_type: Optional[str] = None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ServiceOperation(BaseModel):
|
|
51
|
+
params: str
|
|
52
|
+
operation_id: str
|
|
53
|
+
query_params: List[str]
|
|
54
|
+
header_params: List[str]
|
|
55
|
+
return_type: OpReturnType
|
|
56
|
+
operation: Operation
|
|
57
|
+
pathItem: PathItem
|
|
58
|
+
content: str
|
|
59
|
+
async_client: Optional[bool] = False
|
|
60
|
+
tag: Optional[str] = None
|
|
61
|
+
path_name: str
|
|
62
|
+
body_param: Optional[str] = None
|
|
63
|
+
method: str
|
|
64
|
+
is_sse: bool = False
|
|
65
|
+
use_orjson: bool = False
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Property(BaseModel):
|
|
69
|
+
name: str
|
|
70
|
+
type: TypeConversion
|
|
71
|
+
required: bool
|
|
72
|
+
default: Optional[str]
|
|
73
|
+
import_type: Optional[List[str]] = None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Model(BaseModel):
|
|
77
|
+
file_name: str
|
|
78
|
+
content: str
|
|
79
|
+
openapi_object: Optional[Schema] = None
|
|
80
|
+
properties: List[Property] = []
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class Service(BaseModel):
|
|
84
|
+
file_name: str
|
|
85
|
+
operations: List[ServiceOperation]
|
|
86
|
+
content: str
|
|
87
|
+
async_client: Optional[bool] = False
|
|
88
|
+
library_import: str
|
|
89
|
+
use_orjson: bool = False
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class APIConfig(BaseModel):
|
|
93
|
+
file_name: str
|
|
94
|
+
base_url: str
|
|
95
|
+
content: str
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class ConversionResult(BaseModel):
|
|
99
|
+
models: List[Model] = Field(default_factory=list)
|
|
100
|
+
clients: List[Model] = Field(default_factory=list)
|
|
101
|
+
exceptions: List[Model] = Field(default_factory=list)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAPI parsers for different specification versions.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .openapi_30 import generate_code_3_0, parse_openapi_3_0
|
|
6
|
+
from .openapi_31 import generate_code_3_1, parse_openapi_3_1
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"parse_openapi_3_0",
|
|
10
|
+
"generate_code_3_0",
|
|
11
|
+
"parse_openapi_3_1",
|
|
12
|
+
"generate_code_3_1",
|
|
13
|
+
]
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAPI 3.0 specific parsing and generation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
from openapi_pydantic.v3.v3_0 import OpenAPI
|
|
8
|
+
|
|
9
|
+
from ab_openapi_python_generator.common import HTTPLibrary, PydanticVersion
|
|
10
|
+
from ab_openapi_python_generator.language_converters.python.generator import (
|
|
11
|
+
generator as base_generator,
|
|
12
|
+
)
|
|
13
|
+
from ab_openapi_python_generator.models import ConversionResult
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def parse_openapi_3_0(spec_data: dict) -> OpenAPI:
|
|
17
|
+
"""
|
|
18
|
+
Parse OpenAPI 3.0 specification data.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
spec_data: Dictionary containing OpenAPI 3.0 specification
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
OpenAPI: Parsed OpenAPI 3.0 specification object
|
|
25
|
+
|
|
26
|
+
Raises:
|
|
27
|
+
ValidationError: If the specification is invalid
|
|
28
|
+
"""
|
|
29
|
+
return OpenAPI(**spec_data) # type: ignore - pydantic issue with extra fields
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def generate_code_3_0(
|
|
33
|
+
data: OpenAPI,
|
|
34
|
+
library: HTTPLibrary = HTTPLibrary.httpx,
|
|
35
|
+
env_token_name: Optional[str] = None,
|
|
36
|
+
use_orjson: bool = False,
|
|
37
|
+
custom_template_path: Optional[str] = None,
|
|
38
|
+
pydantic_version: PydanticVersion = PydanticVersion.V2,
|
|
39
|
+
) -> ConversionResult:
|
|
40
|
+
"""
|
|
41
|
+
Generate Python code from OpenAPI 3.0 specification.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
data: OpenAPI 3.0 specification object
|
|
45
|
+
library: HTTP library to use
|
|
46
|
+
env_token_name: Environment variable name for token
|
|
47
|
+
use_orjson: Whether to use orjson for serialization
|
|
48
|
+
custom_template_path: Custom template path
|
|
49
|
+
pydantic_version: Pydantic version to use
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
ConversionResult: Generated code and metadata
|
|
53
|
+
"""
|
|
54
|
+
from ab_openapi_python_generator.common import library_config_dict
|
|
55
|
+
|
|
56
|
+
library_config = library_config_dict[library]
|
|
57
|
+
|
|
58
|
+
return base_generator(
|
|
59
|
+
data=data,
|
|
60
|
+
library_config=library_config,
|
|
61
|
+
env_token_name=env_token_name,
|
|
62
|
+
use_orjson=use_orjson,
|
|
63
|
+
custom_template_path=custom_template_path,
|
|
64
|
+
pydantic_version=pydantic_version,
|
|
65
|
+
)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAPI 3.1 specific parsing and generation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
from openapi_pydantic.v3.v3_1 import OpenAPI
|
|
8
|
+
|
|
9
|
+
from ab_openapi_python_generator.common import HTTPLibrary, PydanticVersion
|
|
10
|
+
from ab_openapi_python_generator.language_converters.python.generator import (
|
|
11
|
+
generator as base_generator,
|
|
12
|
+
)
|
|
13
|
+
from ab_openapi_python_generator.models import ConversionResult
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def parse_openapi_3_1(spec_data: dict) -> OpenAPI:
|
|
17
|
+
"""
|
|
18
|
+
Parse OpenAPI 3.1 specification data.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
spec_data: Dictionary containing OpenAPI 3.1 specification
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
OpenAPI: Parsed OpenAPI 3.1 specification object
|
|
25
|
+
|
|
26
|
+
Raises:
|
|
27
|
+
ValidationError: If the specification is invalid
|
|
28
|
+
"""
|
|
29
|
+
return OpenAPI(**spec_data) # type: ignore - pydantic issue with extra fields
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def generate_code_3_1(
|
|
33
|
+
data: OpenAPI,
|
|
34
|
+
library: HTTPLibrary = HTTPLibrary.httpx,
|
|
35
|
+
env_token_name: Optional[str] = None,
|
|
36
|
+
use_orjson: bool = False,
|
|
37
|
+
custom_template_path: Optional[str] = None,
|
|
38
|
+
pydantic_version: PydanticVersion = PydanticVersion.V2,
|
|
39
|
+
) -> ConversionResult:
|
|
40
|
+
"""
|
|
41
|
+
Generate Python code from OpenAPI 3.1 specification.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
data: OpenAPI 3.1 specification object
|
|
45
|
+
library: HTTP library to use
|
|
46
|
+
env_token_name: Environment variable name for token
|
|
47
|
+
use_orjson: Whether to use orjson for serialization
|
|
48
|
+
custom_template_path: Custom template path
|
|
49
|
+
pydantic_version: Pydantic version to use
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
ConversionResult: Generated code and metadata
|
|
53
|
+
"""
|
|
54
|
+
from ab_openapi_python_generator.common import library_config_dict
|
|
55
|
+
|
|
56
|
+
library_config = library_config_dict[library]
|
|
57
|
+
|
|
58
|
+
return base_generator(
|
|
59
|
+
data=data,
|
|
60
|
+
library_config=library_config,
|
|
61
|
+
env_token_name=env_token_name,
|
|
62
|
+
use_orjson=use_orjson,
|
|
63
|
+
custom_template_path=custom_template_path,
|
|
64
|
+
pydantic_version=pydantic_version,
|
|
65
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAPI version detection utilities.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, Literal
|
|
6
|
+
|
|
7
|
+
OpenAPIVersion = Literal["3.0", "3.1"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def detect_openapi_version(spec_data: Dict[str, Any]) -> OpenAPIVersion:
|
|
11
|
+
"""
|
|
12
|
+
Detect the OpenAPI version from specification data.
|
|
13
|
+
|
|
14
|
+
Performs basic validation to ensure the specification is well-formed enough
|
|
15
|
+
to route to the appropriate parser. The actual parser will handle detailed
|
|
16
|
+
validation of the specification content.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
spec_data: Dictionary containing OpenAPI specification
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
OpenAPIVersion: Either "3.0" or "3.1"
|
|
23
|
+
|
|
24
|
+
Raises:
|
|
25
|
+
ValueError: If the specification is malformed or has unsupported version
|
|
26
|
+
"""
|
|
27
|
+
# Basic validation: must be a dictionary
|
|
28
|
+
if not isinstance(spec_data, dict):
|
|
29
|
+
raise ValueError("OpenAPI specification must be a dictionary/object")
|
|
30
|
+
|
|
31
|
+
# Basic validation: must have openapi field
|
|
32
|
+
if "openapi" not in spec_data:
|
|
33
|
+
raise ValueError("Missing required 'openapi' field in specification")
|
|
34
|
+
|
|
35
|
+
openapi_version = spec_data.get("openapi")
|
|
36
|
+
|
|
37
|
+
# Basic validation: openapi field must be a string
|
|
38
|
+
if not isinstance(openapi_version, str):
|
|
39
|
+
raise ValueError("'openapi' field must be a string")
|
|
40
|
+
|
|
41
|
+
# Basic validation: must not be empty
|
|
42
|
+
if not openapi_version.strip():
|
|
43
|
+
raise ValueError("'openapi' field cannot be empty")
|
|
44
|
+
|
|
45
|
+
# Version detection
|
|
46
|
+
if openapi_version.startswith("3.0"):
|
|
47
|
+
return "3.0"
|
|
48
|
+
elif openapi_version.startswith("3.1"):
|
|
49
|
+
return "3.1"
|
|
50
|
+
else:
|
|
51
|
+
raise ValueError(f"Unsupported OpenAPI version: {openapi_version}. Only OpenAPI 3.0.x and 3.1.x are supported.")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def is_openapi_30(spec_data: Dict[str, Any]) -> bool:
|
|
55
|
+
"""Check if the specification is OpenAPI 3.0.x"""
|
|
56
|
+
try:
|
|
57
|
+
return detect_openapi_version(spec_data) == "3.0"
|
|
58
|
+
except ValueError:
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def is_openapi_31(spec_data: Dict[str, Any]) -> bool:
|
|
63
|
+
"""Check if the specification is OpenAPI 3.1.x"""
|
|
64
|
+
try:
|
|
65
|
+
return detect_openapi_version(spec_data) == "3.1"
|
|
66
|
+
except ValueError:
|
|
67
|
+
return False
|
|
@@ -1,32 +1,27 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: ab-openapi-python-generator
|
|
3
|
-
Version: 2.1
|
|
3
|
+
Version: 2.2.1
|
|
4
4
|
Summary: Openapi Python Generator
|
|
5
|
-
|
|
6
|
-
License: MIT
|
|
7
|
-
Keywords: OpenAPI,Generator,Python,async
|
|
8
|
-
Author: Marco Müllner
|
|
9
|
-
Author-email: muellnermarco@gmail.com
|
|
10
|
-
Requires-Python: >=3.9,<4.0
|
|
11
|
-
Classifier: Development Status :: 3 - Alpha
|
|
12
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
-
Classifier: Programming Language :: Python :: 3
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
-
Requires-Dist: Jinja2 (>=3.1.2,<4.0.0)
|
|
18
|
-
Requires-Dist: black (>=21.10b0)
|
|
19
|
-
Requires-Dist: click (>=8.1.3,<9.0.0)
|
|
20
|
-
Requires-Dist: httpx[all] (>=0.28.0,<0.29.0)
|
|
21
|
-
Requires-Dist: importlib-metadata (>=8.7.0,<9.0.0)
|
|
22
|
-
Requires-Dist: isort (>=5.10.1)
|
|
23
|
-
Requires-Dist: openapi-pydantic (>=0.5.1,<0.6.0)
|
|
24
|
-
Requires-Dist: orjson (>=3.9.15,<4.0.0)
|
|
25
|
-
Requires-Dist: pydantic (>=2.10.2,<3.0.0)
|
|
26
|
-
Requires-Dist: pyyaml (>=6.0.2,<7.0.0)
|
|
27
|
-
Project-URL: Changelog, https://github.com/auth-broker/openapi-python-generator/releases
|
|
28
|
-
Project-URL: Documentation, https://openapi-python-generator.readthedocs.io
|
|
5
|
+
Project-URL: Homepage, https://github.com/auth-broker/openapi-python-generator
|
|
29
6
|
Project-URL: Repository, https://github.com/auth-broker/openapi-python-generator
|
|
7
|
+
Project-URL: Documentation, https://openapi-python-generator.readthedocs.io
|
|
8
|
+
Project-URL: Changelog, https://github.com/auth-broker/openapi-python-generator/releases
|
|
9
|
+
Author-email: Marco Müllner <muellnermarco@gmail.com>, Matt Coulter <mattcoul7@gmail.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: Generator,OpenAPI,Python,async
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Requires-Python: <4,>=3.9
|
|
15
|
+
Requires-Dist: black>=21.10b0
|
|
16
|
+
Requires-Dist: click<9,>=8.1.3
|
|
17
|
+
Requires-Dist: httpx[all]<0.29,>=0.28.0
|
|
18
|
+
Requires-Dist: importlib-metadata<9,>=8.7.0
|
|
19
|
+
Requires-Dist: isort>=5.10.1
|
|
20
|
+
Requires-Dist: jinja2<4,>=3.1.2
|
|
21
|
+
Requires-Dist: openapi-pydantic<0.6,>=0.5.1
|
|
22
|
+
Requires-Dist: orjson<4,>=3.9.15
|
|
23
|
+
Requires-Dist: pydantic<3,>=2.10.2
|
|
24
|
+
Requires-Dist: pyyaml<7,>=6.0.2
|
|
30
25
|
Description-Content-Type: text/markdown
|
|
31
26
|
|
|
32
27
|
# Openapi Python Generator
|
|
@@ -128,4 +123,3 @@ This project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter]
|
|
|
128
123
|
[license]: https://github.com/MarcoMuellner/openapi-python-generator/blob/main/LICENSE
|
|
129
124
|
[contributor guide]: https://github.com/MarcoMuellner/openapi-python-generator/blob/main/CONTRIBUTING.md
|
|
130
125
|
[Quick start page]: https://marcomuellner.github.io/openapi-python-generator/quick_start/
|
|
131
|
-
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
ab_openapi_python_generator/__init__.py,sha256=ph2EznAu4nkHFruaG542m4TQ_xAvDlf36uKxtAkVP1s,460
|
|
2
|
+
ab_openapi_python_generator/__main__.py,sha256=Vz0FsBvxQtyozKkZZFK_ag8XgJcx-5obRm44ogjD0Wk,2500
|
|
3
|
+
ab_openapi_python_generator/common.py,sha256=rh6AxoWyL-jb5WbeLhiohvFUBUnevn4N7ixHPKTIiDM,1221
|
|
4
|
+
ab_openapi_python_generator/generate_data.py,sha256=s71eWNKqEv5_v0Tfb2jnIME-JOag_Jz9gKxMGT5Veq8,7944
|
|
5
|
+
ab_openapi_python_generator/models.py,sha256=b0AyJalyZSvGyi5f5xj5ymd_s9iR09kH1UurrJDxxck,2312
|
|
6
|
+
ab_openapi_python_generator/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
ab_openapi_python_generator/version_detector.py,sha256=PUDTpgDMHljUzxhLhMrCzHMdS3mgYQibWDiI4-vLRB4,2104
|
|
8
|
+
ab_openapi_python_generator/language_converters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
ab_openapi_python_generator/language_converters/python/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
ab_openapi_python_generator/language_converters/python/client_generator.py,sha256=6TknEdUyhsBvvez0Nn_JoZuj8rmwX3k1OEnNIAzJ09A,17817
|
|
11
|
+
ab_openapi_python_generator/language_converters/python/common.py,sha256=gZHNhVC4OwGBlmr3h9Ffz3iILO1ykva9CrqhBfeYQXU,1616
|
|
12
|
+
ab_openapi_python_generator/language_converters/python/exception_generator.py,sha256=a4iX53W8qhQ99Tkdk2sHPCq3HvtZp5JctTbObgsRX7I,653
|
|
13
|
+
ab_openapi_python_generator/language_converters/python/generator.py,sha256=g-WTVLidcpDLqYW00VufmK0_jfRAM1ZVOSAcHyLAhOU,1664
|
|
14
|
+
ab_openapi_python_generator/language_converters/python/jinja_config.py,sha256=EsASthmgh_XrwR7GiDooTyTiiIoqScz9HymvUpNBYFY,1210
|
|
15
|
+
ab_openapi_python_generator/language_converters/python/model_generator.py,sha256=RoHO-R-fgmBL1osmzIoPEmYAUg-9BxUSThFSbUvQYoA,34543
|
|
16
|
+
ab_openapi_python_generator/language_converters/python/templates/alias_union.jinja2,sha256=tndnrDky4srNRyB_HnUMZFW7HtoJvEfwIFteYIu0ELc,468
|
|
17
|
+
ab_openapi_python_generator/language_converters/python/templates/async_client_httpx_pydantic_2.jinja2,sha256=UqLgyv4_Mk7LbvB9_wP4afmhWRRE-42w3mq1Sdbr8JY,2414
|
|
18
|
+
ab_openapi_python_generator/language_converters/python/templates/discriminator_enum.jinja2,sha256=GJZ_ih1eURnJRPZq5P_3aKhYB5iJHymj2fpuNSdM90Y,163
|
|
19
|
+
ab_openapi_python_generator/language_converters/python/templates/enum.jinja2,sha256=7dewyMLifUTe9xjCTE62deuncPiNkp7eGkIaY-IWsLo,263
|
|
20
|
+
ab_openapi_python_generator/language_converters/python/templates/http_exception.jinja2,sha256=BHy48PD74aU74NGWC98iciacmGotHuWw9CaOqnCL8Kc,294
|
|
21
|
+
ab_openapi_python_generator/language_converters/python/templates/models.jinja2,sha256=s5PllbxiEENsZ5_fKSh1gKMJWhcLfBdH5744nKnAZsQ,797
|
|
22
|
+
ab_openapi_python_generator/language_converters/python/templates/models_pydantic_2.jinja2,sha256=o6-OlV5upoZOAVx6aQbsaigPMTCi4Uuec-oP1FNDJ60,904
|
|
23
|
+
ab_openapi_python_generator/language_converters/python/templates/sync_client_httpx_pydantic_2.jinja2,sha256=wGDXmQs1WTkMdHv-hE-UrHoWgY4hfA7vf7LujODeDRw,2390
|
|
24
|
+
ab_openapi_python_generator/parsers/__init__.py,sha256=UITDIV7rp3HAA1M8kQc_9cNG6e1tS2vDa4mTKua6aMY,300
|
|
25
|
+
ab_openapi_python_generator/parsers/openapi_30.py,sha256=2Hq5Vl3V4NUHKol-OO2nmt8-Fh6Lqk-hJUys8s-4UNI,1924
|
|
26
|
+
ab_openapi_python_generator/parsers/openapi_31.py,sha256=jyKYAKDRkqKUPSeP6xZuM6ZCE0JjuowAe_pbm01iqWE,1924
|
|
27
|
+
ab_openapi_python_generator-2.2.1.dist-info/METADATA,sha256=MZ9kTdhD6LGNqCphxqY2o59_hJyoSCLPhHwuzTFAl-A,5186
|
|
28
|
+
ab_openapi_python_generator-2.2.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
29
|
+
ab_openapi_python_generator-2.2.1.dist-info/entry_points.txt,sha256=zezbCs2qP8lSUbcjL6cN5XHoHxf9JTe15CZYH8Yuzo0,90
|
|
30
|
+
ab_openapi_python_generator-2.2.1.dist-info/licenses/LICENSE,sha256=0myanGwJ2vUOZN12aN95o0My6XEysnnVlbKikYw3pHg,1070
|
|
31
|
+
ab_openapi_python_generator-2.2.1.dist-info/RECORD,,
|