-
Notifications
You must be signed in to change notification settings - Fork 427
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
[bugfix] Allow calling Pickler.derive on non-mirrored types #3289
Conversation
summonFrom { | ||
case schema: Schema[T] => fromExistingSchemaAndRw[T](schema) | ||
case _ => buildNewPickler[T]() | ||
case _ => | ||
summonFrom { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we flatten the summonFrom
s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm looks like we can indeed. The code feels strange, but works, thanks :)
case r: Reader[T] => r | ||
case _ => | ||
errorForType[T]( | ||
"Use Pickler.derive[%s] instead of nonMirrorPickler. This method has to be in scope to resolve predefined picklers." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is nonMirrorPickler
something people should ever use direclty, or our impl detail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it has to be in public scope. Without it Pickler has issues with deriving ReadWriter
for String
and some other types. This probably may be worked around, but for now I'd keep it public.
// It turns out that summoning a Pickler can sometimes fall into this branch, even if we explicitly state that we want a NotGiven in the method signature | ||
case m: Mirror.Of[T] => | ||
errorForType[T]( | ||
"Found unexpected Mirror. Failed to summon a Pickler[%s]. Try using Pickler.derived or importing sttp.tapir.json.pickler.generic.auto.*" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe suggest reporting a bug in tapir, if this happens?
Fixes #3287
For custom type
T
which are not products or coproducts, one should be able to derive a Pickler by providing implicitReader[T]
,Writer[T]
(orReadWriter[T]
) andSchema[T]
, then just callingPickler.derive[T]
.Currenlty
derive
requires aMirror.Of[T]
in its signature, which needs to be removed and moved deeper into asummonFrom
resolution, to defer determining how to construct the Pickler.Additionally, the
nonMirrorPickler[T]
method will have a better error message, indicating that user should usePickler.derived[T]
We can't hide
nonMirrorPickler
entirely, because it has to be in scope for correct resolution of predefined picklers (like String, List, Option, etc).