79214541

Date: 2024-11-22 10:26:45
Score: 4
Natty:
Report link

I have done a modification to modify key based on following requirements:

Excluding some keys Using key extendedValue if data is over of 254 chars.

I have written following code on serialiser:

@model_serializer(mode="wrap")
def _serialize(self, handler):
    d = handler(self)
    d_attributes = list()
    for k, v in self.__dict__.items():
        if k not in excluded:
            data = { "name": k.upper() }
            if len(v) < limit:
                data.update({ "value": v })
            else:
                data.update({ "extendedValue": v })

            d_attributes.append(data)
            
    d["attributes"] = d_attributes
    return d

But I receive following error:

Error serializing to JSON: PydanticSerializationError: Error calling function `_serialize`: ValueError: Circular reference detected (id repeated)

Instead if I rollback with following code:

@model_serializer(mode="wrap")
def _serialize(self, handler):
    d = handler(self)
    d_attributes = list()
    for k, v in self.__dict__.items():
        if k not in excluded:
            data = { "name": k.upper(), "value": v  }

            d_attributes.append(data)
            
    d["attributes"] = d_attributes
    return d

It works. Any Idea?

Reasons:
  • Blacklisted phrase (1): Any Idea?
  • RegEx Blacklisted phrase (1): I receive following error
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Aldo Pellini