Skip to content

Commit

Permalink
fix: Do not serialize twice the common fields in PropertyAffordance
Browse files Browse the repository at this point in the history
And document which between DataSchema and InteractionAffordance wins.
  • Loading branch information
lu-zero committed May 9, 2024
1 parent 3922dbc commit d79e2fd
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,38 @@ where
}
}

// Do not serialize the fields that are shared with DataSchema
fn omit_common<S, O>(i: &InteractionAffordance<O>, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
O: ExtendableThing,
{
use serde::ser::SerializeMap;

let mut s = ser.serialize_map(Some(3))?;
s.serialize_entry("forms", &i.forms)?;
if i.uri_variables.is_some() {
s.serialize_entry("uriVariables", &i.uri_variables)?;
}

Serialize::serialize(
&&i.other,
crate::flat_map_serialize::FlatMapSerializer(&mut s),
)?;

s.end()
}

/// An affordance that exposes the state of a `Thing`
///
/// The fields `title`, `titles`, `description`, `descriptions`,
/// are serialized from `PropertyAffordance::data_schema`.
#[skip_serializing_none]
#[derive(Deserialize, Serialize)]
pub struct PropertyAffordance<Other: ExtendableThing> {
/// The interaction affordance.
#[serde(flatten)]
#[serde(serialize_with = "omit_common")]
pub interaction: InteractionAffordance<Other>,

/// The data schema representing the property.
Expand Down

0 comments on commit d79e2fd

Please sign in to comment.