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

Serializer.run_validators does not add defaults from read_only fields to data. #8974

Open
pyrocat opened this issue May 4, 2023 · 3 comments

Comments

@pyrocat
Copy link

pyrocat commented May 4, 2023

It seems there's a typo in Serializer.run_validators method which is supposed to update initial data with default values from read-only fields. Instead, the value remains unchanged.

 def run_validators(self, value):
      """
      Add read_only fields with defaults to value before running validators.
      """
      if isinstance(value, dict):
          to_validate = self._read_only_defaults()
          to_validate.update(value)
      else:
          to_validate = value
      super().run_validators(to_validate)

Looks like it should be value.update(to_validate) instead of to_validate.update(value)

@auvipy
Copy link
Member

auvipy commented May 6, 2023

can you send a PR with unit tests?

@yabea
Copy link

yabea commented May 6, 2023

I think this code is correct.

  • If there is a readonly field and the field is not in value, use the default value
  • If there is a readonly field and the field is in value, use the value in value
  • Cannot add the readonly field to value, because of the validated_data corresponding to value

@auvipy
Copy link
Member

auvipy commented May 7, 2023

in that case we can close this issue or move this to discussion

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

3 participants