Skip to content

Commit eebd489

Browse files
committed
Added new option methodNamingConvention to abstract php class
1 parent b5b760a commit eebd489

File tree

15 files changed

+60
-2
lines changed

15 files changed

+60
-2
lines changed

docs/generators/php-dt.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3232
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3333
|licenseName|The name of the license| |null|
34+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3435
|modelPackage|package for generated models| |null|
3536
|modern|use modern language features (generated code will require PHP 8.1)| |false|
3637
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|

docs/generators/php-flight.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3232
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3333
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3434
|licenseName|The name of the license| |null|
35+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3536
|modelPackage|package for generated models| |null|
3637
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3738
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/php-laravel.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3232
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3333
|licenseName|The name of the license| |null|
34+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3435
|modelPackage|package for generated models| |null|
3536
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3637
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/php-lumen.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3232
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3333
|licenseName|The name of the license| |null|
34+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3435
|modelPackage|package for generated models| |null|
3536
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3637
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/php-mezzio-ph.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3232
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3333
|licenseName|The name of the license| |null|
34+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3435
|modelPackage|package for generated models| |null|
3536
|modern|use modern language features (generated code will require PHP 8.0)| |false|
3637
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|

docs/generators/php-nextgen.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3232
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3333
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3434
|licenseName|The name of the license| |null|
35+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3536
|modelPackage|package for generated models| |null|
3637
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3738
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/php-slim4.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3232
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3333
|licenseName|The name of the license| |null|
34+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3435
|modelPackage|package for generated models| |null|
3536
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3637
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/php-symfony.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3636
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3737
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3838
|licenseName|The name of the license| |null|
39+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3940
|modelPackage|package for generated models| |null|
4041
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
4142
|phpLegacySupport|Should the generated code be compatible with PHP 5.x?| |true|

docs/generators/php.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3333
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3434
|library|HTTP library template (sub-template) to use|<dl><dt>**guzzle**</dt><dd>Guzzle</dd><dt>**psr-18**</dt><dd>psr/http-client-implementation, also known as PSR-18. (beta support)</dd></dl>|guzzle|
3535
|licenseName|The name of the license| |null|
36+
|methodNamingConvention|naming convention of class method name, e.g. PascalCase.| |PascalCase|
3637
|modelPackage|package for generated models| |null|
3738
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|
3839
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

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

+42-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
4747
private final Logger LOGGER = LoggerFactory.getLogger(AbstractPhpCodegen.class);
4848

4949
public static final String VARIABLE_NAMING_CONVENTION = "variableNamingConvention";
50+
public static final String METHOD_NAMING_CONVENTION = "methodNamingConvention";
5051
public static final String PACKAGE_NAME = "packageName";
5152
public static final String SRC_BASE_PATH = "srcBasePath";
5253
@Getter @Setter
@@ -64,6 +65,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
6465
protected String apiDirName = "Api";
6566
protected String modelDirName = "Model";
6667
protected String variableNamingConvention = "snake_case";
68+
@Getter @Setter
69+
protected String methodNamingConvention = "PascalCase";
6770
protected String apiDocPath = docsBasePath + "/" + apiDirName;
6871
protected String modelDocPath = docsBasePath + "/" + modelDirName;
6972
protected String interfaceNamePrefix = "", interfaceNameSuffix = "Interface";
@@ -150,6 +153,8 @@ public AbstractPhpCodegen() {
150153
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
151154
cliOptions.add(new CliOption(VARIABLE_NAMING_CONVENTION, "naming convention of variable name, e.g. camelCase.")
152155
.defaultValue("snake_case"));
156+
cliOptions.add(new CliOption(METHOD_NAMING_CONVENTION, "naming convention of class method name, e.g. PascalCase.")
157+
.defaultValue("PascalCase"));
153158
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, "The main namespace to use for all classes. e.g. Yay\\Pets"));
154159
cliOptions.add(new CliOption(PACKAGE_NAME, "The main package name for classes. e.g. GeneratedPetstore"));
155160
cliOptions.add(new CliOption(SRC_BASE_PATH, "The directory to serve as source root."));
@@ -245,6 +250,10 @@ public void processOpts() {
245250
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
246251
}
247252

253+
if (additionalProperties.containsKey(METHOD_NAMING_CONVENTION)) {
254+
this.setMethodNamingConvention((String) additionalProperties.get(METHOD_NAMING_CONVENTION));
255+
}
256+
248257
if (additionalProperties.containsKey(CodegenConstants.GIT_USER_ID)) {
249258
this.setGitUserId((String) additionalProperties.get(CodegenConstants.GIT_USER_ID));
250259
}
@@ -427,8 +436,11 @@ public void setParameterNamingConvention(String variableNamingConvention) {
427436
this.variableNamingConvention = variableNamingConvention;
428437
}
429438

430-
@Override
431-
public String toVarName(String name) {
439+
/**
440+
* Abstracted method to get non convention var name because convention is handled
441+
* different for toVarName and getter and setter
442+
*/
443+
private String toNonConventionVarName(String name) {
432444
// obtain the name from nameMapping directly if provided
433445
if (nameMapping.containsKey(name)) {
434446
return nameMapping.get(name);
@@ -441,6 +453,13 @@ public String toVarName(String name) {
441453
// sanitize name
442454
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
443455

456+
return name;
457+
}
458+
459+
@Override
460+
public String toVarName(String name) {
461+
name = toNonConventionVarName(name);
462+
444463
if ("camelCase".equals(variableNamingConvention)) {
445464
// return the name in camelCase style
446465
// phone_number => phoneNumber
@@ -462,6 +481,27 @@ public String toVarName(String name) {
462481
return name;
463482
}
464483

484+
@Override
485+
public String getterAndSetterCapitalize(String name) {
486+
// Always underscore before camelCase or PascalCase the getter/setter method because else it will
487+
// result in wrong format in combination with variableNamingConvention=camelCase
488+
name = underscore(toNonConventionVarName(name));
489+
490+
if ("snake_case".equals(methodNamingConvention)) {
491+
// return the name in underscore style and prepend with _ to get method separation (e.g. get_phone_number)
492+
// PhoneNumber => phone_number
493+
return "_" + underscore(name);
494+
} else if ("camelCase".equals(methodNamingConvention)) {
495+
// return the name in camelCase style
496+
// phone_number => phoneNumber
497+
return camelize(name, LOWERCASE_FIRST_LETTER);
498+
} else {
499+
// return the name in PascalCase style
500+
// phone_number => PhoneNumber
501+
return camelize(name);
502+
}
503+
}
504+
465505
@Override
466506
public String toParamName(String name) {
467507
// obtain the name from parameterNameMapping directly if provided

modules/openapi-generator/src/test/java/org/openapitools/codegen/lumen/PhpLumenServerOptionsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected CodegenConfig getCodegenConfig() {
4242
protected void verifyOptions() {
4343
verify(clientCodegen).setSortParamsByRequiredFlag(Boolean.valueOf(PhpLumenServerOptionsProvider.SORT_PARAMS_VALUE));
4444
verify(clientCodegen).setParameterNamingConvention(PhpLumenServerOptionsProvider.VARIABLE_NAMING_CONVENTION_VALUE);
45+
verify(clientCodegen).setMethodNamingConvention(PhpLumenServerOptionsProvider.METHOD_NAMING_CONVENTION_VALUE);
4546
verify(clientCodegen).setModelPackage(PhpLumenServerOptionsProvider.MODEL_PACKAGE_VALUE);
4647
verify(clientCodegen).setApiPackage(PhpLumenServerOptionsProvider.API_PACKAGE_VALUE);
4748
verify(clientCodegen).setInvokerPackage(PhpLumenServerOptionsProvider.INVOKER_PACKAGE_VALUE);

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpClientOptionsProvider.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PhpClientOptionsProvider implements OptionsProvider {
3030
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
3131
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
3232
public static final String VARIABLE_NAMING_CONVENTION_VALUE = "snake_case";
33+
public static final String METHOD_NAMING_CONVENTION_VALUE = "PascalCase";
3334
public static final String INVOKER_PACKAGE_VALUE = "OpenAPITools\\Client\\Php";
3435
public static final String PACKAGE_NAME_VALUE = "OpenAPIToolsClient-php";
3536
public static final String SRC_BASE_PATH_VALUE = "libPhp";
@@ -58,6 +59,7 @@ public Map<String, String> createOptions() {
5859
.put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, SORT_MODEL_PROPERTIES_VALUE)
5960
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
6061
.put(PhpClientCodegen.VARIABLE_NAMING_CONVENTION, VARIABLE_NAMING_CONVENTION_VALUE)
62+
.put(PhpClientCodegen.METHOD_NAMING_CONVENTION, METHOD_NAMING_CONVENTION_VALUE)
6163
.put(CodegenConstants.INVOKER_PACKAGE, INVOKER_PACKAGE_VALUE)
6264
.put(PhpClientCodegen.PACKAGE_NAME, PACKAGE_NAME_VALUE)
6365
.put(PhpClientCodegen.SRC_BASE_PATH, SRC_BASE_PATH_VALUE)

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpLumenServerOptionsProvider.java

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import org.openapitools.codegen.CodegenConstants;
2222
import org.openapitools.codegen.languages.AbstractPhpCodegen;
23+
import org.openapitools.codegen.languages.PhpClientCodegen;
2324

2425
import java.util.Map;
2526

@@ -30,6 +31,7 @@ public class PhpLumenServerOptionsProvider implements OptionsProvider {
3031
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
3132
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
3233
public static final String VARIABLE_NAMING_CONVENTION_VALUE = "snake_case";
34+
public static final String METHOD_NAMING_CONVENTION_VALUE = "PascalCase";
3335
public static final String INVOKER_PACKAGE_VALUE = "lumen";
3436
public static final String PACKAGE_NAME_VALUE = "php";
3537
public static final String SRC_BASE_PATH_VALUE = "libPhp";
@@ -53,6 +55,7 @@ public Map<String, String> createOptions() {
5355
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
5456
return builder.put(CodegenConstants.MODEL_PACKAGE, MODEL_PACKAGE_VALUE)
5557
.put(AbstractPhpCodegen.VARIABLE_NAMING_CONVENTION, VARIABLE_NAMING_CONVENTION_VALUE)
58+
.put(AbstractPhpCodegen.METHOD_NAMING_CONVENTION, METHOD_NAMING_CONVENTION_VALUE)
5659
.put(AbstractPhpCodegen.PACKAGE_NAME, PACKAGE_NAME_VALUE)
5760
.put(AbstractPhpCodegen.SRC_BASE_PATH, SRC_BASE_PATH_VALUE)
5861
.put(CodegenConstants.API_PACKAGE, API_PACKAGE_VALUE)

0 commit comments

Comments
 (0)