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

Update requests irredeemably result in a 422 validation error #51

Open
gaapt opened this issue Oct 29, 2018 · 1 comment
Open

Update requests irredeemably result in a 422 validation error #51

gaapt opened this issue Oct 29, 2018 · 1 comment

Comments

@gaapt
Copy link

gaapt commented Oct 29, 2018

A simple request updating localized fields results in 422 errors.

The SDK forms payloads with wrongly formed fields insofar as it structures the updating property as null.

In the following example:

client = contentful_management.Client('TOKEN')
entry = client.entries('SpaceID').find('EntryID')
entry._fields['en-US']['name'] = 'test' 
entry.save()

The SDK generates a JSON payload with

 fields:    {   [-] 
         ...  
         null:  'test'
        }  

Instead of null, it should instead be generated name: 'test'. Because of this error, the Management API prompts a 422 Validation error.

@dlitvakb
Copy link
Contributor

Hey @gaapt,

I'm not entirely sure why this may be happening, but given the limited context, I think it's because when trying to update a field that's not currently available in the selected locale directly through the internal state, this doesn't get re-serialized properly when sent back to the API.

Therefore, to force the SDK to fetch the entry's content type and ensure field serialization, I'd recommend forcing the entry to the locale you want to edit, and then assign the field directly through the accessor properties, you can repeat this for any amount of locales you want to edit.

client = contentful_management.Client('TOKEN')
entry = client.entries('SpaceID').find('EntryID')

entry.locale = 'en-US'  # or any other locale to edit
entry.name = 'name for english'
entry.other_field = 'some other value'

# and you can do the same for any number of locales
entry.locale = 'es-AR'
entry.name = 'nombre en español' 

# ... etc ...

entry.save()

This will force the SDK to trigger the behaviour for looking up the content type for missing fields which gets triggered any time an assignment to a possible field happens.

Hope this helps,

Cheers

PS: I'll look into how to also introduce this behaviour for when trying to update the fields dictionary directly, but for the moment, the solution provided should work.

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