Skip to content

[PHP][php-symfony] Fix native enum default value expression in AbstractPhpCodegen#23556

Open
JerrySLau wants to merge 2 commits intoOpenAPITools:masterfrom
JerrySLau:fix_php_enum_ref_default_backed_enum
Open

[PHP][php-symfony] Fix native enum default value expression in AbstractPhpCodegen#23556
JerrySLau wants to merge 2 commits intoOpenAPITools:masterfrom
JerrySLau:fix_php_enum_ref_default_backed_enum

Conversation

@JerrySLau
Copy link
Copy Markdown
Contributor

@JerrySLau JerrySLau commented Apr 15, 2026

Summary

AbstractPhpCodegen.toEnumDefaultValue historically returned self::{datatype}_{case}, which matches old class-constant style enums (#10273) but is invalid for PHP 8.1 backed enums when datatype is a namespaced class name. After #23541, OpenAPI default beside an enum $ref on model properties flows through updateCodegenPropertyEnum, so the wrong expression is emitted into model_variables.mustache (#16605) and breaks php -l / static analysis.

This change emits {datatype}::{case} (native enum case reference), documents the data flow in Javadoc, extends the existing php-symfony fixture optional-enum-query-ref-default.yaml with a pet-store POST /pets + Pet.HTTP.CreatePetRequest example, and adds PhpSymfonyServerCodegenTest#testModelPropertyEnumRefWithDefaultUsesNativeEnumCaseInCodegen to lock the behavior.

Branch vs master (this workspace): 1 commit touching AbstractPhpCodegen.java, PhpSymfonyServerCodegenTest.java, optional-enum-query-ref-default.yaml.

How to validate

  1. Unit / generator tests:
    ./mvnw -pl modules/openapi-generator '-Dtest=org.openapitools.codegen.php.**' test
  2. Manual / CLI: Generate php-symfony from modules/openapi-generator/src/test/resources/3_1/php-symfony/optional-enum-query-ref-default.yaml and confirm PetHTTPCreatePetRequest (or equivalent) uses …::AVAILABLE (or FQCN ::CASE) for the defaulted enum property, not self::…_AVAILABLE.
  3. Regression: Existing testOptionalEnumRefQueryParameterWithDefaultAppliesOpenApiSemantics on the same YAML still passes (optional enum query + default).

Related

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:

    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    

    (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-samples reported 742 generators finished). The tree had no extra diffs afterward—commit any sample/doc changes if your branch differs after rebasing on latest master.

  • File the PR against the correct branch: master (upcoming 7.x.0 minor 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 emit Type::CASE (using the short class name under modelPackage) instead of self::FQCN_CASE, so enum defaults on $ref-backed model properties compile and lint correctly.

  • Bug Fixes
    • Update AbstractPhpCodegen.toEnumDefaultValue to return {datatype}::{case} for native enums and strip modelPackage so model defaults use the short class name.
    • Add Javadoc explaining enum default flow via updateCodegenPropertyEnum.
    • Extend optional-enum-query-ref-default.yaml with Pet.HTTP.CreatePetRequest using an enum $ref + default.
    • Add regression test PhpSymfonyServerCodegenTest#testModelPropertyEnumRefWithDefaultUsesNativeEnumCaseInCodegen.

Written for commit 6fbc3b7. Summary will update on new commits.

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.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Strip modelPackage from AbstractPhpCodegen.toEnumDefaultValue so model
property defaults resolve in-file like type hints; keep native enum syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant