33import uuid
44from contextlib import contextmanager , suppress
55from pathlib import Path
6- from typing import Literal , Optional
76
87from datamodel_code_generator import DatetimeClassType , PythonVersion
98from datamodel_code_generator .model import pydantic_v2 as pydantic_model
1615def 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 )
0 commit comments