Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose contextual descriptor's fallback descriptor #2611

Open
rnett opened this issue Mar 23, 2024 · 3 comments
Open

Expose contextual descriptor's fallback descriptor #2611

rnett opened this issue Mar 23, 2024 · 3 comments
Labels

Comments

@rnett
Copy link

rnett commented Mar 23, 2024

What is your use-case and why do you need this feature?

If a type is annotated with @Contextual, but there is no serializer in context, it will use the type's own serializer (i.e. plugin-generated) if it has one. This is contained in the ContextualSerializer.fallbackSerializer property, which is private. I'm trying to do some schema introspection (based on descriptors) and this is making it impossible to have my introspected schema match the actual behavior, since I can't identify the fallback serializer used (I'm doing this in the context of a serializers module, so resolving the serializer itself is not an issue).

Describe the solution you'd like

Make SerializersModule.getContextualDescriptor return the fallback serializer's descriptor if the fallback serializer would be used (or add an overload that does this).

It's simple enough to implement, if you approve the idea I'm happy to make a PR.

@rnett rnett added the feature label Mar 23, 2024
@sandwwraith
Copy link
Member

Yeah, I like the idea, it is intended for descriptors to match the behavior of serializers. However, implementing it may not be straightforward, since by the time we have ContextualSerializer.descriptor, we already have lost reference to fallbackSerializer, since it is stored only inside ContextualSerializer itself. Therefore, fallbackSerializer.descriptor has to be stored additionally somewhere. I'm not sure if it is better to store it in ContextDescriptor (because it is also being used by PolymorphicSerializer) or to store it directly in ContextualSerializer.descriptor (and making it a separate internal implementation of SerialDescriptor interface).

@pdvrieze
Copy link
Contributor

@sandwwraith @rnett I would want this for XML too.

Btw. there is a workaround way to "get" the serializer from the contextual serializer by calling deserialize with a special decoder that has no module and will record the serializer provided - it can then throw an exception instead of actually deserializing (from nothing). It would be good to get this information in a better way (especially when it would need nested "deserialization").

As an aside, there is a similar issue with NullableSerializer, although the serial descriptor is normally detailed enough.

@rnett
Copy link
Author

rnett commented Mar 26, 2024

As an aside, there is a similar issue with NullableSerializer, although the serial descriptor is normally detailed enough.

Yeah I've run into this too, especially when trying to use them as keys for caching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants