Repository: celery/kombu

Issue:
bug: serializer does not accept all uuids
The new serializer introduced in #1575, fails when the payload contains uuids whose version are not within 1 and 5. In our case it starts failing because we have some UUIDs version 7.

This is because in line https://github.com/el-chogo/kombu/blob/6b89388870271e23c31f6c2845f248c832fb7c6f/kombu/utils/json.py#LL48C19-L48C21 you are explicitly setting the version.

However, [UUID's constructor](https://github.com/python/cpython/blob/e830289c52cecd99e5e2291972d648e9b3452a51/Lib/uuid.py#L213-L221), only uses the provided version, to ensure it's between 1 and 5. It does not even store that value. 
However, given the widespread adoption of new [uuid formats](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04), having this restriction is not really required.

Below is the full stack trace:

```
ValueError: illegal version number
  File "kombu/serialization.py", line 41, in _reraise_errors
    yield
  File "kombu/serialization.py", line 265, in loads
    return decode(data)
  File "kombu/utils/json.py", line 87, in loads
    return _loads(s, object_hook=object_hook)
  File "__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "kombu/utils/json.py", line 65, in object_hook
    return decoder(o["__value__"])
  File "kombu/utils/json.py", line 127, in <lambda>
    lambda o: uuid.UUID(**o),
  File "uuid.py", line 214, in __init__
    raise ValueError('illegal version number')
DecodeError: illegal version number
  File "celery/worker/consumer/consumer.py", line 596, in on_task_received
    strategy(
  File "celery/worker/strategy.py", line 135, in task_message_handler
    if body is None and 'args' not in message.payload:
  File "kombu/message.py", line 216, in payload
    return self._decoded_cache if self._decoded_cache else self.decode()
  File "kombu/message.py", line 201, in decode
    self._decoded_cache = self._decode()
  File "kombu/message.py", line 205, in _decode
    return loads(self.body, self.content_type,
  File "kombu/serialization.py", line 264, in loads
    with _reraise_errors(DecodeError):
  File "contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "kombu/serialization.py", line 45, in _reraise_errors
    reraise(wrapper, wrapper(exc), sys.exc_info()[2])
  File "kombu/exceptions.py", line 34, in reraise
    raise value.with_traceback(tb)
  File "kombu/serialization.py", line 41, in _reraise_errors
    yield
  File "kombu/serialization.py", line 265, in loads
    return decode(data)
  File "kombu/utils/json.py", line 87, in loads
    return _loads(s, object_hook=object_hook)
  File "__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "kombu/utils/json.py", line 65, in object_hook
    return decoder(o["__value__"])
  File "kombu/utils/json.py", line 127, in <lambda>
    lambda o: uuid.UUID(**o),
  File "uuid.py", line 214, in __init__
    raise ValueError('illegal version number')
```

This fails with version 5.3.0, and worked with versions 5.3.0a1 and 5.2.4.

cc: @el-chogo @auvipy 


Discussion:

Patch:
diff --git a/kombu/utils/json.py b/kombu/utils/json.py
index ec6269e2..ce1319ac 100644
--- a/kombu/utils/json.py
+++ b/kombu/utils/json.py
@@ -123,6 +123,6 @@ register_type(Decimal, "decimal", str, Decimal)
 register_type(
     uuid.UUID,
     "uuid",
-    lambda o: {"hex": o.hex, "version": o.version},
+    lambda o: {"hex": o.hex},
     lambda o: uuid.UUID(**o),
 )


Test patch:
diff --git a/t/unit/utils/test_json.py b/t/unit/utils/test_json.py
index a026c883..2da90e1a 100644
--- a/t/unit/utils/test_json.py
+++ b/t/unit/utils/test_json.py
@@ -20,7 +20,6 @@ else:
 
 
 class Custom:
-
     def __init__(self, data):
         self.data = data
 
@@ -71,6 +70,11 @@ class test_JSONEncoder:
             lambda: uuid.uuid3(uuid.NAMESPACE_URL, "https://example.org"),
             uuid.uuid4,
             lambda: uuid.uuid5(uuid.NAMESPACE_URL, "https://example.org"),
+            # The uuids below correspond to v6, v7 and v8 respectively and were
+            # generated using the package uuid6.
+            lambda: uuid.UUID("1ee0b1e6-dd55-63d2-867f-88cb9205458f"),
+            lambda: uuid.UUID("0188bcbb-8475-7605-a094-fe41c58df798"),
+            lambda: uuid.UUID("0188bcbb-8cb2-8bf7-b3b5-fd1faa0431bd"),
         ]
         for constructor in constructors:
             id = constructor()