Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changelogs/abilities-meta-empty-properties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
significance: patch
type: fixed
entry: "Fixed the create and update course and lesson abilities being rejected by MCP clients because the `meta` input schema encoded its empty `properties` as a JSON list."
links:
- "#3484"
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ private static function sanitize_args_to_schema( $args ) {
* `properties`, `items`, and `additionalProperties`.
*
* @since 10.1.0
* @since [version] Omit `properties` when it would be empty so it isn't encoded as a JSON list.
*
* @param array $schema A JSON Schema node (or a WP REST arg definition).
* @return array Sanitized schema node.
Expand Down Expand Up @@ -663,6 +664,11 @@ private static function sanitize_schema( $schema ) {
if ( ! empty( $required ) ) {
$sanitized['required'] = array_values( array_unique( $required ) );
}

// An empty PHP array encodes as a JSON list (`[]`), which is not a valid `properties` value.
if ( empty( $sanitized['properties'] ) ) {
unset( $sanitized['properties'] );
}
}

if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,37 @@ public function test_check_permission_denies_unauthorized() {

$this->assertFalse( $allowed );
}

/**
* Test that an object with no properties (e.g. `meta`) doesn't encode `properties` as a JSON list.
*
* @since [version]
*
* @link https://github.com/gocodebox/lifterlms/issues/3484
*
* @return void
*/
public function test_input_schema_omits_empty_properties() {

foreach ( array( 'LLMS_REST_Courses_Controller' => 'courses', 'LLMS_REST_Lessons_Controller' => 'lessons' ) as $controller => $route ) {

$schema = LLMS_Unit_Test_Util::call_method(
'LLMS_REST_Ability_Factory',
'get_input_schema',
array(
array(
'method' => 'POST',
'route' => '/llms/v1/' . $route,
'operation' => 'create',
'controller' => $controller,
),
)
);

$this->assertArrayHasKey( 'meta', $schema['properties'] );
$this->assertSame( 'object', $schema['properties']['meta']['type'] );
$this->assertArrayNotHasKey( 'properties', $schema['properties']['meta'] );
$this->assertStringNotContainsString( '"properties":[]', wp_json_encode( $schema ) );
}
}
}
Loading