Skip to content

Commit

Permalink
Fix a regression in codec configuration serialisation with an empty c…
Browse files Browse the repository at this point in the history
…onfiguration

This fixes a regression introduced in v0.11.2 (89fc63f)
  • Loading branch information
LDeakin committed Feb 5, 2024
1 parent 387d627 commit acaad11
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Add `codecov` support to CI

### Fixed
- Fixed a regression introduced in v0.11.2 ([89fc63f](https://github.com/LDeakin/zarrs/commit/89fc63fa318cfd780e85fec6f9506ca65336a2c3)) where codecs with an empty configuration would serialise as a string rather than a struct with a `name` field, which goes against the zarr spec
- Fixes the `codec_bytes_configuration_none` test and adds `codec_crc32c_configuration_none` test

## [0.11.3] - 2024-01-31

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zarrs"
version = "0.11.3"
version = "0.11.4"
authors = ["Lachlan Deakin <[email protected]>"]
edition = "2021"
rust-version = "1.71"
Expand Down
5 changes: 4 additions & 1 deletion src/array/codec/array_to_bytes/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ mod tests {
let codec_configuration: BytesCodecConfiguration = serde_json::from_str(r#"{}"#).unwrap();
let codec = BytesCodec::new_with_configuration(&codec_configuration);
let metadata = codec.create_metadata().unwrap();
assert_eq!(serde_json::to_string(&metadata).unwrap(), r#""bytes""#);
assert_eq!(
serde_json::to_string(&metadata).unwrap(),
r#"{"name":"bytes"}"#
);
}

fn codec_bytes_round_trip_impl(
Expand Down
16 changes: 15 additions & 1 deletion src/array/codec/bytes_to_bytes/crc32c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,28 @@ const CHECKSUM_SIZE: usize = core::mem::size_of::<u32>();
#[cfg(test)]
mod tests {
use crate::{
array::{codec::BytesToBytesCodecTraits, BytesRepresentation},
array::{
codec::{BytesToBytesCodecTraits, CodecTraits},
BytesRepresentation,
},
byte_range::ByteRange,
};

use super::*;

const JSON1: &str = r#"{}"#;

#[test]
fn codec_crc32c_configuration_none() {
let codec_configuration: Crc32cCodecConfiguration = serde_json::from_str(r#"{}"#).unwrap();
let codec = Crc32cCodec::new_with_configuration(&codec_configuration);
let metadata = codec.create_metadata().unwrap();
assert_eq!(
serde_json::to_string(&metadata).unwrap(),
r#"{"name":"crc32c"}"#
);
}

#[test]
fn codec_crc32c() {
let elements: Vec<u8> = (0..6).collect();
Expand Down
20 changes: 11 additions & 9 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ impl core::fmt::Display for Metadata {
impl serde::Serialize for Metadata {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if let Some(configuration) = &self.configuration {
let mut s = s.serialize_map(Some(2))?;
s.serialize_entry("name", &self.name)?;
s.serialize_entry("configuration", configuration)?;
s.end()
if configuration.is_empty() {
let mut s = s.serialize_map(Some(1))?;
s.serialize_entry("name", &self.name)?;
s.end()
} else {
let mut s = s.serialize_map(Some(2))?;
s.serialize_entry("name", &self.name)?;
s.serialize_entry("configuration", configuration)?;
s.end()
}
} else {
s.serialize_str(self.name.as_str())
}
Expand Down Expand Up @@ -140,11 +146,7 @@ impl Metadata {
) -> Result<Self, serde_json::Error> {
let configuration = serde_json::to_value(configuration)?;
if let serde_json::Value::Object(configuration) = configuration {
if configuration.is_empty() {
Ok(Self::new(name))
} else {
Ok(Self::new_with_configuration(name, configuration))
}
Ok(Self::new_with_configuration(name, configuration))
} else {
Err(serde::ser::Error::custom(
"the configuration cannot be serialized to a JSON struct",
Expand Down

0 comments on commit acaad11

Please sign in to comment.