Open AlisCode opened 4 hours ago
The key that it tries to serialize is a Content::UnitVariant("MyEnum", 0, "A")
. I'm not sure what the right thing is to do here. Maybe format it as MyEnum::A
? I don't think we can reliably match what serde would do normally.
serde_json
serializes it as "A"
for sure, and as "a"
if using e.g. #[serde(rename_all = "camelCase")]
, but I see that adding the rename to MyEnum changes it to Content::UnitVariant("MyEnum", 0, "a")
, so it looks like that 3rd argument is what we're looking for ? I dont think MyEnum::A
is the right way to go.
What do you think of adding a case for UnitVariant
in the code that serializes a map, like this :
diff --git a/insta/src/content/json.rs b/insta/src/content/json.rs
index e4dd5e6..c03556c 100644
--- a/insta/src/content/json.rs
+++ b/insta/src/content/json.rs
@@ -180,6 +180,8 @@ impl Serializer {
let real_key = key.resolve_inner();
if let Content::String(ref s) = real_key {
self.write_escaped_str(s);
+ } else if let Content::UnitVariant(_, _, s) = real_key {
+ self.write_escaped_str(s);
} else if let Some(num) = real_key.as_i64() {
self.write_escaped_str(&num.to_string());
} else if let Some(num) = real_key.as_i128() {
I dont know if that would solve every other use cases, but it does work for the use case of this bug. If you approve of that idea, I'll happily create a PR :)
I don't think we can reliably match what serde would do normally.
I was surprised to learn that insta
is not using serde_json
under the hood, I wonder what the rationale is ?
What happened?
It looks like
insta
does not support serializing the typeBTreeMap<K, V>
when K is a custom enum.serde_json
does that fine, though.Reproduction steps
Here is an example that reproduces the issue.
The serialization through
serde
works fine, butinsta
fails to serialize with the following error :Insta Version
1.41.1
rustc Version
1.80.1
What did you expect?
I expect my BTreeMap to be correctly serialized in JSON and a snapshot to be generated