django-oscar / django-oscar-api

RESTful JSON API for django-oscar
Other
363 stars 160 forks source link

Fixes handling of null product image attributes #343

Closed crgwbr closed 3 months ago

crgwbr commented 3 months ago

Fixes handling of null product image attributes

When an image product attribute is null, the product API would throw the following exception:

ValueError: The 'value_image' attribute has no file associated with it.

  […]

  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "rest_framework/serializers.py", line 686, in to_representation
    return [
  File "rest_framework/serializers.py", line 687, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "oscarapi/serializers/fields.py", line 227, in to_representation
    return value.value.url
  File "django/db/models/fields/files.py", line 66, in url
    self._require_file()
  File "django/db/models/fields/files.py", line 41, in _require_file
    raise ValueError(

This patch fixes this by checking if the image field has a value before trying to generate a URL.