diff --git a/native/kotlin/api/kotlin/src/integrationTest/kotlin/PostTypesEndpointTest.kt b/native/kotlin/api/kotlin/src/integrationTest/kotlin/PostTypesEndpointTest.kt index f954e8037..7f210cb73 100644 --- a/native/kotlin/api/kotlin/src/integrationTest/kotlin/PostTypesEndpointTest.kt +++ b/native/kotlin/api/kotlin/src/integrationTest/kotlin/PostTypesEndpointTest.kt @@ -6,6 +6,7 @@ import uniffi.wp_api.PostType import uniffi.wp_api.PostTypeCapabilities import uniffi.wp_api.PostTypeSupports import uniffi.wp_api.WpErrorCode +import uniffi.wp_api.postTypeSupports import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull @@ -26,7 +27,7 @@ class PostTypesEndpointTest { val postTypesPost = client.request { requestBuilder -> requestBuilder.postTypes().retrieveWithEditContext(PostType.Post) }.assertSuccessAndRetrieveData().data - assert(postTypesPost.supports.map[PostTypeSupports.Title]!!) + assert(postTypeSupports(postTypesPost.supports, PostTypeSupports.Title)) assertFalse(postTypesPost.capabilities[PostTypeCapabilities.EditPosts]!!.isEmpty()) } diff --git a/native/swift/Sources/wordpress-api/Extensions.swift b/native/swift/Sources/wordpress-api/Extensions.swift index 93c80cf02..30db47e8f 100644 --- a/native/swift/Sources/wordpress-api/Extensions.swift +++ b/native/swift/Sources/wordpress-api/Extensions.swift @@ -122,3 +122,13 @@ extension PostType: ExpressibleByStringLiteral { self.init(stringLiteral) } } + +public extension PostTypeSupportsMap { + func supports(_ feature: PostTypeSupports) -> Bool { + postTypeSupports(supportsMap: self, feature: feature) + } + + func supports(_ feature: String) -> Bool { + postTypeSupports(supportsMap: self, feature: .custom(feature)) + } +} diff --git a/wp_api/src/post_types.rs b/wp_api/src/post_types.rs index b905d5c50..54f378fe9 100644 --- a/wp_api/src/post_types.rs +++ b/wp_api/src/post_types.rs @@ -1,4 +1,4 @@ -use crate::impl_as_query_value_from_to_string; +use crate::{JsonValue, impl_as_query_value_from_to_string}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::str::FromStr; @@ -97,7 +97,29 @@ pub struct PostTypeSupportsMap { #[serde(deserialize_with = "deserialize_empty_array_or_hashmap")] #[serde(flatten)] #[serde(rename = "supports")] - pub map: HashMap, + pub map: HashMap, +} + +impl PostTypeSupportsMap { + /// Check if the post type supports a specific feature by checking if the key is present. + /// + /// Note: This only checks for key presence, not the associated value. WordPress typically + /// includes a feature in the map with a `true` value when supported, and omits it entirely + /// when not supported. The value can also be an object with additional configuration (e.g., + /// `editor` may have nested settings). We assume that if a key is present, the feature is + /// supported, regardless of the actual value. + pub fn supports(&self, feature: &PostTypeSupports) -> bool { + self.map.contains_key(feature) + } +} + +/// Check if a post type supports a specific feature by checking if the key is present. +/// +/// Note: This only checks for key presence, not the associated value. See +/// `PostTypeSupportsMap::supports` for details on this assumption. +#[uniffi::export] +fn post_type_supports(supports_map: &PostTypeSupportsMap, feature: PostTypeSupports) -> bool { + supports_map.supports(&feature) } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, uniffi::Record)] diff --git a/wp_api_integration_tests/tests/test_post_types_immut.rs b/wp_api_integration_tests/tests/test_post_types_immut.rs index 140cba347..532df4b91 100644 --- a/wp_api_integration_tests/tests/test_post_types_immut.rs +++ b/wp_api_integration_tests/tests/test_post_types_immut.rs @@ -92,10 +92,7 @@ async fn retrieve_post_types_with_edit_context( // It's possible that we might have more test sites in the future and some of their // post types might not support `Title` in which case it's perfectly fine to completely // remove this assertion. - assert_eq!( - post_type.supports.map.get(&PostTypeSupports::Title), - Some(true).as_ref() - ); + assert!(post_type.supports.supports(&PostTypeSupports::Title)); // All post types in our current testing sites have `EditPost` capability, so we use this // assertion to verify that we are able to parse `capabilities` field properly. //