[Schema] Stop rejecting elicitation schemas the specification allows (#437)

* [Schema] Stop rejecting elicitation schemas the specification allows

The specification makes `title` optional on every elicitation schema
definition. AbstractSchemaDefinition required it, so a client reading a
conformant server's elicitation/create refused the request - and a server
building one could not omit it either.

`title` is now nullable on all seven definitions, omitted from the
serialized form when absent, and rejected only when present and not a
string.

* Fix stale title docblocks; allow omitting title where safe

* Give Number's title param a default too

* Apply suggestions from code review

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
This commit is contained in:
Christopher Hertel 2026-08-18 19:49:15 +02:00 • committed by GitHub
commit cf2c4d4ead
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 144 additions and 65 deletions

View file

@ -71,13 +71,23 @@ final class BooleanSchemaDefinitionTest extends TestCase
$this->assertTrue($schema->default);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = BooleanSchemaDefinition::fromArray([]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for boolean schema definition.');
/* @phpstan-ignore argument.type */
BooleanSchemaDefinition::fromArray([]);
BooleanSchemaDefinition::fromArray(['title' => 42]);
}
public function testJsonSerializeWithMinimalParams(): void

View file

@ -114,13 +114,23 @@ final class EnumSchemaDefinitionTest extends TestCase
$this->assertSame(['Poor', 'Fair', 'Good'], $schema->enumNames);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = EnumSchemaDefinition::fromArray(['enum' => ['a', 'b']]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for enum schema definition.');
/* @phpstan-ignore argument.type */
EnumSchemaDefinition::fromArray(['enum' => ['a', 'b']]);
EnumSchemaDefinition::fromArray(['title' => 42]);
}
public function testFromArrayWithMissingEnum(): void

View file

@ -137,13 +137,26 @@ final class MultiSelectEnumSchemaDefinitionTest extends TestCase
$this->assertSame(3, $schema->maxItems);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = MultiSelectEnumSchemaDefinition::fromArray([
'items' => ['type' => 'string', 'enum' => ['a']],
]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for multi-select enum schema definition.');
/* @phpstan-ignore argument.type */
MultiSelectEnumSchemaDefinition::fromArray([
'title' => 42,
'items' => ['type' => 'string', 'enum' => ['a']],
]);
}

View file

@ -133,13 +133,23 @@ final class NumberSchemaDefinitionTest extends TestCase
$this->assertSame(10, $schema->maximum);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = NumberSchemaDefinition::fromArray(['type' => 'integer']);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for number schema definition.');
/* @phpstan-ignore argument.type */
NumberSchemaDefinition::fromArray(['type' => 'integer']);
NumberSchemaDefinition::fromArray(['title' => 42]);
}
public function testJsonSerializeAsInteger(): void

View file

@ -114,13 +114,23 @@ final class StringSchemaDefinitionTest extends TestCase
$this->assertSame(100, $schema->maxLength);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = StringSchemaDefinition::fromArray([]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for string schema definition.');
/* @phpstan-ignore argument.type */
StringSchemaDefinition::fromArray([]);
StringSchemaDefinition::fromArray(['title' => 42]);
}
public function testJsonSerializeWithMinimalParams(): void

View file

@ -124,13 +124,23 @@ final class TitledEnumSchemaDefinitionTest extends TestCase
$this->assertSame('b', $schema->default);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = TitledEnumSchemaDefinition::fromArray(['oneOf' => [['const' => 'a', 'title' => 'A']]]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for titled enum schema definition.');
/* @phpstan-ignore argument.type */
TitledEnumSchemaDefinition::fromArray(['oneOf' => [['const' => 'a', 'title' => 'A']]]);
TitledEnumSchemaDefinition::fromArray(['title' => 42]);
}
public function testFromArrayWithMissingOneOf(): void

View file

@ -166,13 +166,26 @@ final class TitledMultiSelectEnumSchemaDefinitionTest extends TestCase
$this->assertSame(2, $schema->maxItems);
}
public function testFromArrayWithMissingTitle(): void
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = TitledMultiSelectEnumSchemaDefinition::fromArray([
'items' => ['anyOf' => [['const' => 'a', 'title' => 'A']]],
]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
$this->expectExceptionMessage('Invalid "title" for titled multi-select enum schema definition.');
/* @phpstan-ignore argument.type */
TitledMultiSelectEnumSchemaDefinition::fromArray([
'title' => 42,
'items' => ['anyOf' => [['const' => 'a', 'title' => 'A']]],
]);
}