Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ protected function getSchemeParameter(string $name, array $property, string $def
return new AnyOfSchema($itemsSchema, $name, $description);
}

$options = data_get($property, 'enum');

if (is_array($options) && $options !== [] && $this->isEnumOptions($options)) {
return new EnumSchema($name, $description, array_values($options));
}

$itemsSchema = $this->getSchemeParameter('', data_get($property, 'items', []), $definitionName);

return match ($type) {
Expand All @@ -389,6 +395,23 @@ protected function getSchemeParameter(string $name, array $property, string $def
};
}

/**
* EnumSchema resolves its own type from the options, so anything it cannot
* type has to fall through to the regular resolution.
*
* @param array<int|string, mixed> $options
*/
protected function isEnumOptions(array $options): bool
{
foreach ($options as $option) {
if (! is_string($option) && ! is_int($option) && ! is_float($option)) {
return false;
}
}

return true;
}

/**
* @param array<string, mixed> $property
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/TestDoubles/RelayFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ protected function fetchToolDefinitions(): array
'required' => ['nameOrId'],
],
],
[
'name' => 'enum_tool',
'description' => 'A tool with enum parameters',
'inputSchema' => [
'type' => 'object',
'properties' => [
'status' => [
'type' => 'string',
'description' => 'The status to filter on',
'enum' => ['open', 'closed'],
],
'categories' => [
'type' => 'array',
'description' => 'The categories to filter on',
'items' => [
'type' => 'string',
'enum' => ['news', 'sport'],
],
],
],
'required' => ['status'],
],
],
];
}

Expand Down
32 changes: 31 additions & 1 deletion tests/Unit/RelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Illuminate\Support\Facades\Cache;
use Prism\Prism\Schema\AnyOfSchema;
use Prism\Prism\Schema\ArraySchema;
use Prism\Prism\Schema\EnumSchema;
use Prism\Prism\Tool;
use Prism\Relay\Exceptions\ServerConfigurationException;
use Prism\Relay\Exceptions\ToolDefinitionException;
Expand Down Expand Up @@ -87,7 +89,7 @@
$tools = $relay->tools();

// Test we have the tools we expect
expect($tools)->toHaveCount(7);
expect($tools)->toHaveCount(8);
});

it('handles different parameter types correctly in tools', function (): void {
Expand Down Expand Up @@ -155,3 +157,31 @@
'description' => 'Parameter nameOrId for union_tool',
]);
});

it('maps json schema enums to enum schemas', function (): void {
$relay = new RelayFake($this->serverName);

$tool = $relay->tools()[7];
$status = $tool->parameters()['status'];

expect($status)
->toBeInstanceOf(EnumSchema::class)
->and($status->toArray())->toBe([
'description' => 'The status to filter on',
'enum' => ['open', 'closed'],
'type' => 'string',
]);
});

it('maps json schema enums nested in array items', function (): void {
$relay = new RelayFake($this->serverName);

$tool = $relay->tools()[7];
$categories = $tool->parameters()['categories'];

expect($categories)
->toBeInstanceOf(ArraySchema::class)
->and($categories->items)
->toBeInstanceOf(EnumSchema::class)
->and($categories->items->options)->toBe(['news', 'sport']);
});