Skip to content

feat!: Support default field for array and map#467

Open
Kriskras99 wants to merge 1 commit intomainfrom
feat/default_array_map
Open

feat!: Support default field for array and map#467
Kriskras99 wants to merge 1 commit intomainfrom
feat/default_array_map

Conversation

@Kriskras99
Copy link
Contributor

One thing I did notice in the spec is that the text says support a single attribute while the example does show a default field, so the text of the spec should be updated as well.

#[error("Default value for a map must be a object! Got: {0}")]
MapDefaultWrongType(serde_json::Value),

#[error("Default value for a map must be a object with (String, {0})! Found: (String, {1:?})")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[error("Default value for a map must be a object with (String, {0})! Found: (String, {1:?})")]
#[error("Default value for a map must be an object with (String, {0})! Found: (String, {1:?})")]

#[error("Default value for an array must be an array of {0}! Found: {1:?}")]
ArrayDefaultWrongInnerType(Schema, Value),

#[error("Default value for a map must be a object! Got: {0}")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[error("Default value for a map must be a object! Got: {0}")]
#[error("Default value for a map must be an object! Got: {0}")]

.unwrap_or_else(|| "unexpected".to_string());
assert_eq!(expected, err);
assert_eq!(
r#"Default value for a map must be a object! Got: "invalid""#,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r#"Default value for a map must be a object! Got: "invalid""#,
r#"Default value for a map must be an object! Got: "invalid""#,


assert_eq!(
err.to_string(),
"Default value for a map must be a object with (String, String)! Found: (String, Boolean(true))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Default value for a map must be a object with (String, String)! Found: (String, Boolean(true))"
"Default value for a map must be an object with (String, String)! Found: (String, Boolean(true))"


assert_eq!(
err.to_string(),
"Default value for a map must be a object with (String, String)! Found: (String, Boolean(true))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Default value for a map must be a object with (String, String)! Found: (String, Boolean(true))"
"Default value for a map must be an object with (String, String)! Found: (String, Boolean(true))"

unreachable!("JsonValue::Object can only become a Value::Map")
};
// Check that the default type matches the schema type
if let Some(value) = map.values().find(|v| !v.validate(&types)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the values contain Schema::Ref then you will need to pass the schemata, i.e. use Value::validate_internal()


let default = if let Some(default) = complex.get("default").cloned() {
if let Value::Object(_) = default {
let crate::types::Value::Map(map) = crate::types::Value::from(default) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not caused by this PR:
This could be exploited to trigger the panic at

JsonValue::Number(n) => panic!("{n:?} does not fit into an Avro long"),

I think we should get rid of this panic!() and log an error/warning + return JsonValue::Double(f64::MAX) (a best effort).

);

let json = serde_json::to_string(&Schema::Array(array))?;
json.contains(r#""default":["foo","bar"]"#);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
json.contains(r#""default":["foo","bar"]"#);
assert!(json.contains(r#""default":["foo","bar"]"#));

assert_eq!(map.default, Some(HashMap::new()));

let json = serde_json::to_string(&Schema::Map(map))?;
json.contains(r#""default":{}"#);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
json.contains(r#""default":{}"#);
assert!(json.contains(r#""default":{}"#));

assert_eq!(map.default, Some(hashmap));

let json = serde_json::to_string(&Schema::Map(map))?;
json.contains(r#""default":{"foo":"bar"}"#);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
json.contains(r#""default":{"foo":"bar"}"#);
assert!(json.contains(r#""default":{"foo":"bar"}"#));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants