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
This commit is contained in:
Mostafa Aghajani 2025-06-09 14:26:53 +03:00 committed by GitHub
parent fbd94d5fbb
commit fa64c8e012
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 423 additions and 9 deletions

View File

@ -6,11 +6,13 @@ on:
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
- samples/server/petstore/php-flight/**
- samples/server/petstore/php-laravel/**
- samples/server/petstore/php-laravel-issue-21334/**
pull_request:
paths:
- samples/server/petstore/php-symfony/SymfonyBundle-php/**
- samples/server/petstore/php-flight/**
- samples/server/petstore/php-laravel/**
- samples/server/petstore/php-laravel-issue-21334/**
jobs:
build:
name: Build PHP projects
@ -28,6 +30,7 @@ jobs:
- samples/server/petstore/php-symfony/SymfonyBundle-php/
- samples/server/petstore/php-flight/
- samples/server/petstore/php-laravel/
- samples/server/petstore/php-laravel-issue-21334/
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools

View File

@ -0,0 +1,8 @@
generatorName: php-laravel
outputDir: samples/server/petstore/php-laravel-issue-21334
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue21334.yaml
templateDir: modules/openapi-generator/src/main/resources/php-laravel
gitUserId: openapitools
gitRepoId: petstore
additionalProperties:
variableNamingConvention: "original"

View File

@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |camelCase|
|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|
## IMPORT MAPPING

View File

@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -37,7 +37,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -39,7 +39,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|supportStreaming|Support streaming endpoint| |false|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -38,7 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |camelCase|
|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|
## IMPORT MAPPING

View File

@ -43,7 +43,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -39,7 +39,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|srcBasePath|The directory to serve as source root.| |null|
|variableNamingConvention|naming convention of variable name, e.g. camelCase.| |snake_case|
|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|
## IMPORT MAPPING

View File

@ -149,6 +149,10 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
cliOptions.add(new CliOption(VARIABLE_NAMING_CONVENTION, "naming convention of variable name, e.g. camelCase.")
.addEnum("camelCase", "Use camelCase convention")
.addEnum("PascalCase", "Use PascalCase convention")
.addEnum("snake_case", "Use snake_case convention")
.addEnum("original", "Do not change the variable name")
.defaultValue("snake_case"));
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, "The main namespace to use for all classes. e.g. Yay\\Pets"));
cliOptions.add(new CliOption(PACKAGE_NAME, "The main package name for classes. e.g. GeneratedPetstore"));
@ -451,6 +455,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
name = camelize(name, LOWERCASE_FIRST_LETTER);
} else if ("PascalCase".equals(variableNamingConvention)) {
name = camelize(name, UPPERCASE_FIRST_CHAR);
} else if ("original".equals(variableNamingConvention)) {
// return the name as it is
} else { // default to snake case
// return the name in underscore style
// PhoneNumber => phone_number

View File

@ -0,0 +1,43 @@
openapi: 3.0.3
info:
title: Issue 21334 - Abstract PHP Variable Naming Convention
description: ''
version: 1.0.0
paths:
/dummy:
get:
description: ''
operationId: getDummy
parameters:
- name: paramCamelCase
in: query
required: false
schema:
type: string
- name: ParamPascalCase
in: query
required: false
schema:
type: string
- name: param_withRandomCase
in: query
required: false
schema:
type: string
responses:
'200':
description: successful operation
components:
schemas:
SampleModelToTestNamingConvention:
type: object
properties:
propCamelCase:
type: string
default: 'defaultName'
PropPascalCase:
type: string
default: 'DefaultName'
Prop_withRandomCase:
type: string
default: 'defaultName'

View File

@ -0,0 +1 @@
/vendor/

View File

@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@ -0,0 +1,9 @@
.gitignore
Api/DefaultApiInterface.php
Http/Controllers/DefaultController.php
Model/NoContent200.php
Model/SampleModelToTestNamingConvention.php
README.md
composer.json
phpunit.xml.dist
routes.php

View File

@ -0,0 +1 @@
7.14.0-SNAPSHOT

View File

@ -0,0 +1,42 @@
<?php declare(strict_types=1);
/**
* Issue 21334 - Abstract PHP Variable Naming Convention
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
* PHP version 8.1
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator
* https://openapi-generator.tech
* Do not edit the class manually.
*
* Source files are located at:
*
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
*/
namespace OpenAPI\Server\Api;
interface DefaultApiInterface {
/**
* Operation getDummy
* @param null | string $paramCamelCase
* @param null | string $ParamPascalCase
* @param null | string $param_withRandomCase
* @return \OpenAPI\Server\Model\NoContent200
*/
public function getDummy(
?string $paramCamelCase,
?string $ParamPascalCase,
?string $param_withRandomCase,
):
\OpenAPI\Server\Model\NoContent200
;
}

View File

@ -0,0 +1,98 @@
<?php declare(strict_types=1);
/**
* Issue 21334 - Abstract PHP Variable Naming Convention
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
* PHP version 8.1
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator
* https://openapi-generator.tech
* Do not edit the class manually.
*
* Source files are located at:
*
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
*/
namespace OpenAPI\Server\Http\Controllers;
use Crell\Serde\SerdeCommon;
use Illuminate\Routing\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use OpenAPI\Server\Api\DefaultApiInterface;
class DefaultController extends Controller
{
/**
* Constructor
*/
public function __construct(
private readonly DefaultApiInterface $api,
private readonly SerdeCommon $serde = new SerdeCommon(),
)
{
}
/**
* Operation getDummy
*
* .
*
*/
public function getDummy(Request $request): JsonResponse
{
$validator = Validator::make(
array_merge(
[
],
$request->all(),
),
[
'paramCamelCase' => [
'string',
],
'ParamPascalCase' => [
'string',
],
'param_withRandomCase' => [
'string',
],
],
);
if ($validator->fails()) {
return response()->json(['error' => 'Invalid input'], 400);
}
$paramCamelCase = $request->string('paramCamelCase')->value();
$ParamPascalCase = $request->string('ParamPascalCase')->value();
$param_withRandomCase = $request->string('param_withRandomCase')->value();
try {
$apiResult = $this->api->getDummy($paramCamelCase, $ParamPascalCase, $param_withRandomCase);
} catch (\Exception $exception) {
// This shouldn't happen
report($exception);
return response()->json(['error' => $exception->getMessage()], 500);
}
if ($apiResult instanceof \OpenAPI\Server\Model\NoContent200) {
return response()->json($this->serde->serialize($apiResult, format: 'array'), 200);
}
// This shouldn't happen
return response()->abort(500);
}
}

View File

@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/**
* Issue 21334 - Abstract PHP Variable Naming Convention
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
* PHP version 8.1
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator
* https://openapi-generator.tech
* Do not edit the class manually.
*
* Source files are located at:
*
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
*/
/**
* NoContent200
*/
namespace OpenAPI\Server\Model;
/**
* NoContent200
* @description No content for 200
*/
use Crell\Serde\Renaming\Cases;
use Crell\Serde\Attributes as Serde;
#[Serde\ClassSettings(renameWith: Cases::snake_case)]
class NoContent200
{
/**
*
* dummy property for no-content responses
* @param null | string $dummy
*/
public function __construct(
public ?string $dummy = null,
) {}
}

View File

@ -0,0 +1,53 @@
<?php declare(strict_types=1);
/**
* Issue 21334 - Abstract PHP Variable Naming Convention
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
* PHP version 8.1
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator
* https://openapi-generator.tech
* Do not edit the class manually.
*
* Source files are located at:
*
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
*/
/**
* SampleModelToTestNamingConvention
*/
namespace OpenAPI\Server\Model;
/**
* SampleModelToTestNamingConvention
*/
use Crell\Serde\Renaming\Cases;
use Crell\Serde\Attributes as Serde;
#[Serde\ClassSettings(renameWith: Cases::snake_case)]
class SampleModelToTestNamingConvention
{
/**
*
*
* @param string $propCamelCase
*
*
* @param string $PropPascalCase
*
*
* @param string $Prop_withRandomCase
*/
public function __construct(
public string $propCamelCase = 'defaultName',
public string $PropPascalCase = 'DefaultName',
public string $Prop_withRandomCase = 'defaultName',
) {}
}

View File

@ -0,0 +1,31 @@
{
"name": "openapitools/petstore",
"description": "",
"keywords": [
"openapi",
"php",
"sdk",
"api"
],
"homepage": "https://openapi-generator.tech",
"license": "unlicense",
"authors": [
{
"name": "OpenAPI",
"homepage": "https://openapi-generator.tech"
}
],
"require": {
"php": "^8.1",
"laravel/framework": "^10.0",
"crell/serde": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"OpenAPI\\Server\\" : ""
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./Api</directory>
<directory suffix=".php">./Model</directory>
<directory suffix=".php">./Http/Controllers</directory>
<directory suffix=".php">.</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="E_ALL" />
</php>
</phpunit>

View File

@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/**
* Issue 21334 - Abstract PHP Variable Naming Convention
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
* PHP version 8.1
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator
* https://openapi-generator.tech
* Do not edit the class manually.
*
* Source files are located at:
*
* > https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php-laravel/
*/
use Illuminate\Support\Facades\Route;
/**
* GET getDummy
* Summary:
* Notes:
*/
Route::GET('/dummy', [\OpenAPI\Server\Http\Controllers\DefaultController::class, 'getDummy'])->name('default.get.dummy');