Skip to content

Conversation

@h-mayorquin
Copy link

Fixes #2697

@yarikoptic
Copy link
Member

@h-mayorquin I think it broke tests -- do they pass for you locally?

copilot's take on it

The failing job 60823788006 encountered a KeyError: 'embargoedUntil' when serializing the metadata field in AssetDetailSerializer. This typically means the code attempted to access metadata['access'][0]['embargoedUntil'] but the key 'embargoedUntil' was not present.

Solution:
You need to ensure that the key exists before accessing it, or handle cases where it may be missing.

Code Fix

Example for serializer or any place where the field is accessed:

embargoed_until = None
access = first_version.metadata.get('access', [])
if access and isinstance(access[0], dict):
    embargoed_until = access[0].get('embargoedUntil')
# Use embargoed_until (could be None if not present)

If you need the value to always be present, add a default:

embargoed_until = access[0].get('embargoedUntil', 'default-value')

Alternatively, handle the missing field in your serializer:

  • Use Django REST Framework’s SerializerMethodField or override to_representation to catch and handle KeyError.

Additional suggestion

  • Check the tests which failed (such as test_asset_create_embargo, test_asset_rest_update_embargo, etc.) and ensure your test setup/fixtures include the expected field in the metadata, or update the tests to handle assets where 'embargoedUntil' may not exist.

Summary:
Guard access to 'embargoedUntil' with .get() or explicit checks, and update serializer code accordingly. This should resolve the KeyError and prevent 500 errors on serialization.

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

Successfully merging this pull request may close these issues.

Asset metadata missing embargoedUntil field causing validation errors

2 participants