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

Fix invalid contributors causing crash #1771

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dandiapi/api/models/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def citation(cls, metadata):
version = metadata['version']
# If we can't find any contributors, use this citation format
citation = f'{name} ({year}). (Version {version}) [Data set]. DANDI archive. {url}'
if 'contributor' in metadata and metadata['contributor']:
if 'contributor' in metadata and isinstance(metadata['contributor'], list):
cl = '; '.join(
[
val['name']
Expand Down
16 changes: 16 additions & 0 deletions dandiapi/api/tests/test_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,19 @@ def test_dandiset_rest_search_identifier(api_client, draft_version):

assert results[0]['draft_version']['version'] == draft_version.version
assert results[0]['draft_version']['name'] == draft_version.name


@pytest.mark.django_db()
@pytest.mark.parametrize(
'contributors',
[None, 'string', 1, [], {}],
)
def test_dandiset_contact_person_malformed_contributors(api_client, draft_version, contributors):
draft_version.metadata['contributor'] = contributors
draft_version.save()

results = api_client.get(
f'/api/dandisets/{draft_version.dandiset.identifier}/',
)

assert results.data['draft_version']['dandiset']['contact_person'] == ''
4 changes: 2 additions & 2 deletions dandiapi/api/views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from dandiapi.search.models import AssetSearch


def extract_contact_person(version):
def extract_contact_person(version: Version) -> str:
"""Extract a version's contact person from its metadata."""
# TODO: move this logic into dandischema since it is schema-dependant
contributors = version.metadata.get('contributor')
if contributors is not None:
if contributors is not None and isinstance(contributors, list):
for contributor in contributors:
name = contributor.get('name')
role_names = contributor.get('roleName')
Expand Down