mirror of
https://github.com/koxudaxi/datamodel-code-generator.git
synced 2024-03-18 14:54:37 +03:00
Fix object instance comparison. Add test. (#1849)
* Fix object instance comparison. Add test. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: James Bezuk <james.bezuk@cognex.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -87,7 +87,9 @@ class DataModelField(DataModelFieldBase):
|
||||
return result
|
||||
|
||||
def self_reference(self) -> bool:
|
||||
return isinstance(self.parent, BaseModel) and self.parent.reference.path in {
|
||||
return isinstance(
|
||||
self.parent, BaseModelBase
|
||||
) and self.parent.reference.path in {
|
||||
d.reference.path for d in self.data_type.all_data_types if d.reference
|
||||
}
|
||||
|
||||
@@ -111,12 +113,12 @@ class DataModelField(DataModelFieldBase):
|
||||
data_type = data_type.data_types[0]
|
||||
if (
|
||||
data_type.reference
|
||||
and isinstance(data_type.reference.source, BaseModel)
|
||||
and isinstance(data_type.reference.source, BaseModelBase)
|
||||
and isinstance(self.default, list)
|
||||
): # pragma: no cover
|
||||
return f'lambda :[{data_type.alias or data_type.reference.source.class_name}.{self._PARSE_METHOD}(v) for v in {repr(self.default)}]'
|
||||
elif data_type.reference and isinstance(
|
||||
data_type.reference.source, BaseModel
|
||||
data_type.reference.source, BaseModelBase
|
||||
): # pragma: no cover
|
||||
return f'lambda :{data_type.alias or data_type.reference.source.class_name}.{self._PARSE_METHOD}({repr(self.default)})'
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# generated by datamodel-codegen:
|
||||
# filename: default_object.yaml
|
||||
# timestamp: 2019-07-26T00:00:00+00:00
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field, RootModel
|
||||
|
||||
from . import Foo as Foo_1
|
||||
from . import Nested
|
||||
|
||||
|
||||
class Foo(RootModel[str]):
|
||||
root: str
|
||||
|
||||
|
||||
class Bar(BaseModel):
|
||||
original_foo: Optional[Foo_1] = Field(
|
||||
default_factory=lambda: Foo_1.model_validate({'text': 'abc', 'number': 123})
|
||||
)
|
||||
nested_foo: Optional[List[Nested.Foo]] = Field(
|
||||
default_factory=lambda: [Nested.Foo.model_validate(v) for v in ['abc', 'efg']]
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
# generated by datamodel-codegen:
|
||||
# filename: default_object.yaml
|
||||
# timestamp: 2019-07-26T00:00:00+00:00
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field, RootModel
|
||||
|
||||
from . import Foo as Foo_1
|
||||
|
||||
|
||||
class Foo(RootModel[str]):
|
||||
root: str
|
||||
|
||||
|
||||
class Bar(BaseModel):
|
||||
foo: Optional[Foo_1] = Field(
|
||||
default_factory=lambda: Foo_1.model_validate({'text': 'abc', 'number': 123})
|
||||
)
|
||||
baz: Optional[List[Foo_1]] = Field(
|
||||
default_factory=lambda: [
|
||||
Foo_1.model_validate(v)
|
||||
for v in [{'text': 'abc', 'number': 123}, {'text': 'efg', 'number': 456}]
|
||||
]
|
||||
)
|
||||
nested_foo: Optional[Foo] = Field(
|
||||
default_factory=lambda: Foo.model_validate('default foo')
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
# generated by datamodel-codegen:
|
||||
# filename: default_object.yaml
|
||||
# timestamp: 2019-07-26T00:00:00+00:00
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Foo(BaseModel):
|
||||
text: Optional[str] = '987'
|
||||
number: Optional[float] = None
|
||||
|
||||
|
||||
class Bar(BaseModel):
|
||||
foo: Optional[Foo] = Field(
|
||||
default_factory=lambda: Foo.model_validate({'text': 'abc', 'number': 123})
|
||||
)
|
||||
baz: Optional[List[Foo]] = Field(
|
||||
default_factory=lambda: [
|
||||
Foo.model_validate(v)
|
||||
for v in [{'text': 'abc', 'number': 123}, {'text': 'efg', 'number': 456}]
|
||||
]
|
||||
)
|
||||
@@ -5171,6 +5171,10 @@ def test_main_jsonschema_pattern_properties_by_reference():
|
||||
'pydantic.BaseModel',
|
||||
'main_openapi_default_object',
|
||||
),
|
||||
(
|
||||
'pydantic_v2.BaseModel',
|
||||
'main_openapi_pydantic_v2_default_object',
|
||||
),
|
||||
(
|
||||
'msgspec.Struct',
|
||||
'main_openapi_msgspec_default_object',
|
||||
|
||||
Reference in New Issue
Block a user