Skip to content

Commit fa64c8e

Browse files
authored
Issue: #21334 Abstract PHP support of "original" for variableNamingConvention (#21336)
* feat(php): add support for 'off' variable naming convention in AbstractPhpCodegen * feat(php-laravel): add tests for issue 21334 with API and model definitions * feat(php-laravel): update variableNamingConvention option in php-laravel help file to include 'off' as a valid value * feat: enhance variableNamingConvention options to include 'off' and detailed descriptions in documentation * feat(php): update variableNamingConvention from 'off' to 'original' in AbstractPhpCodegen * feat: include php-laravel-issue-21334 in workflow triggers for push and pull_request events
1 parent fbd94d5 commit fa64c8e

File tree

25 files changed

+423
-9
lines changed

25 files changed

+423
-9
lines changed

.github/workflows/samples-php8.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ on:
66
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
77
- samples/server/petstore/php-flight/**
88
- samples/server/petstore/php-laravel/**
9+
- samples/server/petstore/php-laravel-issue-21334/**
910
pull_request:
1011
paths:
1112
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
1213
- samples/server/petstore/php-flight/**
1314
- samples/server/petstore/php-laravel/**
15+
- samples/server/petstore/php-laravel-issue-21334/**
1416
jobs:
1517
build:
1618
name: Build PHP projects
@@ -28,6 +30,7 @@ jobs:
2830
- samples/server/petstore/php-symfony/SymfonyBundle-php/
2931
- samples/server/petstore/php-flight/
3032
- samples/server/petstore/php-laravel/
33+
- samples/server/petstore/php-laravel-issue-21334/
3134
steps:
3235
- uses: actions/checkout@v4
3336
- name: Setup PHP with tools
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: php-laravel
2+
outputDir: samples/server/petstore/php-laravel-issue-21334
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue21334.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/php-laravel
5+
gitUserId: openapitools
6+
gitRepoId: petstore
7+
additionalProperties:
8+
variableNamingConvention: "original"

docs/generators/php-dt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
41-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
41+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4242

4343
## IMPORT MAPPING
4444

docs/generators/php-flight.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
41-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |camelCase|
41+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|camelCase|
4242

4343
## IMPORT MAPPING
4444

docs/generators/php-laravel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
41-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
41+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4242

4343
## IMPORT MAPPING
4444

docs/generators/php-lumen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3737
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3838
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
3939
|srcBasePath|The directory to serve as source root.| |null|
40-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
40+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4141

4242
## IMPORT MAPPING
4343

docs/generators/php-mezzio-ph.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
41-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
41+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4242

4343
## IMPORT MAPPING
4444

docs/generators/php-nextgen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
4141
|supportStreaming|Support streaming endpoint| |false|
42-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
42+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4343

4444
## IMPORT MAPPING
4545

docs/generators/php-slim4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3939
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4040
|srcBasePath|The directory to serve as source root.| |null|
41-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |camelCase|
41+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|camelCase|
4242

4343
## IMPORT MAPPING
4444

docs/generators/php-symfony.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4343
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
4444
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4545
|srcBasePath|The directory to serve as source root.| |null|
46-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
46+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4747

4848
## IMPORT MAPPING
4949

docs/generators/php.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3939
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
4040
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4141
|srcBasePath|The directory to serve as source root.| |null|
42-
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
42+
|variableNamingConvention|naming convention of variable name, e.g. camelCase.|<dl><dt>**camelCase**</dt><dd>Use camelCase convention</dd><dt>**PascalCase**</dt><dd>Use PascalCase convention</dd><dt>**snake_case**</dt><dd>Use snake_case convention</dd><dt>**original**</dt><dd>Do not change the variable name</dd></dl>|snake_case|
4343

4444
## IMPORT MAPPING
4545

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public AbstractPhpCodegen() {
149149
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
150150
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
151151
cliOptions.add(new CliOption(VARIABLE_NAMING_CONVENTION, "naming convention of variable name, e.g. camelCase.")
152+
.addEnum("camelCase", "Use camelCase convention")
153+
.addEnum("PascalCase", "Use PascalCase convention")
154+
.addEnum("snake_case", "Use snake_case convention")
155+
.addEnum("original", "Do not change the variable name")
152156
.defaultValue("snake_case"));
153157
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, "The main namespace to use for all classes. e.g. Yay\\Pets"));
154158
cliOptions.add(new CliOption(PACKAGE_NAME, "The main package name for classes. e.g. GeneratedPetstore"));
@@ -451,6 +455,8 @@ public String toVarName(String name) {
451455
name = camelize(name, LOWERCASE_FIRST_LETTER);
452456
} else if ("PascalCase".equals(variableNamingConvention)) {
453457
name = camelize(name, UPPERCASE_FIRST_CHAR);
458+
} else if ("original".equals(variableNamingConvention)) {
459+
// return the name as it is
454460
} else { // default to snake case
455461
// return the name in underscore style
456462
// PhoneNumber => phone_number
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Issue 21334 - Abstract PHP Variable Naming Convention
4+
description: ''
5+
version: 1.0.0
6+
paths:
7+
/dummy:
8+
get:
9+
description: ''
10+
operationId: getDummy
11+
parameters:
12+
- name: paramCamelCase
13+
in: query
14+
required: false
15+
schema:
16+
type: string
17+
- name: ParamPascalCase
18+
in: query
19+
required: false
20+
schema:
21+
type: string
22+
- name: param_withRandomCase
23+
in: query
24+
required: false
25+
schema:
26+
type: string
27+
responses:
28+
'200':
29+
description: successful operation
30+
components:
31+
schemas:
32+
SampleModelToTestNamingConvention:
33+
type: object
34+
properties:
35+
propCamelCase:
36+
type: string
37+
default: 'defaultName'
38+
PropPascalCase:
39+
type: string
40+
default: 'DefaultName'
41+
Prop_withRandomCase:
42+
type: string
43+
default: 'defaultName'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.gitignore
2+
Api/DefaultApiInterface.php
3+
Http/Controllers/DefaultController.php
4+
Model/NoContent200.php
5+
Model/SampleModelToTestNamingConvention.php
6+
README.md
7+
composer.json
8+
phpunit.xml.dist
9+
routes.php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.14.0-SNAPSHOT
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* Issue 21334 - Abstract PHP Variable Naming Convention
5+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6+
* PHP version 8.1
7+
*
8+
* The version of the OpenAPI document: 1.0.0
9+
*
10+
*
11+
* NOTE: This class is auto generated by OpenAPI-Generator
12+
* https://openapi-generator.tech
13+
* Do not edit the class manually.
14+
*
15+
* Source files are located at:
16+
*
17+
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
18+
*/
19+
20+
21+
namespace OpenAPI\Server\Api;
22+
23+
24+
interface DefaultApiInterface {
25+
26+
27+
/**
28+
* Operation getDummy
29+
* @param null | string $paramCamelCase
30+
* @param null | string $ParamPascalCase
31+
* @param null | string $param_withRandomCase
32+
* @return \OpenAPI\Server\Model\NoContent200
33+
*/
34+
public function getDummy(
35+
?string $paramCamelCase,
36+
?string $ParamPascalCase,
37+
?string $param_withRandomCase,
38+
):
39+
\OpenAPI\Server\Model\NoContent200
40+
;
41+
42+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* Issue 21334 - Abstract PHP Variable Naming Convention
5+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6+
* PHP version 8.1
7+
*
8+
* The version of the OpenAPI document: 1.0.0
9+
*
10+
*
11+
* NOTE: This class is auto generated by OpenAPI-Generator
12+
* https://openapi-generator.tech
13+
* Do not edit the class manually.
14+
*
15+
* Source files are located at:
16+
*
17+
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
18+
*/
19+
20+
21+
namespace OpenAPI\Server\Http\Controllers;
22+
23+
use Crell\Serde\SerdeCommon;
24+
use Illuminate\Routing\Controller;
25+
use Illuminate\Http\JsonResponse;
26+
use Illuminate\Http\Request;
27+
use Illuminate\Support\Facades\Validator;
28+
29+
30+
use OpenAPI\Server\Api\DefaultApiInterface;
31+
32+
class DefaultController extends Controller
33+
{
34+
/**
35+
* Constructor
36+
*/
37+
public function __construct(
38+
private readonly DefaultApiInterface $api,
39+
private readonly SerdeCommon $serde = new SerdeCommon(),
40+
)
41+
{
42+
}
43+
44+
/**
45+
* Operation getDummy
46+
*
47+
* .
48+
*
49+
*/
50+
public function getDummy(Request $request): JsonResponse
51+
{
52+
$validator = Validator::make(
53+
array_merge(
54+
[
55+
56+
],
57+
$request->all(),
58+
),
59+
[
60+
'paramCamelCase' => [
61+
'string',
62+
],
63+
'ParamPascalCase' => [
64+
'string',
65+
],
66+
'param_withRandomCase' => [
67+
'string',
68+
],
69+
],
70+
);
71+
72+
if ($validator->fails()) {
73+
return response()->json(['error' => 'Invalid input'], 400);
74+
}
75+
76+
$paramCamelCase = $request->string('paramCamelCase')->value();
77+
78+
$ParamPascalCase = $request->string('ParamPascalCase')->value();
79+
80+
$param_withRandomCase = $request->string('param_withRandomCase')->value();
81+
82+
try {
83+
$apiResult = $this->api->getDummy($paramCamelCase, $ParamPascalCase, $param_withRandomCase);
84+
} catch (\Exception $exception) {
85+
// This shouldn't happen
86+
report($exception);
87+
return response()->json(['error' => $exception->getMessage()], 500);
88+
}
89+
90+
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
91+
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
92+
}
93+
94+
95+
// This shouldn't happen
96+
return response()->abort(500);
97+
}
98+
}

0 commit comments

Comments
 (0)