Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python-pydantic-v1: Return the primitive type in to_dict for anyOf models #19488

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def to_dict(self) -> dict:
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def to_dict(self) -> dict:
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def to_dict(self) -> dict:
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def to_dict(self) -> dict:
if callable(to_json):
return self.actual_instance.to_dict()
else:
return json.dumps(self.actual_instance)
# primitive type
return self.actual_instance

def to_str(self) -> str:
"""Returns the string representation of the actual instance"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_anyOf_array_of_integers(self):
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))

# test from_josn
# test from_json
json_str = '[12,34,56]'
p = petstore_api.AnyOfColor.from_json(json_str)
self.assertEqual(p.actual_instance, [12, 34,56])
Expand All @@ -187,6 +187,28 @@ def test_anyOf_array_of_integers(self):
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))

# test from_json, schema 3
json_str = '"#123456"'
p = petstore_api.AnyOfColor.from_json(json_str)
self.assertIsInstance(p.actual_instance, str)
self.assertEqual(p.actual_instance, '#123456')

# test to_json, schema 3
p = petstore_api.AnyOfColor(actual_instance='#123456')
self.assertEqual(p.to_json(), '"#123456"')

# test from_dict, schema 3
obj = '#123456'
p = petstore_api.AnyOfColor.from_dict(obj)
self.assertIsInstance(p.actual_instance, str)
self.assertEqual(p.actual_instance, '#123456')

# test to_dict, schema 3
p = petstore_api.AnyOfColor(actual_instance='#123456')
self.assertEqual(p.to_dict(), '#123456')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change, this would render as '"#123456"'

which is incorrect for to_dict

p = petstore_api.AnyOfColor.from_dict(p.to_dict())
self.assertEqual(p.actual_instance, '#123456')

def test_oneOf(self):
# test new Pig
bp = petstore_api.BasquePig.from_dict({"className": "BasquePig", "color": "red"})
Expand Down Expand Up @@ -300,6 +322,9 @@ def test_anyOf(self):
" DanishPig expected dict not int (type=type_error)")
self.assertEqual(str(e), error_message)

# test to_dict
self.assertEqual(p.to_dict(), {'className': 'BasquePig', 'color': 'red'})

# test to_json
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')

Expand Down
Loading