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
24 changes: 24 additions & 0 deletions fixtures/StructuredOutput/ExampleDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Fixtures\StructuredOutput;

use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With;

class ExampleDto
{
public function __construct(
public string $name,
#[With(enum: [7, 19])] public int $taxRate,
#[With(enum: ['Foo', 'Bar', null])] public ?string $category,
) {
}
}
1 change: 1 addition & 0 deletions src/platform/phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
paths:
- src/
- tests/
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"
Expand Down
9 changes: 5 additions & 4 deletions src/platform/src/Contract/JsonSchema/Attribute/With.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
final readonly class With
{
/**
* @param list<int|string>|null $enum
* @param string|int|string[]|null $const
* @param list<int|float|string|null>|null $enum
* @param string|int|string[]|null $const
*/
public function __construct(
// can be used by many types
Expand Down Expand Up @@ -53,8 +53,9 @@ public function __construct(
public ?bool $dependentRequired = null,
) {
if (\is_array($enum)) {
if (array_filter($enum, fn ($item) => \is_string($item)) !== $enum) {
throw new InvalidArgumentException('All enum values must be strings.');
/* @phpstan-ignore-next-line function.alreadyNarrowedType */
if (array_filter($enum, fn (mixed $item) => null === $item || \is_int($item) || \is_float($item) || \is_string($item)) !== $enum) {
throw new InvalidArgumentException('All enum values must be float, integer, strings, or null.');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function testValidEnum()
$this->assertSame($enum, $toolParameter->enum);
}

public function testInvalidEnumContainsNonString()
public function testInvalidEnumContainsInvalidType()
{
$this->expectException(InvalidArgumentException::class);
$enum = ['value1', 2];
new With(enum: $enum);
$enum = ['value1', new \stdClass()];
new With(enum: $enum); /* @phpstan-ignore-line argument.type */
}

public function testValidConstString()
Expand Down
25 changes: 25 additions & 0 deletions src/platform/tests/Contract/JsonSchema/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Fixtures\StructuredOutput\ExampleDto;
use Symfony\AI\Fixtures\StructuredOutput\MathReasoning;
use Symfony\AI\Fixtures\StructuredOutput\Step;
use Symfony\AI\Fixtures\StructuredOutput\User;
Expand Down Expand Up @@ -240,4 +241,28 @@ public function testBuildPropertiesForStepClass()

$this->assertSame($expected, $actual);
}

public function testBuildPropertiesForExampleDto()
{
$expected = [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'taxRate' => [
'type' => 'integer',
'enum' => [7, 19],
],
'category' => [
'type' => ['string', 'null'],
'enum' => ['Foo', 'Bar', null],
],
],
'required' => ['name', 'taxRate'],
'additionalProperties' => false,
];

$actual = $this->factory->buildProperties(ExampleDto::class);

$this->assertSame($expected, $actual);
}
}