Skip to content

Commit

Permalink
fix: Preserve class names when generating models from JSON Schema 202… (
Browse files Browse the repository at this point in the history
#2185)

* fix: Preserve class names when generating models from JSON Schema 2020-12

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
koxudaxi and pre-commit-ci[bot] authored Nov 23, 2024
1 parent acf904f commit 368de18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions datamodel_code_generator/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,13 @@ def add_ref(self, ref: str, resolved: bool = False) -> Reference:
split_ref = ref.rsplit('/', 1)
if len(split_ref) == 1:
original_name = Path(
split_ref[0][:-1] if self.is_external_root_ref(path) else split_ref[0]
split_ref[0].rstrip('#')
if self.is_external_root_ref(path)
else split_ref[0]
).stem
else:
original_name = (
Path(split_ref[1][:-1]).stem
Path(split_ref[1].rstrip('#')).stem
if self.is_external_root_ref(path)
else split_ref[1]
)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from datamodel_code_generator.reference import get_relative_path
from datamodel_code_generator.reference import ModelResolver, get_relative_path


@pytest.mark.parametrize(
Expand Down Expand Up @@ -47,3 +47,23 @@ def test_get_relative_path_windows(
assert PureWindowsPath(
get_relative_path(PureWindowsPath(base_path), PureWindowsPath(target_path))
) == PureWindowsPath(expected)


def test_model_resolver_add_ref_with_hash():
model_resolver = ModelResolver()
reference = model_resolver.add_ref(
'https://json-schema.org/draft/2020-12/meta/core#'
)
assert reference.original_name == 'core'


def test_model_resolver_add_ref_without_hash():
model_resolver = ModelResolver()
reference = model_resolver.add_ref('meta/core')
assert reference.original_name == 'core'


def test_model_resolver_add_ref_unevaluated():
model_resolver = ModelResolver()
reference = model_resolver.add_ref('meta/unevaluated')
assert reference.original_name == 'unevaluated'

0 comments on commit 368de18

Please sign in to comment.