Skip to content

Commit 2389f4d

Browse files
oguzkocerjkmassel
andauthored
Use JsonValue for post type supports field (#1050)
* 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 * Update Kotlin test to use asJsonBool() for post type supports * Add post_type_supports helper to check feature support Add a `supports` method to `PostTypeSupportsMap` for Rust usage and a `post_type_supports` free function for FFI clients to check if a post type supports a specific feature by key presence. Includes documentation explaining the assumption that key presence implies support, regardless of the associated value. * Replace PostTypeSupportsValue with JsonValue directly Simplify post type supports by using JsonValue directly instead of the PostTypeSupportsValue wrapper type. Tests now use the supports method on PostTypeSupportsMap to check feature support. Changes: - Change PostTypeSupportsMap.map from HashMap<_, Arc<PostTypeSupportsValue>> to HashMap<_, JsonValue> - Remove PostTypeSupportsValue struct and its impl block - Update Rust test to use supports method directly - Update Kotlin test to use postTypeSupports helper * Add Swift support --------- Co-authored-by: Jeremy Massel <1123407+jkmassel@users.noreply.github.com>
1 parent cf1fe5d commit 2389f4d

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

native/kotlin/api/kotlin/src/integrationTest/kotlin/PostTypesEndpointTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import uniffi.wp_api.PostType
66
import uniffi.wp_api.PostTypeCapabilities
77
import uniffi.wp_api.PostTypeSupports
88
import uniffi.wp_api.WpErrorCode
9+
import uniffi.wp_api.postTypeSupports
910
import kotlin.test.assertEquals
1011
import kotlin.test.assertFalse
1112
import kotlin.test.assertNull
@@ -26,7 +27,7 @@ class PostTypesEndpointTest {
2627
val postTypesPost = client.request { requestBuilder ->
2728
requestBuilder.postTypes().retrieveWithEditContext(PostType.Post)
2829
}.assertSuccessAndRetrieveData().data
29-
assert(postTypesPost.supports.map[PostTypeSupports.Title]!!)
30+
assert(postTypeSupports(postTypesPost.supports, PostTypeSupports.Title))
3031
assertFalse(postTypesPost.capabilities[PostTypeCapabilities.EditPosts]!!.isEmpty())
3132
}
3233

native/swift/Sources/wordpress-api/Extensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@ extension PostType: ExpressibleByStringLiteral {
122122
self.init(stringLiteral)
123123
}
124124
}
125+
126+
public extension PostTypeSupportsMap {
127+
func supports(_ feature: PostTypeSupports) -> Bool {
128+
postTypeSupports(supportsMap: self, feature: feature)
129+
}
130+
131+
func supports(_ feature: String) -> Bool {
132+
postTypeSupports(supportsMap: self, feature: .custom(feature))
133+
}
134+
}

wp_api/src/post_types.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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;
@@ -97,7 +97,29 @@ pub struct PostTypeSupportsMap {
9797
#[serde(deserialize_with = "deserialize_empty_array_or_hashmap")]
9898
#[serde(flatten)]
9999
#[serde(rename = "supports")]
100-
pub map: HashMap<PostTypeSupports, bool>,
100+
pub map: HashMap<PostTypeSupports, JsonValue>,
101+
}
102+
103+
impl PostTypeSupportsMap {
104+
/// Check if the post type supports a specific feature by checking if the key is present.
105+
///
106+
/// Note: This only checks for key presence, not the associated value. WordPress typically
107+
/// includes a feature in the map with a `true` value when supported, and omits it entirely
108+
/// when not supported. The value can also be an object with additional configuration (e.g.,
109+
/// `editor` may have nested settings). We assume that if a key is present, the feature is
110+
/// supported, regardless of the actual value.
111+
pub fn supports(&self, feature: &PostTypeSupports) -> bool {
112+
self.map.contains_key(feature)
113+
}
114+
}
115+
116+
/// Check if a post type supports a specific feature by checking if the key is present.
117+
///
118+
/// Note: This only checks for key presence, not the associated value. See
119+
/// `PostTypeSupportsMap::supports` for details on this assumption.
120+
#[uniffi::export]
121+
fn post_type_supports(supports_map: &PostTypeSupportsMap, feature: PostTypeSupports) -> bool {
122+
supports_map.supports(&feature)
101123
}
102124

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

wp_api_integration_tests/tests/test_post_types_immut.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ async fn retrieve_post_types_with_edit_context(
9292
// It's possible that we might have more test sites in the future and some of their
9393
// post types might not support `Title` in which case it's perfectly fine to completely
9494
// remove this assertion.
95-
assert_eq!(
96-
post_type.supports.map.get(&PostTypeSupports::Title),
97-
Some(true).as_ref()
98-
);
95+
assert!(post_type.supports.supports(&PostTypeSupports::Title));
9996
// All post types in our current testing sites have `EditPost` capability, so we use this
10097
// assertion to verify that we are able to parse `capabilities` field properly.
10198
//

0 commit comments

Comments
 (0)