Skip to content

Commit 6fbc3b7

Browse files
committed
fix(php): enum default uses short class + ::CASE under modelPackage
Strip modelPackage from AbstractPhpCodegen.toEnumDefaultValue so model property defaults resolve in-file like type hints; keep native enum syntax.
1 parent f13c25e commit 6fbc3b7

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,16 +790,37 @@ public String toEnumValue(String value, String datatype) {
790790
* <p>
791791
* Execution: {@code datatype} is produced upstream (e.g. {@link DefaultCodegen#updateCodegenPropertyEnum}) via
792792
* {@link #getTypeDeclaration(Schema)} for the referenced enum schema; {@code value} is the sanitized case name
793-
* from {@link #toEnumVarName}. We concatenate with {@code ::} so templates emit valid PHP (e.g.
794-
* {@code \Vendor\Model\Status::AVAILABLE}).
793+
* from {@link #toEnumVarName}. When the enum class sits under {@link #modelPackage}, we emit only the short class
794+
* name plus {@code ::} so it matches sibling model references in generated files ({@code namespace} is
795+
* {@code modelPackage}; unqualified names resolve correctly). A fully qualified body without a leading
796+
* {@code \} would be resolved relative to the file namespace and is invalid PHP for defaults.
795797
*
796798
* @param value enum case name (e.g. {@code AVAILABLE})
797-
* @param datatype enum class as in generated PHP (often leading {@code \} + FQCN)
798-
* @return PHP default expression for that case
799+
* @param datatype enum class as produced by {@link #getTypeDeclaration(Schema)} (may include {@code modelPackage})
800+
* @return PHP default expression for that case (e.g. {@code PetStatus::AVAILABLE})
799801
*/
800802
@Override
801803
public String toEnumDefaultValue(String value, String datatype) {
802-
return datatype + "::" + value;
804+
return unqualifiedEnumClassForModelDefault(datatype) + "::" + value;
805+
}
806+
807+
/**
808+
* Strips {@link #modelPackage} from a declared enum class name so defaults use the same unqualified form as
809+
* property type hints in model templates.
810+
*
811+
* @param datatype enum class string from codegen (optional leading {@code \})
812+
* @return short class name if under {@code modelPackage}, otherwise the original {@code datatype}
813+
*/
814+
private String unqualifiedEnumClassForModelDefault(String datatype) {
815+
if (StringUtils.isBlank(datatype) || StringUtils.isBlank(modelPackage)) {
816+
return datatype;
817+
}
818+
String normalized = datatype.charAt(0) == '\\' ? datatype.substring(1) : datatype;
819+
String prefix = modelPackage + "\\";
820+
if (normalized.startsWith(prefix)) {
821+
return normalized.substring(prefix.length());
822+
}
823+
return datatype;
803824
}
804825

805826
@Override

0 commit comments

Comments
 (0)