Skip to content

Commit 8865201

Browse files
authored
Merge branch 'master' into 419/callbacks
2 parents 6eb5d02 + 2f41065 commit 8865201

File tree

5 files changed

+229
-217
lines changed

5 files changed

+229
-217
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ Options:
3939
-r, --generate-routers Generate modular api with multiple routers using RouterAPI (for bigger applications).
4040
--specify-tags Use along with --generate-routers to generate specific routers from given list of tags.
4141
-c, --custom-visitors PATH - A custom visitor that adds variables to the template.
42+
-d, --output-model-type Specify a Pydantic base model to use (see [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator); default is `pydantic.BaseModel`).
43+
-p, --python-version Specify a Python version to target (default is `3.8`).
4244
--install-completion Install completion for the current shell.
4345
--show-completion Show completion for the current shell, to copy it
4446
or customize the installation.
4547
--help Show this message and exit.
4648
```
4749

50+
### Pydantic 2 support
51+
52+
Specify the Pydantic 2 `BaseModel` version in the command line, for example:
53+
54+
```sh
55+
$ fastapi-codegen --input api.yaml --output app --output-model-type pydantic_v2.BaseModel
56+
```
57+
4858
## Example
4959
### OpenAPI
5060
```sh

docs/development-contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ git clone [email protected]:<your username>/fastapi-code-generator.git
1818
$ cd fastapi-code-generator
1919

2020
## 2. Install [poetry](https://github.com/python-poetry/poetry)
21-
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
21+
$ curl -sSL curl -sSL https://install.python-poetry.org | python3 -
2222

2323
## 3. Install dependencies
2424
$ poetry install

fastapi_code_generator/__main__.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from typing import Any, Dict, List, Optional
66

77
import typer
8-
from datamodel_code_generator import LiteralType, PythonVersion, chdir
8+
from datamodel_code_generator import DataModelType, LiteralType, PythonVersion, chdir
99
from datamodel_code_generator.format import CodeFormatter
1010
from datamodel_code_generator.imports import Import, Imports
11+
from datamodel_code_generator.model import get_data_model_types
1112
from datamodel_code_generator.reference import Reference
1213
from datamodel_code_generator.types import DataType
1314
from jinja2 import Environment, FileSystemLoader
@@ -57,6 +58,12 @@ def main(
5758
None, "--custom-visitor", "-c"
5859
),
5960
disable_timestamp: bool = typer.Option(False, "--disable-timestamp"),
61+
output_model_type: DataModelType = typer.Option(
62+
DataModelType.PydanticBaseModel.value, "--output-model-type", "-d"
63+
),
64+
python_version: PythonVersion = typer.Option(
65+
PythonVersion.PY_38.value, "--python-version", "-p"
66+
),
6067
) -> None:
6168
input_name: str = input_file
6269
input_text: str
@@ -69,31 +76,20 @@ def main(
6976
else:
7077
model_path = MODEL_PATH
7178

72-
if enum_field_as_literal:
73-
return generate_code(
74-
input_name,
75-
input_text,
76-
encoding,
77-
output_dir,
78-
template_dir,
79-
model_path,
80-
enum_field_as_literal, # type: ignore[arg-type]
81-
custom_visitors=custom_visitors,
82-
disable_timestamp=disable_timestamp,
83-
generate_routers=generate_routers,
84-
specify_tags=specify_tags,
85-
)
8679
return generate_code(
8780
input_name,
8881
input_text,
8982
encoding,
9083
output_dir,
9184
template_dir,
9285
model_path,
86+
enum_field_as_literal=enum_field_as_literal or None,
9387
custom_visitors=custom_visitors,
9488
disable_timestamp=disable_timestamp,
9589
generate_routers=generate_routers,
9690
specify_tags=specify_tags,
91+
output_model_type=output_model_type,
92+
python_version=python_version,
9793
)
9894

9995

@@ -119,6 +115,8 @@ def generate_code(
119115
disable_timestamp: bool = False,
120116
generate_routers: Optional[bool] = None,
121117
specify_tags: Optional[str] = None,
118+
output_model_type: DataModelType = DataModelType.PydanticBaseModel,
119+
python_version: PythonVersion = PythonVersion.PY_38,
122120
) -> None:
123121
if not model_path:
124122
model_path = MODEL_PATH
@@ -130,10 +128,19 @@ def generate_code(
130128
template_dir = (
131129
BUILTIN_MODULAR_TEMPLATE_DIR if generate_routers else BUILTIN_TEMPLATE_DIR
132130
)
133-
if enum_field_as_literal:
134-
parser = OpenAPIParser(input_text, enum_field_as_literal=enum_field_as_literal) # type: ignore[arg-type]
135-
else:
136-
parser = OpenAPIParser(input_text)
131+
132+
data_model_types = get_data_model_types(output_model_type, python_version)
133+
134+
parser = OpenAPIParser(
135+
input_text,
136+
enum_field_as_literal=enum_field_as_literal,
137+
data_model_type=data_model_types.data_model,
138+
data_model_root_type=data_model_types.root_model,
139+
data_model_field_type=data_model_types.field_model,
140+
data_type_manager_type=data_model_types.data_type_manager,
141+
dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
142+
)
143+
137144
with chdir(output_dir):
138145
models = parser.parse()
139146
output = output_dir / model_path
@@ -153,7 +160,7 @@ def generate_code(
153160
)
154161

155162
results: Dict[Path, str] = {}
156-
code_formatter = CodeFormatter(PythonVersion.PY_38, Path().resolve())
163+
code_formatter = CodeFormatter(python_version, Path().resolve())
157164

158165
template_vars: Dict[str, object] = {"info": parser.parse_info()}
159166
visitors: List[Visitor] = []

0 commit comments

Comments
 (0)