Fix to let schema attribute enum type hint allow null values (#271)

This commit is contained in:
Sule-Balogun Olanrewaju 2026-04-11 16:21:14 +01:00 • committed by GitHub
commit 5d57b685a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 24 deletions

View file

@ -24,8 +24,8 @@ namespace Mcp\Capability\Attribute;
* definition?: array<string, mixed>, * definition?: array<string, mixed>,
* type?: string, * type?: string,
* description?: string, * description?: string,
* enum?: array<int, float|string|int>, * enum?: array<int, int|float|string|null>,
* gormat?: string, * format?: string,
* minLength?: int, * minLength?: int,
* maxLength?: int, * maxLength?: int,
* pattern?: string, * pattern?: string,
@ -64,7 +64,7 @@ class Schema
public ?string $description = null; public ?string $description = null;
public mixed $default = null; public mixed $default = null;
/** /**
* @var ?array<int, float|string|int> * @var ?array<int, int|float|string|null>
*/ */
public ?array $enum = null; // list of allowed values public ?array $enum = null; // list of allowed values
public ?string $format = null; // e.g., 'email', 'date-time' public ?string $format = null; // e.g., 'email', 'date-time'
@ -108,7 +108,7 @@ class Schema
* @param ?array<string, mixed> $definition A complete JSON schema array. If provided, other parameters are ignored. * @param ?array<string, mixed> $definition A complete JSON schema array. If provided, other parameters are ignored.
* @param ?string $type the JSON schema type * @param ?string $type the JSON schema type
* @param ?string $description description of the element * @param ?string $description description of the element
* @param ?array<int, float|string|int> $enum allowed enum values * @param ?array<int, int|float|string|null> $enum allowed enum values
* @param ?string $format String format (e.g., 'date-time', 'email'). * @param ?string $format String format (e.g., 'date-time', 'email').
* @param ?int $minLength minimum length for strings * @param ?int $minLength minimum length for strings
* @param ?int $maxLength maximum length for strings * @param ?int $maxLength maximum length for strings

View file

@ -46,7 +46,7 @@ use phpDocumentor\Reflection\DocBlock\Tags\Param;
* type?: string|array, * type?: string|array,
* description?: string, * description?: string,
* default?: mixed, * default?: mixed,
* enum?: array<string|int>, * enum?: array<int, int|float|string|null>,
* items?: array<string, mixed>, * items?: array<string, mixed>,
* } * }
* @phpstan-type VariadicParameterSchema array{ * @phpstan-type VariadicParameterSchema array{

View file

@ -471,6 +471,7 @@ class SchemaValidatorTest extends TestCase
'active' => ['type' => 'boolean'], 'active' => ['type' => 'boolean'],
'score' => ['type' => 'number'], 'score' => ['type' => 'number'],
'items' => ['type' => 'array', 'items' => ['type' => 'string']], 'items' => ['type' => 'array', 'items' => ['type' => 'string']],
'status' => ['enum' => [null, 'active', 'inactive']],
'nullableValue' => ['type' => ['string', 'null']], 'nullableValue' => ['type' => ['string', 'null']],
'optionalValue' => ['type' => 'string'], 'optionalValue' => ['type' => 'string'],
], ],
@ -486,6 +487,7 @@ class SchemaValidatorTest extends TestCase
* active: bool, * active: bool,
* score: float, * score: float,
* items: string[], * items: string[],
* status: 'active'|'inactive'|null,
* nullableValue: null, * nullableValue: null,
* optionalValue: string * optionalValue: string
* } * }
@ -498,6 +500,7 @@ class SchemaValidatorTest extends TestCase
'active' => true, 'active' => true,
'score' => 99.5, 'score' => 99.5,
'items' => ['a', 'b'], 'items' => ['a', 'b'],
'status' => null,
'nullableValue' => null, 'nullableValue' => null,
'optionalValue' => 'present', 'optionalValue' => 'present',
]; ];