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

[BUG] If validate assignment is true, the value will not change. #1223

Closed
milksys opened this issue Jul 10, 2024 · 2 comments
Closed

[BUG] If validate assignment is true, the value will not change. #1223

milksys opened this issue Jul 10, 2024 · 2 comments

Comments

@milksys
Copy link

milksys commented Jul 10, 2024

If validate assignment is true, the value will not change.

this is sampel code

Describe the bug

from ninja import Schema

class Model(Schema):
    name: str
    is_bool: bool

    model_config = ConfigDict(validate_assignment=True)


model = Model(name="NewJeans",  is_bool=False)

print(model)  # => name="NewJeans" is_bool=False

model.name= "Ive"
model.is_bool = True

# Not change value
print(model)  # => name="NewJeans" is_bool=False

In the previous 1.1.0 version, that PR threw an error, but now the value does not change (#1024)

Versions (please complete the following information):

  • Python version: 3.11.1
  • Django version: 5.0.7
  • Django-Ninja version: 1.2.1
  • Pydantic version: 2.8.2
@austinpgraham
Copy link
Contributor

I've been debugging this over the past couple of hours, and seems to be related how pydantic updates fields after they've been validated; appears pydantic-core is updating the dict property directly: https://github.com/pydantic/pydantic-core/blob/0e6b377d2bef52e744deff22314269c8751f7b00/src/validators/dataclass.rs#L368 (I'm not super familiar with the pydantic codebase so I may be looking at the wrong place)

However, I did validate this by debugging through the existing test added in the PR you linked @milksys.

tests/test_schema.py 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /Users/me/django-ninja/ninja/schema.py(225)_run_root_validator()
-> return handler(values)
(Pdb) c

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB continue (IO-capturing resumed) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /Users/me/django-ninja/tests/test_schema.py(212)test_django_getter_validates_assignment_and_reassigns_the_value()
-> schema_inst.str_var = "reassigned_value"
(Pdb) c

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB continue (IO-capturing resumed) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /Users/me/django-ninja/ninja/schema.py(225)_run_root_validator()
-> return handler(values)
(Pdb) handler(values)
<DjangoGetter: ValidateAssignmentSchema(str_var='test_value')>
(Pdb) handler(values).__dict__
{'str_var': 'reassigned_value', '__pydantic_extra__': None}
(Pdb) 

Looks like the dict of the DjangoGetter is getting updated directly, rather than the _obj, which is the representation of the actual object being validated. I don't have a good solution yet, will try and tackle it this weekend unless someone else gets to it first.

@austinpgraham
Copy link
Contributor

@milksys PR is out: #1232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants