From c223b3710a3ec0ffa46ceeacb87e2b1895fd0821 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Tue, 26 Nov 2024 10:40:34 +0100 Subject: [PATCH] fix: Resolve conflict between "debugId" and "debug_id" (#100) Using an alias produces an error when both fields are set. The intended behavior is to only read from "debug_id" if "debugId" is not present. We now encode this logic explicitly by deserializing both and choosing manually. --- CHANGELOG.md | 7 +++++++ src/decoder.rs | 4 +++- src/encoder.rs | 2 ++ src/jsontypes.rs | 6 +++++- src/types.rs | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdf0f3f..2057f67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +### Various fixes & improvements + +- Fixed an error when deserializing sourcemaps with + both `"debugId"` and `"debug_id"` keys (#100) by @loewenheim + ## 9.1.0 ### Various fixes & improvements diff --git a/src/decoder.rs b/src/decoder.rs index 464505e..456c705 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -253,7 +253,9 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { let mut sm = SourceMap::new(file, tokens, names, sources, source_content); sm.set_source_root(rsm.source_root); - sm.set_debug_id(rsm.debug_id); + // Use debug_id_old (from "debug_id" key) only if debug_id + // from ( "debugId" key) is unset + sm.set_debug_id(rsm.debug_id.or(rsm.debug_id_old)); if let Some(ignore_list) = rsm.ignore_list { for idx in ignore_list { sm.add_to_ignore_list(idx); diff --git a/src/encoder.rs b/src/encoder.rs index fc0d8f5..f5f056a 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -179,6 +179,7 @@ impl Encodable for SourceMap { x_metro_module_paths: None, x_facebook_sources: None, debug_id: self.get_debug_id(), + debug_id_old: None, } } } @@ -213,6 +214,7 @@ impl Encodable for SourceMapIndex { x_metro_module_paths: None, x_facebook_sources: None, debug_id: None, + debug_id_old: None, } } } diff --git a/src/jsontypes.rs b/src/jsontypes.rs index b0fc685..2e9b01a 100644 --- a/src/jsontypes.rs +++ b/src/jsontypes.rs @@ -54,8 +54,12 @@ pub struct RawSourceMap { pub x_metro_module_paths: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub x_facebook_sources: FacebookSources, - #[serde(skip_serializing_if = "Option::is_none", alias = "debugId")] + #[serde(skip_serializing_if = "Option::is_none", rename = "debugId")] pub debug_id: Option, + // This field only exists to be able to deserialize from "debug_id" keys + // if "debugId" is unset. + #[serde(skip_serializing_if = "Option::is_none", rename = "debug_id")] + pub debug_id_old: Option, } #[derive(Deserialize)] diff --git a/src/types.rs b/src/types.rs index bcc5253..72398f2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1340,6 +1340,7 @@ mod tests { "sources":["coolstuff.js"], "names":["x","alert"], "mappings":"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM", + "debug_id": "11111111-1111-1111-1111-111111111111", "debugId":"00000000-0000-0000-0000-000000000000" }"#;