Repository: safe-global/safe-config-service

Issue:
`/api/v1/chains` returns unexpected nativeCurrency.logoUri path
**Describe the bug**
I deployed a project locally using the code from `https://github.com/safe-global/safe-infrastructure`. However, after adding a configuration for a chain in cfg, the `/v1/chains` endpoint of cgw started returning a 500 error. After enabling debug logs, I found that cgw returned the following error:
```json
{
    "build_number": "edc276d",
    "level": "error",
    "message": {
        "chain_id": null,
        "client_ip": "172.70.223.68",
        "detail": "[\n  {\n    \"validation\": \"url\",\n    \"code\": \"invalid_string\",\n    \"message\": \"Invalid url\",\n    \"path\": [\n      \"nativeCurrency\",\n      \"logoUri\"\n    ]\n  }\n]",
        "method": "GET",
        "path": "/v1/chains",
        "response_time_ms": 107.19920100085437,
        "route": "/v1/chains",
        "safe_app_user_agent": null,
        "status_code": 500
    },
    "request_id": "1e37d27b-02b6-468a-a290-b6e06f7bc911",
    "timestamp": "2024-03-29T07:54:29.220Z",
    "version": "v1.31.0"
}
````
After investigating further, I found that the `nativeCurrency.logoUri` in the response data from cfg is not an absolute path:

```json
{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
          ...
            "nativeCurrency": {
                "name": "ETH",
                "symbol": "ETH",
                "decimals": 18,
                "logoUri": "/media/chains/100/currency_logo_3fdtfcf.png"
            },
          ...
    ]
}
```

**To Reproduce**
Steps to reproduce the behavior:

 - Clone the code from `https://github.com/safe-global/safe-infrastructure`
 - Modify the MEDIA_URL in cfg.env to `/media/`
**Expected behavior**
The `nativeCurrency.logoUri` in the response data from cfg should be `https://config.xxx.xyz/media/chains/100/currency_logo_3fdtfcf.png` instead of `/media/chains/100/currency_logo_3fdtfcf.png`.

**Environment (please complete the following information):**
 - Version of safe-config-service: latest(possibly [v2.75.0])
 
**Additional context**
I made the following edits to the `src/chains/serializers.py` file
from
```python
    @staticmethod
    @swagger_serializer_method(serializer_or_field=CurrencySerializer)  # type: ignore[misc]
    def get_native_currency(obj: Chain) -> ReturnDict:
        return CurrencySerializer(obj).data
```
to
```python
    @swagger_serializer_method(serializer_or_field=CurrencySerializer)  # type: ignore[misc]
    def get_native_currency(self, obj: Chain) -> ReturnDict:
        request = self.context.get('request')
        logo_uri = obj.currency_logo_uri
        dict = CurrencySerializer(obj).data
        dict.update({"logo_uri":request.build_absolute_uri(logo_uri)})
        return dict
```
After applying the temporary workaround, cfg is able to return the expected data. However, since I'm not familiar with Python and Django, this was just a temporary solution on my part. I hope that you can find a better way to fix this issue.


Discussion:
Hi @gugugu47 I ran into a similar issue. If you set `MEDIA_URL` env, it seems to resolve the issue
Hey @gugugu47 and @abhinavmsra. Thanks for reporting this issue.

It seems that the issue is related to a missing context on the Native Currency serialiser. For context: https://www.django-rest-framework.org/api-guide/serializers/#including-extra-context

> There are some cases where you need to provide extra context to the serializer in addition to the object being serialized. One common case is if you're using a serializer that includes hyperlinked relations, which requires the serializer to have access to the current request so that it can properly generate fully qualified URLs.

This context was lost between the Chain serialiser and the Native Currency one. I will open a Pull Request to fix it.

Resolving patch:
diff --git a/src/chains/serializers.py b/src/chains/serializers.py
--- a/src/chains/serializers.py
+++ b/src/chains/serializers.py
@@ -171,10 +171,11 @@ class Meta:
             "features",
         ]
 
-    @staticmethod
     @swagger_serializer_method(serializer_or_field=CurrencySerializer)  # type: ignore[misc]
-    def get_native_currency(obj: Chain) -> ReturnDict:
-        return CurrencySerializer(obj).data
+    def get_native_currency(self, obj: Chain) -> ReturnDict:
+        return CurrencySerializer(
+            obj, context={"request": self.context["request"]}
+        ).data
 
     @staticmethod
     @swagger_serializer_method(serializer_or_field=ThemeSerializer)  # type: ignore[misc]


Test patch:
diff --git a/src/chains/tests/test_views.py b/src/chains/tests/test_views.py
--- a/src/chains/tests/test_views.py
+++ b/src/chains/tests/test_views.py
@@ -64,7 +64,7 @@ def test_json_payload_format(self) -> None:
                         "name": chain.currency_name,
                         "symbol": chain.currency_symbol,
                         "decimals": chain.currency_decimals,
-                        "logoUri": chain.currency_logo_uri.url,
+                        "logoUri": f"http://testserver{chain.currency_logo_uri.url}",
                     },
                     "transactionService": chain.transaction_service_uri,
                     "vpcTransactionService": chain.vpc_transaction_service_uri,
@@ -183,7 +183,7 @@ def test_json_payload_format(self) -> None:
                 "name": chain.currency_name,
                 "symbol": chain.currency_symbol,
                 "decimals": chain.currency_decimals,
-                "logoUri": chain.currency_logo_uri.url,
+                "logoUri": f"http://testserver{chain.currency_logo_uri.url}",
             },
             "transactionService": chain.transaction_service_uri,
             "vpcTransactionService": chain.vpc_transaction_service_uri,