Skip to content

Commit fd566ce

Browse files
author
Joakim Nordling
committed
Change so you can only control if you want strict validation or not
1 parent d49d3b4 commit fd566ce

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

openapi_to_fastapi/model_generator.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import uuid
44
from contextlib import contextmanager, suppress
55
from pathlib import Path
6-
from typing import Literal, Optional
76

87
from datamodel_code_generator import DatetimeClassType, PythonVersion
98
from datamodel_code_generator.model import pydantic_v2 as pydantic_model
@@ -16,22 +15,18 @@
1615
def generate_model_from_schema(
1716
schema: str,
1817
format_code: bool = False,
19-
extra_fields: Optional[Literal["allow", "ignore", "forbid"]] = None,
20-
use_strict_types: bool = False,
21-
use_strict_dates: bool = False,
18+
strict_validation: bool = False,
2219
) -> str:
2320
"""
2421
Given an OpenAPI schema, generate pydantic models from everything defined
2522
in the "components/schemas" section
2623
2724
:param schema: Content of an OpenAPI spec, plain text
2825
:param format_code: Whether to format generated code
29-
:param extra_fields: What to do with extra fields
30-
:param use_strict_types: Whether to use strict types
31-
:param use_strict_dates: Whether to be strict about date and date times
26+
:param strict_validation: Whether to use strict validation
3227
:return: Importable python code with generated models
3328
"""
34-
if use_strict_types:
29+
if strict_validation:
3530
strict_types = (
3631
StrictTypes.str,
3732
StrictTypes.bytes,
@@ -42,7 +37,7 @@ def generate_model_from_schema(
4237
else:
4338
strict_types = None
4439

45-
if use_strict_dates:
40+
if strict_validation:
4641
target_datetime_class = DatetimeClassType.Awaredatetime
4742
else:
4843
target_datetime_class = DatetimeClassType.Datetime
@@ -58,7 +53,7 @@ def generate_model_from_schema(
5853
extra_template_data=None,
5954
target_python_version=PythonVersion.PY_39,
6055
dump_resolve_reference_action=None,
61-
extra_fields=extra_fields,
56+
extra_fields="forbid" if strict_validation else None,
6257
strict_types=strict_types,
6358
field_constraints=False,
6459
snake_case_field=False,
@@ -69,7 +64,7 @@ def generate_model_from_schema(
6964

7065
result = str(parser.parse(format_=format_code))
7166

72-
if use_strict_dates:
67+
if strict_validation:
7368
result = override_with_stricter_dates(result)
7469

7570
return result
@@ -91,9 +86,7 @@ def load_models(
9186
name: str = "",
9287
cleanup: bool = True,
9388
format_code: bool = False,
94-
extra_fields: Optional[Literal["allow", "ignore", "forbid"]] = None,
95-
use_strict_types: bool = False,
96-
use_strict_dates: bool = False,
89+
strict_validation: bool = False,
9790
):
9891
"""
9992
Generate pydantic models from OpenAPI spec and return a python module,
@@ -105,10 +98,7 @@ def load_models(
10598
:param name: Prefix for a module name, optional
10699
:param cleanup: Whether to remove a file with models afterwards
107100
:param format_code: Whether to format generated code
108-
:param extra_fields: What to do with extra fields, None falls back to default for
109-
pydantic, i.e. ignore.
110-
:param use_strict_types: Whether to use strict types
111-
:param use_strict_dates: Whether to be strict about date and date times
101+
:param strict_validation: Whether to use strict validation
112102
:return: Module with pydantic models
113103
"""
114104
prefix = name.replace("/", "").replace(" ", "").replace("\\", "") + "_"
@@ -118,9 +108,7 @@ def load_models(
118108
),
119109
delete=cleanup,
120110
) as tmp_file:
121-
model_py = generate_model_from_schema(
122-
schema, format_code, extra_fields, use_strict_types, use_strict_dates
123-
)
111+
model_py = generate_model_from_schema(schema, format_code, strict_validation)
124112
tmp_file.write(model_py)
125113
if not cleanup:
126114
logger.info("Generated module %s: %s", name, tmp_file.name)

openapi_to_fastapi/routes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ def _validate_and_parse_specs(self, cleanup=True):
125125
path,
126126
cleanup=cleanup,
127127
format_code=self._format_code,
128-
extra_fields="forbid" if strict_validation else None,
129-
use_strict_types=strict_validation,
130-
use_strict_dates=strict_validation,
128+
strict_validation=strict_validation,
131129
)
132130
post = path_item.post
133131
if post:

0 commit comments

Comments
 (0)