Skip to content

PATCH incorrectly normalizes default values in sub-documents #1234

Closed

Description

@zhangtemplar

Consider the following schema:

'entity': {
  'name': {
    'type': 'string',
    'required': True },
  'contact': {
    'type': 'dict',
    'required': True,
    'schema': {
      'phone': {
        'type': 'string',
        'required': False,
        'default': '1234567890' },
      'email': {
        'type': 'string',
        'required': False,
        'default': 'abc@efg.com' },
    }
  }
}

Assume I have data like:

{
  "name": "test",
  "contact: {
    "phone": "0987654321",
    "email": "uvw@xyz.com"
  }
}

When I do a patch with data {"contact.phone": "1111111111"} to update the phone only:

Expected behavior

The data will be changed to

{
  "name": "test",
  "contact: {
    "phone": "1111111111",
    "email": "uvw@xyz.com"
  }
}

Actual Behavior

But the data I get is:

{
  "name": "test",
  "contact: {
    "phone": "1111111111",
    "email": "abc@efg.com"
  }
}

Yes, the email is also changed: since email is not provided in PATCH body, its default value is used. Is it the intended behavior for eve?

Environment

  • Python version: 3.5.2
  • Eve version: 0.8.1

Activity

  1. zhangtemplar commented on Mar 12, 2019

    @zhangtemplar
    ContributorAuthor

    To resolve this problem, I think the solution would be changing the eve/validation.py's validate_update method and set normalize to be True.

        def validate_update(self, document, document_id, persisted_document=None):
            """ Validate method to be invoked when performing an update, not an
            insert.
    
            :param document: the document to be validated.
            :param document_id: the unique id of the document.
            :param persisted_document: the persisted document to be updated.
            """
            self.document_id = document_id
            self.persisted_document = persisted_document
            return super(Validator, self).validate(document, update=True, normalize=False)
    
  2. zhangtemplar commented on Mar 12, 2019

    @zhangtemplar
    ContributorAuthor

    @nicolaiarocci D1235 is ready.

  3. nicolaiarocci commented on Mar 28, 2019

    @nicolaiarocci
    Member

    @zhangtemplar see my comment in the PR

  4. added this to the 0.8.2 milestone on Mar 30, 2019
  5. nicolaiarocci commented on Apr 2, 2019

    @nicolaiarocci
    Member

    I reverted 9b2ba0f while we wait for a non-failing patch.

  6. changed the title [-]PATCH not working as expected for nested document[/-] [+]PATCH incorrectly normalizes default values in subdocuments[/+] on Apr 4, 2019
  7. changed the title [-]PATCH incorrectly normalizes default values in subdocuments[/-] [+]PATCH incorrectly normalizes default values in sub-documents[/+] on Apr 4, 2019
  8. added a commit that references this issue on Apr 4, 2019
  9. added 2 commits that reference this issue on Mar 24, 2024
    9b2ba0f
    0c1a2c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions