diff --git a/modules/openapi-generator/src/main/resources/php-flight/register_routes.mustache b/modules/openapi-generator/src/main/resources/php-flight/register_routes.mustache index c66e90b42b9..42aebdee00a 100644 --- a/modules/openapi-generator/src/main/resources/php-flight/register_routes.mustache +++ b/modules/openapi-generator/src/main/resources/php-flight/register_routes.mustache @@ -73,7 +73,10 @@ function parseParam(mixed $param, string $type) } elseif (str_ends_with($type, '[]')) { return array_map(fn($el) => parseParam($el, substr($type, 0, -2)), $param); } elseif (str_starts_with($nonNullType, '\\{{escapedModelPackage}}')) { - return new $nonNullType($param); + if (enum_exists($nonNullType)) { + return $nonNullType::tryFrom($param); + } + return $nonNullType::fromArray($param); } else { return $param; } @@ -82,4 +85,4 @@ function parseParam(mixed $param, string $type) function declaresMethod(\ReflectionClass $reflectionClass, string $methodName): bool { return $reflectionClass->hasMethod($methodName) && $reflectionClass->getMethod($methodName)->getDeclaringClass()->getName() === $reflectionClass->getName(); -} \ No newline at end of file +} diff --git a/modules/openapi-generator/src/main/resources/php-flight/register_routes_test.mustache b/modules/openapi-generator/src/main/resources/php-flight/register_routes_test.mustache index c50fbade9c5..ad45d9fcca6 100644 --- a/modules/openapi-generator/src/main/resources/php-flight/register_routes_test.mustache +++ b/modules/openapi-generator/src/main/resources/php-flight/register_routes_test.mustache @@ -5,6 +5,8 @@ {{#apiInfo}} namespace {{testPackage}}; +use function {{invokerPackage}}\parseParam; + class RegisterRoutesTest extends \PHPUnit\Framework\TestCase { {{#apis}} public function testRegisterRoutes{{classname}}(): void @@ -27,5 +29,25 @@ class RegisterRoutesTest extends \PHPUnit\Framework\TestCase { $this->assertTrue(true); } {{/apis}} +{{#models}} +{{#model}} +{{#isEnum}} + public function testParseParamsEnum{{classname}}(): void + { + {{#allowableValues}} + {{#enumVars}} + {{#-first}} + $value = {{{value}}}; + {{/-first}} + {{/enumVars}} + {{/allowableValues}} + $this->assertEquals( + parseParam($value, '\\{{escapedModelPackage}}\\{{classname}}'), + \{{modelPackage}}\{{classname}}::{{#allowableValues}}{{#enumVars}}{{#-first}}{{{name}}}{{/-first}}{{/enumVars}}{{/allowableValues}} + ); + } +{{/isEnum}} +{{/model}} +{{/models}} } {{/apiInfo}} diff --git a/samples/server/petstore/php-flight/RegisterRoutes.php b/samples/server/petstore/php-flight/RegisterRoutes.php index 367e0df8daf..74bdf66b928 100644 --- a/samples/server/petstore/php-flight/RegisterRoutes.php +++ b/samples/server/petstore/php-flight/RegisterRoutes.php @@ -47,7 +47,7 @@ class RegisterRoutes { \Flight::route('DELETE /pet/@petId', function (string $petId) use ($handler) { $r = \Flight::request(); $handler->deletePet( - parseParam($petId, 'int'), + parseParam($petId, 'int'), parseParam($r->getHeader('api_key'), '?string') ); \Flight::halt(204); @@ -313,7 +313,7 @@ class RegisterRoutes { \Flight::route('GET /user/login', function () use ($handler) { $r = \Flight::request(); $result = $handler->loginUser( - parseParam($r->query['username'] ?? null, 'string'), + parseParam($r->query['username'] ?? null, 'string'), parseParam($r->query['password'] ?? null, 'string') ); if ($result === null) { @@ -343,7 +343,7 @@ class RegisterRoutes { \Flight::route('PUT /user/@username', function (string $username) use ($handler) { $r = \Flight::request(); $handler->updateUser( - parseParam($username, 'string'), + parseParam($username, 'string'), parseParam(json_decode($r->getBody(), true), '\\OpenAPIServer\\Model\\User') ); \Flight::halt(204); @@ -368,7 +368,10 @@ function parseParam(mixed $param, string $type) } elseif (str_ends_with($type, '[]')) { return array_map(fn($el) => parseParam($el, substr($type, 0, -2)), $param); } elseif (str_starts_with($nonNullType, '\\OpenAPIServer\\Model')) { - return new $nonNullType($param); + if (enum_exists($nonNullType)) { + return $nonNullType::tryFrom($param); + } + return $nonNullType::fromArray($param); } else { return $param; } @@ -377,4 +380,4 @@ function parseParam(mixed $param, string $type) function declaresMethod(\ReflectionClass $reflectionClass, string $methodName): bool { return $reflectionClass->hasMethod($methodName) && $reflectionClass->getMethod($methodName)->getDeclaringClass()->getName() === $reflectionClass->getName(); -} \ No newline at end of file +} diff --git a/samples/server/petstore/php-flight/Test/RegisterRoutesTest.php b/samples/server/petstore/php-flight/Test/RegisterRoutesTest.php index 3e7dcfefb5e..737debbe455 100644 --- a/samples/server/petstore/php-flight/Test/RegisterRoutesTest.php +++ b/samples/server/petstore/php-flight/Test/RegisterRoutesTest.php @@ -17,6 +17,8 @@ namespace OpenAPIServer\Test; +use function OpenAPIServer\parseParam; + class RegisterRoutesTest extends \PHPUnit\Framework\TestCase { public function testRegisterRoutesAbstractPetApi(): void { @@ -49,4 +51,28 @@ class RegisterRoutesTest extends \PHPUnit\Framework\TestCase { \OpenAPIServer\RegisterRoutes::registerRoutes($handler); $this->assertTrue(true); } + public function testParseParamsEnumFindPetsByStatusStatusParameterInner(): void + { + $value = 'available'; + $this->assertEquals( + parseParam($value, '\\OpenAPIServer\\Model\\FindPetsByStatusStatusParameterInner'), + \OpenAPIServer\Model\FindPetsByStatusStatusParameterInner::AVAILABLE + ); + } + public function testParseParamsEnumOrderStatus(): void + { + $value = 'placed'; + $this->assertEquals( + parseParam($value, '\\OpenAPIServer\\Model\\OrderStatus'), + \OpenAPIServer\Model\OrderStatus::PLACED + ); + } + public function testParseParamsEnumPetStatus(): void + { + $value = 'available'; + $this->assertEquals( + parseParam($value, '\\OpenAPIServer\\Model\\PetStatus'), + \OpenAPIServer\Model\PetStatus::AVAILABLE + ); + } }