swagger-codegen or openapi-generator PHP version

  docker, openapi-generator, php, swagger, version-control

The actual question: is it possible to choose the PHP version (5.6, 7.1, 7.2, …) to generate code?

I got a swagger.json from https://api.otto.market/docs
which should be an openapi.json or so, since the file contains "openapi": "3.0.3",. Anyway …

What i found out so far is that they (swagger and openapi-generator) seem to have templates for the code generation.
And those templates are written in the language for the version in use.

F.e. this (openapitools/openapi-generator-cli)

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate 
-i /local/path/to/swagger.json 
-g php 
-o /local/path/to/generated/code/

creates a composer.json with

...
"require": {
    "php": "^7.3 || ^8.0",
    ...
},
...

I now COULD use the templates and change the requirements.
But i think this would not be the target of a code generator.
Info about templates added to bottom.

What i guess is that there should be templates written for other PHP version.
But how would i choose them?
Are there any?


Swagger: as i read swagger came first, and openapi-generator is a fork.
So i tried swagger:

docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate 
-i /local/path/to/swagger.json 
-l php 
-o /local/path/to/generated/code/

Swagger created code for PHP 5.5 (as of the generated composer.json).
But it brings warnings and the generated code looks "broken".
I actually do not wonder since the file is made for openapi": "3.0.3.

But what i see is: swagger has templates for PHP 5.5.


My personal current conclusion:
I can generate code real quick f.e. for an API that i do not really (have to) know.
But how does it help me if i cannot change the language version?

F.e. i need an API client in 2 projects.
One is in PHP 7.1 and the other one in 7.3. How do i solve this?

Anybody had to deal with this? Or any ideas?



INFO
templates: how to use templates:
Call docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli author template -g php -o /local/templates/
to save all templates to path templates/.
You then can f.e. copy the composer.mustache to f.e. deploy/templates/ and change it.
On generating code you use -t /local/deploy/templates to use your changed template.
Example:

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate 
-i /local/path/to/swagger.json 
-g php 
-o /local/path/to/generated/code/ 
-t /local/deploy/templates

Source: Ask PHP

LEAVE A COMMENT