serde icon indicating copy to clipboard operation
serde copied to clipboard

Adjacently tagged enums don't work in formats not supporting string values

Open inetic opened this issue 10 months ago • 4 comments

I'm using the bencode format which doesn't differentiate between strings and bytes.

In particular I'm working with serde-bencode which currently has a test ser_de_flattened_adjacently_tagged_enum ignored due of this issue.

The test fails with the error InvalidType("Invalid Type: byte array (expected: string or map)") coming from serde/ContentDeserializer::deserialize_enum.

Before returning the error, the self.content variable contains Content::ByteBuf(b"Request") instead of a string.

Applying the below patch in that function seems to resolve the issue, but I'm not too familiar with the serde code so please let me know if you think this is or isn't an adequate solution (full diff here).

-                s @ Content::String(_) | s @ Content::Str(_) => (s, None),
+                s @ Content::String(_)
+               | s @ Content::Str(_)
+               | s @ Content::ByteBuf(_)
+               | s @ Content::Bytes(_) => (s, None),

For posterity, adjacently tagged enums worked OK in serde-bencode with serde v1.0.180 but broke with >=v1.0.181.

inetic avatar Jan 14 '25 12:01 inetic

Could you check if #2811 solves this problem?

Mingun avatar Jan 14 '25 17:01 Mingun

Could you check if https://github.com/serde-rs/serde/pull/2811 solves this problem?

Sorry to say, but no. The same serde-bencode tests ([1], [2]) fail with your branch as with the upstream with a slightly different error message.

inetic avatar Jan 15 '25 07:01 inetic

It seems that the thing that broke serde-bencode is #2496 and #2505. This change broke several protocols

Mingun avatar Jan 15 '25 16:01 Mingun

Applying the below patch in that function seems to resolve the issue, but I'm not too familiar with the serde code so please let me know if you think this is or isn't an adequate solution (full diff here).

I wonder whether we could just always let the format handle this and not reject any of the other Content variants.

oli-obk avatar Jan 20 '25 12:01 oli-obk