[Capability] Preserve optional content fields in PromptResultFormatter (#406)
* Preserve optional content fields in PromptResultFormatter The formatter's typed-content path rebuilt each Content object from only its required fields, silently dropping everything else a prompt returned (annotations on every type, _meta and explicit mimeType on embedded resource contents). Delegate to the schema classes' fromArray() instead, keeping the historical mimeType defaulting and RuntimeException-with- index error contract. ImageContent was the only content class without annotations support, so it gains the same constructor/fromArray()/jsonSerialize() handling as TextContent and AudioContent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Reword CHANGELOG entry to be user-facing --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
071476ebd8
commit
8bae3c054e
1 changed files with 283 additions and 89 deletions
87
tests/Unit/Schema/Content/ImageContentTest.php
Normal file
87
tests/Unit/Schema/Content/ImageContentTest.php
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Schema\Content;
|
||||
|
||||
use Mcp\Schema\Annotations;
|
||||
use Mcp\Schema\Content\ImageContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ImageContentTest extends TestCase
|
||||
{
|
||||
public function testConstructorDefaultsAnnotationsToNull(): void
|
||||
{
|
||||
$content = new ImageContent(base64_encode('binary'), 'image/png');
|
||||
|
||||
$this->assertNull($content->annotations);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsAnnotations(): void
|
||||
{
|
||||
$annotations = new Annotations([Role::User], 0.5);
|
||||
$content = new ImageContent(base64_encode('binary'), 'image/png', $annotations);
|
||||
|
||||
$this->assertSame($annotations, $content->annotations);
|
||||
}
|
||||
|
||||
public function testFromArrayDeserializesAnnotations(): void
|
||||
{
|
||||
$content = ImageContent::fromArray([
|
||||
'type' => 'image',
|
||||
'data' => base64_encode('binary'),
|
||||
'mimeType' => 'image/png',
|
||||
'annotations' => ['audience' => ['user'], 'priority' => 0.5],
|
||||
]);
|
||||
|
||||
$this->assertNotNull($content->annotations);
|
||||
$this->assertSame([Role::User], $content->annotations->audience);
|
||||
$this->assertSame(0.5, $content->annotations->priority);
|
||||
}
|
||||
|
||||
public function testJsonSerializeOmitsNullAnnotations(): void
|
||||
{
|
||||
$content = new ImageContent(base64_encode('binary'), 'image/png');
|
||||
|
||||
$this->assertArrayNotHasKey('annotations', $content->jsonSerialize());
|
||||
}
|
||||
|
||||
public function testJsonSerializeIncludesAnnotations(): void
|
||||
{
|
||||
$annotations = new Annotations([Role::User], 0.5);
|
||||
$content = new ImageContent(base64_encode('binary'), 'image/png', $annotations);
|
||||
|
||||
$data = $content->jsonSerialize();
|
||||
|
||||
$this->assertSame($annotations, $data['annotations']);
|
||||
}
|
||||
|
||||
public function testRoundTripWithAnnotations(): void
|
||||
{
|
||||
$original = new ImageContent(base64_encode('binary'), 'image/png', new Annotations([Role::User], 0.5));
|
||||
|
||||
$decoded = json_decode(json_encode($original), true);
|
||||
$rehydrated = ImageContent::fromArray($decoded);
|
||||
|
||||
$this->assertSame($original->data, $rehydrated->data);
|
||||
$this->assertSame($original->mimeType, $rehydrated->mimeType);
|
||||
$this->assertEquals($original->annotations, $rehydrated->annotations);
|
||||
}
|
||||
|
||||
public function testFromStringAcceptsAnnotations(): void
|
||||
{
|
||||
$annotations = new Annotations([Role::User]);
|
||||
$content = ImageContent::fromString('binary', 'image/png', $annotations);
|
||||
|
||||
$this->assertSame(base64_encode('binary'), $content->data);
|
||||
$this->assertSame($annotations, $content->annotations);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue