Skip to content

Pydantic v2 #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The key features of the generator are:
- __Support for multiple rest frameworks.__ __OpenAPI python generator__ currently supports the following:
- [httpx](https://pypi.org/project/httpx/)
- [requests](https://pypi.org/project/requests/)
- [aiohttp](https://pypi.org/project/aiohttp/)
- __Async and sync code generation support__, depending on the framework. It will automatically create both for frameworks that support both.
- __Easily extendable using Jinja2 templates__. The code is designed to be easily extendable and should support even more languages and frameworks in the future.
- __Fully tested__. Every generated code is automatically tested against the OpenAPI spec and we have 100% coverage.
Expand Down
28 changes: 23 additions & 5 deletions docs/references/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@ Arguments:

Options:
```console
--library The library to use. Defaults to `httpx`.
--env-token-name The name of the environment variable to use for the API key. Defaults to `access_token`.
--use-orjson Use the `orjson` library for serialization. Defaults to `false`.
--custom-template-path Use a custom template path to override the built in templates.
-h, --help Show this help message and exit.
--library [httpx|requests|aiohttp]
HTTP library to use in the generation of the client.
Defaults to 'httpx'.

--env-token-name TEXT Name of the environment variable that contains the token.
If set, code expects this environment variable and will
raise an error if not set.
Defaults to 'access_token'.

--use-orjson Use the orjson library for JSON serialization. Enables
faster processing and better type support.
Defaults to False.

--custom-template-path TEXT
Custom template path to use. Allows overriding of the
built in templates.

--pydantic-version [v1|v2]
Pydantic version to use for generated models.
Defaults to 'v2'.

--version Show the version and exit.
-h, --help Show this help message and exit.
```
2,973 changes: 1,707 additions & 1,266 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openapi-python-generator"
version = "0.5.0"
version = "1.0.0"
description = "Openapi Python Generator"
authors = ["Marco Müllner <[email protected]>"]
license = "MIT"
Expand All @@ -17,15 +17,15 @@ keywords = ["OpenAPI", "Generator", "Python", "async"]
Changelog = "https://github.com/MarcoMuellner/openapi-python-generator/releases"

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"
httpx = {extras = ["all"], version = "^0.23.0"}
pydantic = "^1.9.1"
orjson = "^3.7.2"
openapi-schema-pydantic = "^1.2.3"
pydantic = "^2.10.2"
orjson = "^3.9.15"
Jinja2 = "^3.1.2"
click = "^8.1.3"
black = ">=21.10b0"
isort = ">=5.10.1"
openapi-pydantic = "^0.5.0"

[tool.poetry.dev-dependencies]
Pygments = ">=2.10.0"
Expand All @@ -48,7 +48,7 @@ typeguard = ">=2.13.3"
xdoctest = {extras = ["colors"], version = ">=0.15.10"}
myst-parser = {version = ">=0.16.1"}
pytest-cov = "^3.0.0"
fastapi = "^0.78.0"
fastapi = "^0.115.5"
uvicorn = "^0.18.1"
respx = "^0.20.1"
aiohttp = "^3.8.3"
Expand Down
16 changes: 12 additions & 4 deletions src/openapi_python_generator/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Optional
from enum import Enum

import click

from openapi_python_generator import __version__
from openapi_python_generator.common import HTTPLibrary
from openapi_python_generator.common import HTTPLibrary, PydanticVersion
from openapi_python_generator.generate_data import generate_data


@click.command()
@click.argument("source")
@click.argument("output")
Expand Down Expand Up @@ -38,6 +38,13 @@
default=None,
help="Custom template path to use. Allows overriding of the built in templates",
)
@click.option(
"--pydantic-version",
type=click.Choice(["v1", "v2"]),
default="v2",
show_default=True,
help="Pydantic version to use for generated models.",
)
@click.version_option(version=__version__)
def main(
source: str,
Expand All @@ -46,6 +53,7 @@ def main(
env_token_name: Optional[str] = None,
use_orjson: bool = False,
custom_template_path: Optional[str] = None,
pydantic_version: PydanticVersion = PydanticVersion.V2,
) -> None:
"""
Generate Python code from an OpenAPI 3.0 specification.
Expand All @@ -54,9 +62,9 @@ def main(
an OUTPUT path, where the resulting client is created.
"""
generate_data(
source, output, library, env_token_name, use_orjson, custom_template_path
source, output, library, env_token_name, use_orjson, custom_template_path,pydantic_version
)


if __name__ == "__main__": # pragma: no cover
main()
main()
4 changes: 4 additions & 0 deletions src/openapi_python_generator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class HTTPLibrary(str, Enum):
requests = "requests"
aiohttp = "aiohttp"

class PydanticVersion(str, Enum):
V1 = "v1"
V2 = "v2"


library_config_dict: Dict[Optional[HTTPLibrary], LibraryConfig] = {
HTTPLibrary.httpx: LibraryConfig(
Expand Down
9 changes: 6 additions & 3 deletions src/openapi_python_generator/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from black import NothingChanged
from httpx import ConnectError
from httpx import ConnectTimeout
from openapi_schema_pydantic import OpenAPI
from openapi_pydantic.v3.v3_0 import OpenAPI
from pydantic import ValidationError

from .common import HTTPLibrary
from .common import HTTPLibrary, PydanticVersion
from .common import library_config_dict
from .language_converters.python.generator import generator
from .language_converters.python.jinja_config import SERVICE_TEMPLATE
Expand Down Expand Up @@ -56,7 +56,8 @@ def get_open_api(source: Union[str, Path]) -> OpenAPI:
return OpenAPI(**orjson.loads(httpx.get(source).text))

with open(source, "r") as f:
return OpenAPI(**orjson.loads(f.read()))
file_content = f.read()
return OpenAPI(**orjson.loads(file_content))
except FileNotFoundError:
click.echo(
f"File {source} not found. Please make sure to pass the path to the OpenAPI 3.0 specification."
Expand Down Expand Up @@ -139,6 +140,7 @@ def generate_data(
env_token_name: Optional[str] = None,
use_orjson: bool = False,
custom_template_path: Optional[str] = None,
pydantic_version: PydanticVersion = PydanticVersion.V2,
) -> None:
"""
Generate Python code from an OpenAPI 3.0 specification.
Expand All @@ -152,6 +154,7 @@ def generate_data(
env_token_name,
use_orjson,
custom_template_path,
pydantic_version,
)

write_data(result, output)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Optional

from openapi_schema_pydantic import OpenAPI
from openapi_pydantic.v3.v3_0 import OpenAPI

from openapi_python_generator.common import PydanticVersion
from openapi_python_generator.language_converters.python.jinja_config import (
API_CONFIG_TEMPLATE,
API_CONFIG_TEMPLATE, API_CONFIG_TEMPLATE_PYDANTIC_V2,
)
from openapi_python_generator.language_converters.python.jinja_config import (
create_jinja_env,
Expand All @@ -12,15 +13,18 @@


def generate_api_config(
data: OpenAPI, env_token_name: Optional[str] = None
data: OpenAPI, env_token_name: Optional[str] = None,
pydantic_version: PydanticVersion = PydanticVersion.V2,
) -> APIConfig:
"""
Generate the API model.
"""

template_name = API_CONFIG_TEMPLATE_PYDANTIC_V2 if pydantic_version == PydanticVersion.V2 else API_CONFIG_TEMPLATE
jinja_env = create_jinja_env()
return APIConfig(
file_name="api_config",
content=jinja_env.get_template(API_CONFIG_TEMPLATE).render(
content=jinja_env.get_template(template_name).render(
env_token_name=env_token_name, **data.dict()
),
base_url=data.servers[0].url if len(data.servers) > 0 else "NO SERVER",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Optional

from openapi_schema_pydantic import OpenAPI
from openapi_pydantic.v3.v3_0 import OpenAPI

from openapi_python_generator.common import PydanticVersion
from openapi_python_generator.language_converters.python import common
from openapi_python_generator.language_converters.python.api_config_generator import (
generate_api_config,
Expand All @@ -22,6 +23,7 @@ def generator(
env_token_name: Optional[str] = None,
use_orjson: bool = False,
custom_template_path: Optional[str] = None,
pydantic_version: PydanticVersion = PydanticVersion.V2,
) -> ConversionResult:
"""
Generate Python code from an OpenAPI 3.0 specification.
Expand All @@ -31,7 +33,7 @@ def generator(
common.set_custom_template_path(custom_template_path)

if data.components is not None:
models = generate_models(data.components)
models = generate_models(data.components, pydantic_version)
else:
models = []

Expand All @@ -40,7 +42,7 @@ def generator(
else:
services = []

api_config = generate_api_config(data, env_token_name)
api_config = generate_api_config(data, env_token_name, pydantic_version)

return ConversionResult(
models=models,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

ENUM_TEMPLATE = "enum.jinja2"
MODELS_TEMPLATE = "models.jinja2"
MODELS_TEMPLATE_PYDANTIC_V2 = "models_pydantic_2.jinja2"
SERVICE_TEMPLATE = "service.jinja2"
HTTPX_TEMPLATE = "httpx.jinja2"
API_CONFIG_TEMPLATE = "apiconfig.jinja2"
API_CONFIG_TEMPLATE_PYDANTIC_V2 = "apiconfig_pydantic_2.jinja2"
TEMPLATE_PATH = Path(__file__).parent / "templates"


Expand Down
Loading
Loading