Skip to content

Commit 77fa028

Browse files
BenjaminHaeBenjamin Haeubleinrenal31
authored
[php-symfony] Symfony6 support (#11810)
* support symfony6 * fix issues with StrictJsonDeserialization * regenerate samples * add suggestions * update samples * support php 7.4 and symfony 5 * allow versions based on semantic versioning * regenerate sample * change method of determining result types * update samples * describe usage of bundle in symfony app * better documentation * fix duplicate auth methods * do not set namespace for default types * fix UploadedFile type * next try fixing auth * regenerate samples * fix: auth method shall not be duplicated * Revert "fix duplicate auth methods" This reverts commit 0dc4187. * chore: regenerate samples * fix tests * regenerate sample * more fixes for tests * update tests * add kernel shutdown Co-authored-by: Benjamin Haeublein <[email protected]> Co-authored-by: Renaud de Chivré <[email protected]>
1 parent 96dd6c5 commit 77fa028

File tree

70 files changed

+603
-776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+603
-776
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public void processOpts() {
348348
supportingFiles.add(new SupportingFile("serialization/SerializerInterface.mustache", toSrcPath(servicePackage, srcBasePath), "SerializerInterface.php"));
349349
supportingFiles.add(new SupportingFile("serialization/JmsSerializer.mustache", toSrcPath(servicePackage, srcBasePath), "JmsSerializer.php"));
350350
supportingFiles.add(new SupportingFile("serialization/StrictJsonDeserializationVisitor.mustache", toSrcPath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitor.php"));
351+
supportingFiles.add(new SupportingFile("serialization/StrictJsonDeserializationVisitorFactory.mustache", toSrcPath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitorFactory.php"));
351352
supportingFiles.add(new SupportingFile("serialization/TypeMismatchException.mustache", toSrcPath(servicePackage, srcBasePath), "TypeMismatchException.php"));
352353
// Validation components
353354
supportingFiles.add(new SupportingFile("validation/ValidatorInterface.mustache", toSrcPath(servicePackage, srcBasePath), "ValidatorInterface.php"));
@@ -434,11 +435,26 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
434435
} else {
435436
op.vendorExtensions.put("x-comment-type", "void");
436437
}
438+
// Create a variable to add typing for return value of interface
439+
if (op.returnType != null) {
440+
if ("array".equals(op.returnContainer)) {
441+
op.vendorExtensions.put("x-return-type", "iterable");
442+
} else {
443+
if (defaultIncludes.contains(op.returnType)) {
444+
op.vendorExtensions.put("x-return-type", "array|" + op.returnType);
445+
}
446+
else {
447+
op.vendorExtensions.put("x-return-type", "array|\\" + op.returnType);
448+
}
449+
}
450+
} else {
451+
op.vendorExtensions.put("x-return-type", "void");
452+
}
437453

438454
// Add operation's authentication methods to whole interface
439455
if (op.authMethods != null) {
440456
for (CodegenSecurity am : op.authMethods) {
441-
if (!authMethods.contains(am)) {
457+
if (authMethods.stream().noneMatch(m -> Objects.equals(m.name, am.name))) {
442458
authMethods.add(am);
443459
}
444460
}

modules/openapi-generator/src/main/resources/php-symfony/.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: php
22
dist: trusty
33
php:
4-
- 7.1.3
5-
- 7.2
4+
- 8.1.1
65

76
install:
87
- composer install --dev --no-interaction

modules/openapi-generator/src/main/resources/php-symfony/ApiPass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* {{bundleName}}ApiPass
44
*
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{invokerPackage}}\DependencyInjection\Compiler

modules/openapi-generator/src/main/resources/php-symfony/ApiServer.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* ApiServer
44
*
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{apiPackage}}
@@ -22,7 +22,7 @@ namespace {{apiPackage}};
2222
/**
2323
* ApiServer Class Doc Comment
2424
*
25-
* PHP version 5
25+
* PHP version 8.1.1
2626
*
2727
* @category Class
2828
* @package {{apiPackage}}

modules/openapi-generator/src/main/resources/php-symfony/Bundle.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* {{bundleClassName}}
44
*
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{invokerPackage}}

modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Controller
44
*
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{controllerPackage}}

modules/openapi-generator/src/main/resources/php-symfony/Extension.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* {{bundleExtensionName}}
44
*
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{invokerPackage}}\DependencyInjection
@@ -40,7 +40,7 @@ class {{bundleExtensionName}} extends Extension
4040
$loader->load('services.yml');
4141
}
4242

43-
public function getAlias()
43+
public function getAlias(): string
4444
{
4545
return '{{bundleAlias}}';
4646
}

modules/openapi-generator/src/main/resources/php-symfony/README.mustache

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
1919

2020
## Requirements
2121

22-
PHP 7.1.3 and later
22+
PHP 8.0 and later
2323

2424
## Installation & Usage
2525

@@ -56,26 +56,20 @@ composer install
5656

5757
Step 1: Please follow the [installation procedure](#installation--usage) first.
5858

59-
Step 2: Enable the bundle in the kernel:
59+
Step 2: Enable the bundle in the bundle configuration:
6060

6161
```php
62-
<?php
63-
// app/AppKernel.php
64-
65-
public function registerBundles()
66-
{
67-
$bundles = array(
68-
// ...
69-
new {{invokerPackage}}\{{bundleClassName}}(),
70-
// ...
71-
);
72-
}
62+
// app/config/bundles.php
63+
return [
64+
// ...
65+
{{invokerPackage}}\{{bundleClassName}}::class => ['all' => true],
66+
];
7367
```
7468

7569
Step 3: Register the routes:
7670

7771
```yaml
78-
# app/config/routing.yml
72+
# app/config/routes.yaml
7973
{{bundleAlias}}:
8074
resource: "@{{bundleName}}Bundle/Resources/config/routing.yml"
8175
```
@@ -112,7 +106,7 @@ class {{baseName}}Api implements {{classname}} // An interface is autogenerated
112106
/**
113107
* Implementation of {{classname}}#{{operationId}}
114108
*/
115-
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
109+
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}, &$responseCode, array &$responseHeaders): {{#isArray}}iterable{{/isArray}}{{^isArray}}array|{{{vendorExtensions.x-comment-type}}}{{/isArray}}
116110
{
117111
// Implement the operation ...
118112
}
@@ -125,11 +119,10 @@ class {{baseName}}Api implements {{classname}} // An interface is autogenerated
125119
Step 5: Tag your API implementation:
126120

127121
```yaml
128-
# src/Acme/MyBundle/Resources/services.yml
122+
# config/services.yml
129123
services:
130124
# ...
131-
acme.my_bundle.api.{{pathPrefix}}:
132-
class: Acme\MyBundle\Api\{{baseName}}Api
125+
Acme\MyBundle\Api\{{baseName}}Api:
133126
tags:
134127
- { name: "{{bundleAlias}}.api", api: "{{pathPrefix}}" }
135128
# ...

modules/openapi-generator/src/main/resources/php-symfony/api.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
/**
33
* {{classname}}
4-
* PHP version 7.1.3
4+
*
5+
* PHP version 8.1.1
56
*
67
* @category Class
78
* @package {{invokerPackage}}
@@ -59,15 +60,15 @@ interface {{classname}}
5960
{{#allParams}}
6061
* @param {{vendorExtensions.x-comment-type}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#isDeprecated}} (deprecated){{/isDeprecated}}
6162
{{/allParams}}
62-
* @param \int $responseCode The HTTP response code to return
6363
* @param \array $responseHeaders Additional HTTP headers to return with the response ()
6464
*
6565
* @return {{{vendorExtensions.x-comment-type}}}
6666
{{#isDeprecated}}
6767
* @deprecated
6868
{{/isDeprecated}}
6969
*/
70-
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}&$responseCode, array &$responseHeaders);
70+
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}&$responseCode, array &$responseHeaders): {{{vendorExtensions.x-return-type}}};
71+
7172
{{/operation}}
7273
}
7374
{{/operations}}

modules/openapi-generator/src/main/resources/php-symfony/api_controller.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{{#operations}}/**
44
* {{controllerName}}
5-
* PHP version 7.1.3
5+
* PHP version 8.1.1
66
*
77
* @category Class
88
* @package {{controllerPackage}}

modules/openapi-generator/src/main/resources/php-symfony/api_doc.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ Method | HTTP request | Description
1111
{{#operations}}
1212
## Service Declaration
1313
```yaml
14-
# src/Acme/MyBundle/Resources/services.yml
14+
# config/services.yml
1515
services:
1616
# ...
17-
acme.my_bundle.api.{{pathPrefix}}:
18-
class: Acme\MyBundle\Api\{{baseName}}Api
17+
Acme\MyBundle\Api\{{baseName}}Api:
1918
tags:
2019
- { name: "{{bundleAlias}}.api", api: "{{pathPrefix}}" }
2120
# ...
@@ -62,7 +61,7 @@ class {{baseName}}Api implements {{classname}}
6261
/**
6362
* Implementation of {{classname}}#{{operationId}}
6463
*/
65-
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
64+
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}, &$responseCode, array &$responseHeaders): {{{vendorExtensions.x-return-type}}}
6665
{
6766
// Implement the operation ...
6867
}

modules/openapi-generator/src/main/resources/php-symfony/composer.mustache

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^7.1.3",
22+
"php": ">=7.4.0|>=8.0.2",
2323
"ext-curl": "*",
2424
"ext-json": "*",
2525
"ext-mbstring": "*",
26-
"symfony/validator": "*",
27-
"jms/serializer-bundle": "^2.0",
28-
"symfony/framework-bundle": "^4.4.8"
26+
"symfony/validator": "^5.0|^6.0",
27+
"jms/serializer-bundle": "^4.0",
28+
"symfony/framework-bundle": "^5.0|^6.0"
2929
},
3030
"require-dev": {
31-
"phpunit/phpunit": "^7.0",
31+
"phpunit/phpunit": "^9.5",
3232
"friendsofphp/php-cs-fixer": "^2.16.3",
33-
"symfony/browser-kit": "*",
34-
"symfony/yaml": "^4.4.8",
33+
"symfony/browser-kit": "^5.0|^6.0",
34+
"symfony/yaml": "^5.0|^6.0",
3535
"hoa/regex": "~1.0"
3636
},
3737
"autoload": {
38-
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
38+
"psr-4": {
39+
"{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/"
40+
}
3941
}
4042
}

modules/openapi-generator/src/main/resources/php-symfony/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* {{classname}}
66
*
7-
* PHP version 7.1.3
7+
* PHP version 8.1.1
88
*
99
* @category Class
1010
* @package {{modelPackage}}

modules/openapi-generator/src/main/resources/php-symfony/routing.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
path: {{path}}
1212
methods: [{{httpMethod}}]
1313
defaults:
14-
_controller: {{bundleAlias}}.controller.{{pathPrefix}}:{{operationId}}Action
14+
_controller: {{bundleAlias}}.controller.{{pathPrefix}}::{{operationId}}Action
1515
{{#hasPathParams}}
1616
requirements:
1717
{{/hasPathParams}}

modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ namespace {{servicePackage}};
55
use JMS\Serializer\SerializerBuilder;
66
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
77
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
8-
use JMS\Serializer\XmlDeserializationVisitor;
8+
use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory;
9+
use DateTime;
10+
use RuntimeException;
911

1012
class JmsSerializer implements SerializerInterface
1113
{
1214
protected $serializer;
1315
1416
public function __construct()
1517
{
16-
$naming_strategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
18+
$namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
1719
$this->serializer = SerializerBuilder::create()
18-
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitor($naming_strategy))
19-
->setDeserializationVisitor('xml', new XmlDeserializationVisitor($naming_strategy))
20+
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitorFactory())
21+
->setDeserializationVisitor('xml', new XmlDeserializationVisitorFactory())
22+
->setPropertyNamingStrategy($namingStrategy)
2023
->build();
2124
}
2225

@@ -79,6 +82,11 @@ class JmsSerializer implements SerializerInterface
7982
}
8083

8184
break;
85+
case 'DateTime':
86+
case '\DateTime':
87+
return new DateTime($data);
88+
default:
89+
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
8290
}
8391

8492
// If we end up here, just return data

0 commit comments

Comments
 (0)