[PHP][php-symfony] Fix native enum default value expression in AbstractPhpCodegen#23556
Open
JerrySLau wants to merge 2 commits intoOpenAPITools:masterfrom
Open
[PHP][php-symfony] Fix native enum default value expression in AbstractPhpCodegen#23556JerrySLau wants to merge 2 commits intoOpenAPITools:masterfrom
JerrySLau wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
AbstractPhpCodegen.toEnumDefaultValue used self::FQCN_CASE, which is invalid for PHP 8.1 enums when model properties use enum $ref with a sibling default. Emit datatype::caseName instead and document the upstream data flow. Extend optional-enum-query-ref-default.yaml with a pet-store CreatePetRequest example; add PhpSymfonyServerCodegenTest regression and refresh the issue draft.
Strip modelPackage from AbstractPhpCodegen.toEnumDefaultValue so model property defaults resolve in-file like type hints; keep native enum syntax.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AbstractPhpCodegen.toEnumDefaultValuehistorically returnedself::{datatype}_{case}, which matches old class-constant style enums (#10273) but is invalid for PHP 8.1 backed enums whendatatypeis a namespaced class name. After #23541, OpenAPIdefaultbeside an enum$refon model properties flows throughupdateCodegenPropertyEnum, so the wrong expression is emitted intomodel_variables.mustache(#16605) and breaksphp -l/ static analysis.This change emits
{datatype}::{case}(native enum case reference), documents the data flow in Javadoc, extends the existing php-symfony fixtureoptional-enum-query-ref-default.yamlwith a pet-storePOST /pets+Pet.HTTP.CreatePetRequestexample, and addsPhpSymfonyServerCodegenTest#testModelPropertyEnumRefWithDefaultUsesNativeEnumCaseInCodegento lock the behavior.Branch vs
master(this workspace): 1 commit touchingAbstractPhpCodegen.java,PhpSymfonyServerCodegenTest.java,optional-enum-query-ref-default.yaml.How to validate
./mvnw -pl modules/openapi-generator '-Dtest=org.openapitools.codegen.php.**' testphp-symfonyfrommodules/openapi-generator/src/test/resources/3_1/php-symfony/optional-enum-query-ref-default.yamland confirmPetHTTPCreatePetRequest(or equivalent) uses…::AVAILABLE(or FQCN::CASE) for the defaulted enum property, notself::…_AVAILABLE.testOptionalEnumRefQueryParameterWithDefaultAppliesOpenApiSemanticson the same YAML still passes (optional enum query + default).Related
fix_php_enum_ref_default_backed_enum.md.toDefaultValue+$ref+default), Fix php enum default value initialization #10273 (toEnumDefaultValueshape), [php-symfony] Support for default scalar value of properties in model #16605 (php-symfony property initializer usesdefaultValue).fixes #NNNNhere.cc PHP committee: @jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon
cc PHP Symfony: @ksm2 @BenjaminHae
PR checklist
Read the contribution guidelines.
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Done (local workspace,
JAVA_HOME=$(mise where java@11)): All three commands completed successfully (generate-samplesreported 742 generators finished). The tree had no extra diffs afterward—commit any sample/doc changes if your branch differs after rebasing on latestmaster.File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) — intended target:master.If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) — add when issue exists.If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Fixes generation of PHP 8.1 backed-enum defaults in
php-symfony. We now emitType::CASE(using the short class name undermodelPackage) instead ofself::FQCN_CASE, so enum defaults on$ref-backed model properties compile and lint correctly.AbstractPhpCodegen.toEnumDefaultValueto return{datatype}::{case}for native enums and stripmodelPackageso model defaults use the short class name.updateCodegenPropertyEnum.optional-enum-query-ref-default.yamlwithPet.HTTP.CreatePetRequestusing an enum$ref+default.PhpSymfonyServerCodegenTest#testModelPropertyEnumRefWithDefaultUsesNativeEnumCaseInCodegen.Written for commit 6fbc3b7. Summary will update on new commits.