Closed ghostdogpr closed 1 year ago
It is rather clear to me that your case is described here: https://chimney.readthedocs.io/en/stable/cookbook/#sealed_value-oneof-fields.
ScalaPB can generate oneOf in 3 different ways:
scalapb.GeneratedOneof { type ValueType = Nothing }
for empty value, and we can use this common subtype to handle this Empty
case - it's the case you are quoting and which is handled by the implicitsealed_value
creates an extra Empty
subtype - with nothing that we could use to distinguish it from any other Empty
case object
, which perhaps shouldn't be automatically mapped to a failed transformation - this is the case you have, and you have to handle it manually, sorrysealed_value_optional
- since ScalaPB does not use nulls, all fields are non-nullable, and empty value has to be represented somehow, the former 2 approaches do this by adding extra empty subtype to ADT, and this one uses Option
in every place that this oneOf would be used - this case is handled by PartialTransformer
s and their automatic unwrapping of Option
s (no import required).So no, it is not a bug. Your protobuf definition explicitly opted-in with the only representation of 3 possible, that Chimney cannot automatically handle and it is described in the documentation. (I say explicitly because you have to name oneOf
sealed_value
, and I don't think someone would come up with such name accidentally). You can map things manually or, if you have the ability, change sealed_value
name in your protobufs to sealed_value_optional
. If you look at the ProtobufOneOfSpec you mentioned, you'll see that there are 3 different tests for 3 different representations. We are generating these protobufs from sources, and update dependencies regularly so any (unlikely) change in behavior would be discovered.
Thanks for the detailed explanation! I wrongly assumed the import was relevant to the second case. This is all clear now 🙏
NP, I can add some warning to the docs to make it clearer.
Checklist
TransformerF
s) orunsafeOption
flagsDescribe the bug Apologies if I misunderstood something in the docs.
In the
oneof
part of the docs (https://chimney.readthedocs.io/en/stable/cookbook/#oneof-fields), it says we can handleEmpty
manually like this:Or using a simple import:
While the first one works well, the second doesn't.
It looks like the import provides
partialTransformerFromEmptyOneOfInstance
for aGeneratedOneof
withValueType = Nothing
, but the protobuf generated code doesn't look like that. It's just a simplecase object Empty
.Reproduction An easy way to reproduce is to modify the existing unit test
ProtobufOneOfSpec
.Before:
After:
Expected behavior According to the docs, it should compile.
Actual behavior
Which Chimney version do you use 0.8.0
Which platform do you use
If you checked JVM 17
Additional context Looking at the generated code from scalapb,
Empty
is defined like this:There is also another
Empty
which is used internally:But it's not the one used when we convert types with Chimney so the import doesn't help.