question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dataclass Extension does not avoid serializer name collisions

See original GitHub issue

Describe the bug This is how we get_name for a Dataclass Serializer self.target.dataclass_definition.dataclass_type.__name__ This will lead to collisions when you have multiple dataclasses with the same name or different serializers that use the same dataclass as the base To Reproduce

class MySerializer(DataclassSerializer):
    class Meta:
        dataclass = MyDataclass
class MyOtherSerializer(DataclassSerializer):
    my_field = serializers.CharField()
    class Meta:
        dataclass = MyDataclass

Issue: There is no collision avoidance when it comes to using dataclass serializers.

Expected behavior I’m not sure how collisions are avoided with normal serializers (I think a postfix of some kind is applied), but I would expect a similar collision avoidance scheme to be used if the extension is unable to provide a unique component name.

Some ideas:

  1. Prefix with some combination of the dataclass module, the serializer module, the serializer name, the serializer ref name
  2. Generate some unique postfix if the name is already present
  3. Allow support for ref_name in Dataclass itself, or dataclass serializer (currently ignored)
  4. Add a get unique name field to the OpenApiDataclassSerializerExtensions and let the extension determine how to get a unique name for the component

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Nov 21, 2022

Sry @ford-carta but this was no simple answer and so I had to set aside extra time.

Issue: There is no collision avoidance when it comes to using dataclass serializers.

There is no collision avoidance for Serializer either. We only have collision avoidance for the enum postprocessing hook.

As a matter of fact this is more complicated to do consistently than meets the eye. That much I learned writing that avoidance. The discovery order may matter and you can’t just fix an added class as we don’t know which one was there first. So for avoidance you must change names of all affected classes that share a collision, which might break e.g. a generated client. For that exact reason we emit warnings/errors if serializer collisions are detected, leaving it to the user to come up with a sensible resolution.

Thus, this problem seems to be introduced by and specific to the drf-dataclasses extension, and it probably doesn’t make sense to introduce a generic solution for this.

I think so too.

though that’ll be a backward compatibility break as component names might change.

Yes, that would be a problem and we need to make a very strong argument for that.

I got to admit that I am not a user of this library and so I have not thoroughly considered all cases in depth or rather in what different ways this library can be used.

Aside from a breaking change, I think it would make sense to consider Meta.ref_name as a name override as we do this also for regular serializers. I see no harm in supporting this, and it would be more consistent imho.

As for changing the default naming scheme, I will look into djangorestframework-dataclasses more thoroughly and get a more informed opinion. I don’t want to change it haphazardly.

And now that I think about it, nested dataclasses is probably another shortcoming of the current implementation.

0reactions
oxancommented, Nov 22, 2022

Yes, exactly that was the reason. I think using the serializer name for explicit usages and dataclass name for anonymous usages feels inconsistent and potentially buggy. Thus, imho the current implementation makes the most sense.

That’s reasonable.

Let me know what you think @oxan @ford-carta

Both look good to me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using DRF spectacular · Issue #705 - GitHub
For obvious reasons, collisions would be bad if they are referenced all over the schema. Each serializer needs a unique name (or unique...
Read more >
dataclasses — Data Classes — Python 3.11.1 documentation
The generated repr string will have the class name and the name and repr of each field, in the order they are defined...
Read more >
Everything you need to know about dataclasses - rmcomplexity
@dataclass does not create a new class, it returns the same defined class. ... It's recommended to use a specific key to avoid...
Read more >
JsonClassDiscriminator doesn't change json class discriminator
I tried to use @JsonClassDiscriminator but Kotlinx still uses type member as discriminator which have name clash with member in class. I changed ......
Read more >
Dataclass Wizard — Dataclass Wizard 0.22.2 documentation
This library provides a set of simple, yet elegant wizarding tools for interacting with the Python dataclasses module. The primary use is as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found