Skip to content

Commit

Permalink
fix: serialization of status list (#1423)
Browse files Browse the repository at this point in the history
* Add serialization for usize as string in StatusList2021Entry

* Add test for statusListIndex serialization in StatusList2021Entry

* chore: downgrade rust version for wasm crate

* chore: fmt
  • Loading branch information
itsyaasir authored Nov 13, 2024
1 parent 6f83696 commit e8df299
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bindings/wasm/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[toolchain]
channel = "stable"
# @itsyaasir - Update to latest stable version when wasm-bindgen is updated
channel = "1.81"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
22 changes: 21 additions & 1 deletion identity_credential/src/revocation/status_list_2021/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ where
.map(ToOwned::to_owned)
}

/// Serialize usize as string.
fn serialize_number_as_string<S>(value: &usize, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&value.to_string())
}

/// [StatusList2021Entry](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021entry) implementation.
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand All @@ -45,7 +53,10 @@ pub struct StatusList2021Entry {
#[serde(rename = "type", deserialize_with = "deserialize_status_entry_type")]
type_: String,
status_purpose: StatusPurpose,
#[serde(deserialize_with = "serde_aux::prelude::deserialize_number_from_string")]
#[serde(
deserialize_with = "serde_aux::prelude::deserialize_number_from_string",
serialize_with = "serialize_number_as_string"
)]
status_list_index: usize,
status_list_credential: Url,
}
Expand Down Expand Up @@ -142,4 +153,13 @@ mod tests {
});
serde_json::from_value::<StatusList2021Entry>(status).expect("wrong type");
}

#[test]
fn test_status_list_index_serialization() {
let base_url = Url::parse("https://example.com/credentials/status/3").unwrap();

let entry1 = StatusList2021Entry::new(base_url.clone(), StatusPurpose::Revocation, 94567, None);
let json1 = serde_json::to_value(&entry1).unwrap();
assert_eq!(json1["statusListIndex"], "94567");
}
}

0 comments on commit e8df299

Please sign in to comment.