Skip to content

Commit

Permalink
fix(ModuleImports): This PR fixes the module import resolver for modu…
Browse files Browse the repository at this point in the history
…le of type `BaseClassDataType` (#1862)

* When resolving imports, in the instance where the model is of data type BaseClassDataType, it needs to be ensured that the from_ value is joined appropriately.

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

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

* Modified nested directory structure of unit test to test module import resolver works as intended.

* Fix broken tests.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Koudai Aono <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 76fc354 commit 95260f5
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 28 deletions.
7 changes: 6 additions & 1 deletion datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,12 @@ def __change_from_import(
continue

if isinstance(data_type, BaseClassDataType):
from_ = ''.join(relative(model.module_name, data_type.full_name))
left, right = relative(model.module_name, data_type.full_name)
from_ = (
''.join([left, right])
if left.endswith('.')
else '.'.join([left, right])
)
import_ = data_type.reference.short_name
full_path = from_, import_
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from typing import Optional

from ... import person
from .. import food, pet
from .. import food
from ..drink import coffee
from ..pet import Pet
from ..relative.animal.pet import pet
from ..relative.animal.pet.pet import Pet


class Robot(Pet):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: external_files_in_directory
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generated by datamodel-codegen:
# filename: definitions/fur.json
# filename: definitions/relative/animal/fur.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: external_files_in_directory
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generated by datamodel-codegen:
# filename: definitions/pet.json
# filename: definitions/relative/animal/pet/pet.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Expand All @@ -8,7 +8,7 @@

from pydantic import BaseModel

from . import fur
from .. import fur


class Pet(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion tests/data/expected/main/main_nested_directory/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

from pydantic import BaseModel, Field, conint

from .definitions import food, friends, pet
from .definitions import food, friends
from .definitions.drink import coffee, tea
from .definitions.machine import robot
from .definitions.relative.animal.pet import pet


class Person(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from __future__ import annotations

from ... import person
from .. import food, pet
from .. import food
from ..drink import coffee
from ..pet import Pet
from ..relative.animal.pet import pet
from ..relative.animal.pet.pet import Pet


class Robot(Pet):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: external_files_in_directory
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generated by datamodel-codegen:
# filename: definitions/fur.json
# filename: definitions/relative/animal/fur.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by datamodel-codegen:
# filename: external_files_in_directory
# timestamp: 2019-07-26T00:00:00+00:00
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# generated by datamodel-codegen:
# filename: definitions/pet.json
# filename: definitions/relative/animal/pet/pet.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel

from . import fur
from .. import fur


class Pet(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion tests/data/expected/main/main_use_union_operator/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

from pydantic import BaseModel, Field, conint

from .definitions import food, friends, pet
from .definitions import food, friends
from .definitions.drink import coffee, tea
from .definitions.machine import robot
from .definitions.relative.animal.pet import pet


class Person(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Robot",
"type": "object",
"allOf": [{ "$ref": "../pet.json"}],
"allOf": [{ "$ref": "../relative/animal/pet/pet.json"}],
"properties": {
"friends": {
"$ref": "../../person.json"
Expand All @@ -15,7 +15,7 @@
"$ref": "../food.json#/definitions/noodle"
},
"pet": {
"$ref": "../pet.json"
"$ref": "../relative/animal/pet/pet.json"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$id": "https://example.com/external_files_in_directory/definitions/fur.json",
"$id": "https://example.com/external_files_in_directory/definitions/relative/animal/fur.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Fur",
"type": "string",
"enum": [
"Short hair",
"Long hair"
],
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$id": "https://example.com/external_files_in_directory/definitions/pet.json",
"$id": "https://example.com/external_files_in_directory/definitions/relative/animal/pet/pet.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
Expand All @@ -11,7 +11,7 @@
"type": "integer"
},
"fur": {
"$ref": "fur.json#"
"$ref": "../fur.json"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "array",
"items": [
{
"$ref": "definitions/pet.json#"
"$ref": "definitions/relative/animal/pet/pet.json#"
}
]
},
Expand Down
16 changes: 8 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,8 +3089,8 @@ def get_mock_response(path: str) -> mocker.Mock:
'httpx.get',
side_effect=[
get_mock_response('person.json'),
get_mock_response('definitions/pet.json'),
get_mock_response('definitions/fur.json'),
get_mock_response('definitions/relative/animal/pet/pet.json'),
get_mock_response('definitions/relative/animal/fur.json'),
get_mock_response('definitions/friends.json'),
get_mock_response('definitions/food.json'),
get_mock_response('definitions/machine/robot.json'),
Expand Down Expand Up @@ -3126,13 +3126,13 @@ def get_mock_response(path: str) -> mocker.Mock:
follow_redirects=True,
),
call(
'https://example.com/external_files_in_directory/definitions/pet.json',
'https://example.com/external_files_in_directory/definitions/relative/animal/pet/pet.json',
headers=None,
verify=True,
follow_redirects=True,
),
call(
'https://example.com/external_files_in_directory/definitions/fur.json',
'https://example.com/external_files_in_directory/definitions/relative/animal/fur.json',
headers=None,
verify=True,
follow_redirects=True,
Expand Down Expand Up @@ -3201,8 +3201,8 @@ def get_mock_response(path: str) -> mocker.Mock:
'httpx.get',
side_effect=[
get_mock_response('person.json'),
get_mock_response('definitions/pet.json'),
get_mock_response('definitions/fur.json'),
get_mock_response('definitions/relative/animal/pet/pet.json'),
get_mock_response('definitions/relative/animal/fur.json'),
get_mock_response('definitions/friends.json'),
get_mock_response('definitions/food.json'),
get_mock_response('definitions/machine/robot.json'),
Expand Down Expand Up @@ -3242,13 +3242,13 @@ def get_mock_response(path: str) -> mocker.Mock:
follow_redirects=True,
),
call(
'https://example.com/external_files_in_directory/definitions/pet.json',
'https://example.com/external_files_in_directory/definitions/relative/animal/pet/pet.json',
headers=headers_requests,
verify=True if not http_ignore_tls else False,
follow_redirects=True,
),
call(
'https://example.com/external_files_in_directory/definitions/fur.json',
'https://example.com/external_files_in_directory/definitions/relative/animal/fur.json',
headers=headers_requests,
verify=True if not http_ignore_tls else False,
follow_redirects=True,
Expand Down

0 comments on commit 95260f5

Please sign in to comment.