|
15 | 15 |
|
16 | 16 | from pydantic import VERSION, BaseModel, Extra, create_model |
17 | 17 |
|
18 | | -try: |
19 | | - from pydantic.generics import GenericModel |
20 | | -except ImportError: |
21 | | - GenericModel = None |
| 18 | +V2 = True if VERSION.startswith("2") else False |
| 19 | + |
| 20 | +if not V2: |
| 21 | + try: |
| 22 | + from pydantic.generics import GenericModel |
| 23 | + except ImportError: |
| 24 | + GenericModel = None |
22 | 25 |
|
23 | 26 | logger = logging.getLogger("pydantic2ts") |
24 | 27 |
|
25 | 28 |
|
26 | 29 | DEBUG = os.environ.get("DEBUG", False) |
27 | 30 |
|
28 | | -V2 = True if VERSION.startswith("2") else False |
29 | | - |
30 | 31 |
|
31 | 32 | def import_module(path: str) -> ModuleType: |
32 | 33 | """ |
@@ -202,16 +203,16 @@ def generate_json_schema_v2(models: List[Type[BaseModel]]) -> str: |
202 | 203 |
|
203 | 204 | try: |
204 | 205 | for m in models: |
205 | | - if m.model_config.get("extra") != Extra.allow: |
206 | | - m.model_config["extra"] = Extra.forbid |
| 206 | + if m.model_config.get("extra") != "allow": |
| 207 | + m.model_config["extra"] = "forbid" |
207 | 208 |
|
208 | 209 | master_model: BaseModel = create_model( |
209 | 210 | "_Master_", **{m.__name__: (m, ...) for m in models} |
210 | 211 | ) |
211 | | - master_model.model_config["extra"] = Extra.forbid |
| 212 | + master_model.model_config["extra"] = "forbid" |
212 | 213 | master_model.model_config["json_schema_extra"] = staticmethod(clean_schema) |
213 | 214 |
|
214 | | - schema: dict = master_model.model_json_schema() |
| 215 | + schema: dict = master_model.model_json_schema(mode="serialization") |
215 | 216 |
|
216 | 217 | for d in schema.get("$defs", {}).values(): |
217 | 218 | clean_schema(d) |
@@ -260,7 +261,9 @@ def generate_typescript_defs( |
260 | 261 | f.write(schema) |
261 | 262 |
|
262 | 263 | if DEBUG: |
263 | | - with open(Path(output).parent / "schema.json", "w") as f: |
| 264 | + debug_schema_file_path = Path(module).parent / "schema_debug.json" |
| 265 | + # raise ValueError(module) |
| 266 | + with open(debug_schema_file_path, "w") as f: |
264 | 267 | f.write(schema) |
265 | 268 |
|
266 | 269 | logger.info("Converting JSON schema to typescript definitions...") |
|
0 commit comments