inveniosoftware / invenio-rdm-records

DataCite-based data model for InvenioRDM flavour.
https://invenio-rdm-records.readthedocs.io
MIT License
15 stars 81 forks source link

serializers: access values only if set #1731

Open rekt-hard opened 2 months ago

rekt-hard commented 2 months ago

:heart: Thank you for your contribution!

Description

Perform resource type vocabulary lookup only if resource type is set. Parse dates only if they are set

Checklist

Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:

Third-party code

If you've added third-party code (copy/pasted or new dependencies), please reach out to an architect.

Reminder

By using GitHub, you have already agreed to the GitHub’s Terms of Service including that:

  1. You license your contribution under the same terms as the current repository’s license.
  2. You agree that you have the right to license your contribution under the current repository’s license.
utnapischtim commented 2 months ago

is this a bugfix or a feature?

rekt-hard commented 2 months ago

Bugfix. It is only about making sure the values exist before performing a lookup or trying to parse it.

For example, performing the following steps will lead to an error:

I will just create an issue out of this

utnapischtim commented 2 months ago

would it not be better to use a try except block?

try:
    resource_type_id = obj["metadata"]["resource_type"]["id"]
except KeyError:
    return missing

or is the "resource_type.id" always there? the same could be applied to the other changes as well

rekt-hard commented 2 months ago

Right - good catch. Not sure about it being there. In other places something like this is used: resource_type_id = py_.get(obj, "metadata.resource_type.id") (which will return None if any intermediate attribute is not available)

Would go for that.

github-actions[bot] commented 1 week ago

This PR was automatically marked as stale.