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.

Copy link
Member

@waxlamp waxlamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this is a small-seeming change that fixes immediate problems, but my alarm bells are ringing at the idea of blindly copying embargo-level metadata from an associated version to the asset itself.

@jjnesbitt, can you assess the longer-term danger here, in case we need to merge this fix in the short term? If we do that, we need to create an issue to look at the deeper problem here and fix at that level.

is_embargoed = self.is_embargoed
if is_embargoed:
# Embargo info is only set on the first version's metadata
first_version = self.versions.first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this always be correct? We may one day have a model where some versions are embargoed while others are not (although, I admit, this seems unlikely). In a case like that, this would become wrong again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the time being the assumption that embargo is a blocker for publishing (that is, embargoed dandisets can only have a draft version) is okay. If that changes, many more places than this will need to be changed anyway.

# Some tests create assets without an associated version
if first_version:
embargoed_until = first_version.metadata['access'][0]['embargoedUntil']
access['embargoedUntil'] = embargoed_until
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out in the associated issue

That said, I am not sure why we have embargoedUntil at the asset level at all since this is only a dandiset parameter.

A better fix would be to take a deeper look at "denormalizing" embargo metadata onto assets as well as Dandisets.

* Fix version retrieval (a simple .first() won't return `draft` if other versions
  are present)
* Factor access logic out into separate method
* Raise exception if dandiset is OPEN with an embargoed asset
* Don't assume `embargoedUntil` is present on version metadata
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

4 participants