Skip to content

Commit ba757db

Browse files
Add --skip-root-model option to skip generating root model (#2572)
* feat: add --skip-root-model option to skip generating the root model * docs: update command help in README 🤖 Generated by GitHub Actions * docs: clarify help text for --skip-root-model option --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0d64c0f commit ba757db

12 files changed

Lines changed: 75 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ Model customization:
478478
Set name of models defined inline from the parent model
479479
--reuse-model Reuse models on the field when a module has the model with the same
480480
content
481+
--skip-root-model Skip generating the model for the root schema element
481482
--target-python-version {3.9,3.10,3.11,3.12,3.13,3.14}
482483
target python version
483484
--treat-dot-as-module

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ Model customization:
470470
Set name of models defined inline from the parent model
471471
--reuse-model Reuse models on the field when a module has the model with the same
472472
content
473+
--skip-root-model Skip generating the model for the root schema element
473474
--target-python-version {3.9,3.10,3.11,3.12,3.13,3.14}
474475
target python version
475476
--treat-dot-as-module

src/datamodel_code_generator/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def generate( # noqa: PLR0912, PLR0913, PLR0914, PLR0915
337337
use_double_quotes: bool = False,
338338
use_union_operator: bool = False,
339339
collapse_root_models: bool = False,
340+
skip_root_model: bool = False,
340341
use_type_alias: bool = False,
341342
special_field_name_prefix: str | None = None,
342343
remove_special_field_name_prefix: bool = False,
@@ -568,6 +569,7 @@ def get_header_and_first_line(csv_file: IO[str]) -> dict[str, Any]:
568569
use_double_quotes=use_double_quotes,
569570
use_union_operator=use_union_operator,
570571
collapse_root_models=collapse_root_models,
572+
skip_root_model=skip_root_model,
571573
use_type_alias=use_type_alias,
572574
special_field_name_prefix=special_field_name_prefix,
573575
remove_special_field_name_prefix=remove_special_field_name_prefix,

src/datamodel_code_generator/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def validate_root(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805
387387
original_field_name_delimiter: Optional[str] = None # noqa: UP045
388388
use_double_quotes: bool = False
389389
collapse_root_models: bool = False
390+
skip_root_model: bool = False
390391
use_type_alias: bool = False
391392
special_field_name_prefix: Optional[str] = None # noqa: UP045
392393
remove_special_field_name_prefix: bool = False
@@ -643,6 +644,7 @@ def main(args: Sequence[str] | None = None) -> Exit: # noqa: PLR0911, PLR0912,
643644
original_field_name_delimiter=config.original_field_name_delimiter,
644645
use_double_quotes=config.use_double_quotes,
645646
collapse_root_models=config.collapse_root_models,
647+
skip_root_model=config.skip_root_model,
646648
use_type_alias=config.use_type_alias,
647649
use_union_operator=config.use_union_operator,
648650
special_field_name_prefix=config.special_field_name_prefix,

src/datamodel_code_generator/arguments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ def start_section(self, heading: str | None) -> None:
154154
default=None,
155155
help="Models generated with a root-type field will be merged into the models using that root-type model",
156156
)
157+
model_options.add_argument(
158+
"--skip-root-model",
159+
action="store_true",
160+
default=None,
161+
help="Skip generating the model for the root schema element",
162+
)
157163
model_options.add_argument(
158164
"--disable-appending-item-suffix",
159165
help="Disable appending `Item` suffix to model name in an array",

src/datamodel_code_generator/parser/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def __init__( # noqa: PLR0913, PLR0915
434434
use_union_operator: bool = False,
435435
allow_responses_without_content: bool = False,
436436
collapse_root_models: bool = False,
437+
skip_root_model: bool = False,
437438
use_type_alias: bool = False,
438439
special_field_name_prefix: str | None = None,
439440
remove_special_field_name_prefix: bool = False,
@@ -569,6 +570,7 @@ def __init__( # noqa: PLR0913, PLR0915
569570
self.use_double_quotes = use_double_quotes
570571
self.allow_responses_without_content = allow_responses_without_content
571572
self.collapse_root_models = collapse_root_models
573+
self.skip_root_model = skip_root_model
572574
self.use_type_alias = use_type_alias
573575
self.capitalise_enum_members = capitalise_enum_members
574576
self.keep_model_order = keep_model_order

src/datamodel_code_generator/parser/graphql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def __init__( # noqa: PLR0913
155155
use_union_operator: bool = False,
156156
allow_responses_without_content: bool = False,
157157
collapse_root_models: bool = False,
158+
skip_root_model: bool = False,
158159
use_type_alias: bool = False,
159160
special_field_name_prefix: str | None = None,
160161
remove_special_field_name_prefix: bool = False,
@@ -240,6 +241,7 @@ def __init__( # noqa: PLR0913
240241
use_union_operator=use_union_operator,
241242
allow_responses_without_content=allow_responses_without_content,
242243
collapse_root_models=collapse_root_models,
244+
skip_root_model=skip_root_model,
243245
use_type_alias=use_type_alias,
244246
special_field_name_prefix=special_field_name_prefix,
245247
remove_special_field_name_prefix=remove_special_field_name_prefix,

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ def __init__( # noqa: PLR0913
508508
use_union_operator: bool = False,
509509
allow_responses_without_content: bool = False,
510510
collapse_root_models: bool = False,
511+
skip_root_model: bool = False,
511512
use_type_alias: bool = False,
512513
special_field_name_prefix: str | None = None,
513514
remove_special_field_name_prefix: bool = False,
@@ -593,6 +594,7 @@ def __init__( # noqa: PLR0913
593594
use_union_operator=use_union_operator,
594595
allow_responses_without_content=allow_responses_without_content,
595596
collapse_root_models=collapse_root_models,
597+
skip_root_model=skip_root_model,
596598
use_type_alias=use_type_alias,
597599
special_field_name_prefix=special_field_name_prefix,
598600
remove_special_field_name_prefix=remove_special_field_name_prefix,
@@ -1894,7 +1896,7 @@ def _parse_file(
18941896
models = get_model_by_path(raw, object_paths)
18951897
model_name = object_paths[-1]
18961898
self.parse_obj(model_name, self.SCHEMA_OBJECT_TYPE.parse_obj(models), path)
1897-
else:
1899+
elif not self.skip_root_model:
18981900
self.parse_obj(obj_name, root_obj, path_parts or ["#"])
18991901
for key, model in definitions.items():
19001902
path = [*path_parts, _schema_path, key]

src/datamodel_code_generator/parser/openapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def __init__( # noqa: PLR0913
237237
use_union_operator: bool = False,
238238
allow_responses_without_content: bool = False,
239239
collapse_root_models: bool = False,
240+
skip_root_model: bool = False,
240241
use_type_alias: bool = False,
241242
special_field_name_prefix: str | None = None,
242243
remove_special_field_name_prefix: bool = False,
@@ -322,6 +323,7 @@ def __init__( # noqa: PLR0913
322323
use_union_operator=use_union_operator,
323324
allow_responses_without_content=allow_responses_without_content,
324325
collapse_root_models=collapse_root_models,
326+
skip_root_model=skip_root_model,
325327
use_type_alias=use_type_alias,
326328
special_field_name_prefix=special_field_name_prefix,
327329
remove_special_field_name_prefix=remove_special_field_name_prefix,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# generated by datamodel-codegen:
2+
# filename: skip_root_model_test.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import Optional
8+
9+
from pydantic import BaseModel
10+
11+
12+
class Person(BaseModel):
13+
name: str
14+
age: Optional[int] = None

0 commit comments

Comments
 (0)