Skip to content

Commit eecfd65

Browse files
committed
Use JsonValue for post type supports field
WordPress 6.9 returns post type supports as either bool or array, so we use JsonValue wrapped in PostTypeSupportsValue for flexibility. Changes: - Add PostTypeSupportsValue type wrapping JsonValue - Update PostTypeSupportsMap to use Arc<PostTypeSupportsValue> - Update test assertion to use as_json_bool() method
1 parent 2896ed4 commit eecfd65

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

wp_api/src/post_types.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::impl_as_query_value_from_to_string;
1+
use crate::{JsonValue, impl_as_query_value_from_to_string};
22
use serde::{Deserialize, Serialize};
33
use std::collections::HashMap;
44
use std::str::FromStr;
5+
use std::sync::Arc;
56
use wp_contextual::WpContextual;
67
use wp_serde_helper::deserialize_empty_array_or_hashmap;
78

@@ -97,7 +98,28 @@ pub struct PostTypeSupportsMap {
9798
#[serde(deserialize_with = "deserialize_empty_array_or_hashmap")]
9899
#[serde(flatten)]
99100
#[serde(rename = "supports")]
100-
pub map: HashMap<PostTypeSupports, bool>,
101+
pub map: HashMap<PostTypeSupports, Arc<PostTypeSupportsValue>>,
102+
}
103+
104+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Object)]
105+
#[serde(transparent)]
106+
pub struct PostTypeSupportsValue {
107+
#[serde(flatten)]
108+
pub json_value: JsonValue,
109+
}
110+
111+
#[uniffi::export]
112+
impl PostTypeSupportsValue {
113+
fn as_json_value(&self) -> JsonValue {
114+
self.json_value.clone()
115+
}
116+
117+
pub fn as_json_bool(&self) -> Option<bool> {
118+
match self.json_value {
119+
JsonValue::Bool(b) => Some(b),
120+
_ => None,
121+
}
122+
}
101123
}
102124

103125
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, uniffi::Record)]

wp_api_integration_tests/tests/test_post_types_immut.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ async fn retrieve_post_types_with_edit_context(
9393
// post types might not support `Title` in which case it's perfectly fine to completely
9494
// remove this assertion.
9595
assert_eq!(
96-
post_type.supports.map.get(&PostTypeSupports::Title),
97-
Some(true).as_ref()
96+
post_type
97+
.supports
98+
.map
99+
.get(&PostTypeSupports::Title)
100+
.and_then(|v| v.as_json_bool()),
101+
Some(true)
98102
);
99103
// All post types in our current testing sites have `EditPost` capability, so we use this
100104
// assertion to verify that we are able to parse `capabilities` field properly.

0 commit comments

Comments
 (0)