Skip to content

Commit 8c4bfd1

Browse files
an5tkoxudaxi
andauthored
Fix all-export generation for hyphenated directories (#3033)
Fixes a bug where --all-exports-scope fails to generate __all__ imports for packages originating from schema directories with hyphens in the path. Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
1 parent 3c244da commit 8c4bfd1

5 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,14 +2592,16 @@ def _collect_exports_for_init(
25922592
) -> list[tuple[str, tuple[str, ...], str]]:
25932593
"""Collect exports for __init__.py based on scope."""
25942594
exports: list[tuple[str, tuple[str, ...], str]] = []
2595-
base = module[:-1] if module[-1] == "__init__.py" else module
2595+
normalized_module = tuple(part.replace("-", "_") for part in module)
2596+
base = normalized_module[:-1] if normalized_module[-1] == "__init__.py" else normalized_module
25962597
base_len = len(base)
25972598

25982599
for proc_module, _, proc_models, _, _, _ in processed_models:
2599-
if not proc_models or proc_module == module:
2600+
normalized_proc_module = tuple(part.replace("-", "_") for part in proc_module)
2601+
if not proc_models or normalized_proc_module == normalized_module:
26002602
continue
2601-
last = proc_module[-1]
2602-
prefix = proc_module[:-1] if last == "__init__.py" else (*proc_module[:-1], last[:-3])
2603+
last = normalized_proc_module[-1]
2604+
prefix = normalized_proc_module[:-1] if last == "__init__.py" else (*normalized_proc_module[:-1], last[:-3])
26032605
if prefix[:base_len] != base or (depth := len(prefix) - base_len) < 1:
26042606
continue
26052607
if scope == AllExportsScope.Children and depth != 1:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated by datamodel-codegen:
2+
# filename: all_exports_hyphenated_directory
3+
4+
from __future__ import annotations
5+
6+
from .order import Order
7+
8+
__all__ = [
9+
"Order",
10+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated by datamodel-codegen:
2+
# filename: hyphenated-directory/order.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel
7+
8+
9+
class Order(BaseModel):
10+
id: str
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"title": "Order",
5+
"properties": {
6+
"id": {
7+
"type": "string"
8+
}
9+
},
10+
"required": ["id"]
11+
}

tests/main/test_main_general.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,21 @@ def test_all_exports_scope_children_with_local_models(output_dir: Path) -> None:
12881288
)
12891289

12901290

1291+
def test_all_exports_scope_children_jsonschema_hyphenated_package(output_dir: Path) -> None:
1292+
"""Test --all-exports-scope=children with hyphenated JSON Schema directories."""
1293+
run_main_and_assert(
1294+
input_path=JSON_SCHEMA_DATA_PATH / "all_exports_hyphenated_directory",
1295+
output_path=output_dir,
1296+
input_file_type="jsonschema",
1297+
extra_args=[
1298+
"--disable-timestamp",
1299+
"--all-exports-scope",
1300+
"children",
1301+
],
1302+
expected_directory=EXPECTED_MAIN_PATH / "jsonschema" / "all_exports_hyphenated_directory",
1303+
)
1304+
1305+
12911306
def test_check_respects_pyproject_toml_settings(tmp_path: Path) -> None:
12921307
"""Test --check uses pyproject.toml formatter settings from output path.
12931308

0 commit comments

Comments
 (0)