[BUG] PHP Symfony server generator forces return type of actions to be an array
Created by: ivan-aksamentov
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
php-symfony
server generator produces incorrect return type annotations for actions: for object types, it will erroneously generate an annotation for the type that is an array of these objects, which produces runtime errors on some configurations of Symfony.
For example, for a request that returns Foo
, instead of generating type annotation as Foo
it will generate type annotation Foo[]
.
To demonstrate, given the default Pet Store swagger 2.0 contract, where getPetById
operation returns a single object of type Pet
(only relevant parts are preserved):
swagger: "2.0"
paths:
/pet/{petId}:
get:
tags:
- "pet"
summary: "Find pet by ID"
description: "Returns a single pet"
operationId: "getPetById"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/Pet"
definitions:
Pet:
type: "object"
...the generated Symfony action getPetById
will be erroneously annotated to return an array. Notice @return OpenAPI\Server\Model\Pet[]
:
/**
* Operation getPetById
*
* Find pet by ID
*
* @param int $petId ID of pet to return (required)
* @param integer $responseCode The HTTP response code to return
* @param array $responseHeaders Additional HTTP headers to return with the response ()
*
* @return OpenAPI\Server\Model\Pet[]
*
*/
public function getPetById($petId, &$responseCode, array &$responseHeaders);
openapi-generator version
I am using npm package for 4.0.3: "@openapitools/openapi-generator-cli": "0.0.16-4.0.3"
But the issue is present on master and v5 branches too.
OpenAPI declaration file content or url
I used the Swagger 2.0 Pet Store example: https://github.com/ivan-aksamentov/openapi-generator-symfony-array-bug-repro/blob/57c538bc7d/petstore.yml
Command line used for generation
openapi-generator generate -g php-symfony -i petstore.yml -o generated/
Steps to reproduce
One can reproduce this issue with this example repository: https://github.com/ivan-aksamentov/openapi-generator-symfony-array-bug-repro
using the following steps (git, java, node and yarn are required):
git clone https://github.com/ivan-aksamentov/openapi-generator-symfony-array-bug-repro
cd openapi-generator-symfony-array-bug-repro
yarn install
yarn generate
After generation, inspect generated/Api/PetApiInterface.php#L121-L133
:
https://github.com/ivan-aksamentov/openapi-generator-symfony-array-bug-repro/blob/57c538bc7d/generated/Api/PetApiInterface.php#L121-L133
Related issues/PRs
Could not find ones
Suggest a fix
The bug is in this snippet in PhpSymfonyServerCodegen.java#L405-L415
:
Note how any non-primitive return type turns into an array:
if (!op.returnTypeIsPrimitive) {
op.vendorExtensions.put("x-commentType", op.returnType + "[]");
}
I submitted PR #3515 that attempts to fix this issue.