fix(php): honor OpenAPI default beside $ref in toDefaultValue#23541
Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom Apr 15, 2026
Merged
fix(php): honor OpenAPI default beside $ref in toDefaultValue#23541wing328 merged 2 commits intoOpenAPITools:masterfrom
wing328 merged 2 commits intoOpenAPITools:masterfrom
Conversation
Parameter/model schemas that combine $ref with a sibling default were not handled by the boolean/number/string branches, so CodegenParameter.defaultValue stayed empty and php-symfony emitted query->get without the second argument. Add a fallback: when getDefault() is set, emit a PHP literal (quoted strings via escapeTextInSingleQuotes, other types via toString()). Update PhpSymfonyServerCodegenTest: assert optional enum-ref query defaults, and relax petstore interface assertions when a default is present (non-nullable signature per api.mustache).
Contributor
There was a problem hiding this comment.
2 issues found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpSymfonyServerCodegenTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpSymfonyServerCodegenTest.java:222">
P3: The interface test was weakened to accept nullable output, so it no longer verifies that a defaulted enum ref is emitted non-nullably.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java:647">
P2: `toDefaultValue` now falls back to `def.toString()` for non-string defaults, which can emit non-PHP literals and break generated PHP code.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
…-symfony tests AbstractPhpCodegen maps getDefault() to valid PHP via defaultValueToPhpLiteral instead of blind toString(). PhpSymfonyServerCodegenTest asserts non-nullable handler params when an enum $ref has an OpenAPI default (petstore + optional spec).
Member
|
thanks for the PR test results look good diff --git a/Api/DefaultApiInterface.php b/Api/DefaultApiInterface.php
index f9659b3..d992eea 100644
--- a/Api/DefaultApiInterface.php
+++ b/Api/DefaultApiInterface.php
@@ -46,7 +46,7 @@ interface DefaultApiInterface
/**
* Operation listFeedHints
*
- * @param PetAnnouncementTone|null $tone Optional filter; default applies when the query key is omitted. (optional)
+ * @param PetAnnouncementTone $tone Optional filter; default applies when the query key is omitted. (optional, default to 'friendly')
* @param int $limit Integer optional query with default (often generated correctly). (optional, default to 10)
* @param int &$responseCode The HTTP Response Code
* @param array $responseHeaders Additional HTTP headers to return with the response ()
@@ -54,7 +54,7 @@ interface DefaultApiInterface
* @return void
*/
public function listFeedHints(
- ?PetAnnouncementTone $tone,
+ PetAnnouncementTone $tone,
int $limit,
int &$responseCode,
array &$responseHeaders
diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php
index 77ab39e..47ffd8a 100644
--- a/Controller/DefaultController.php
+++ b/Controller/DefaultController.php
@@ -60,7 +60,7 @@ class DefaultController extends Controller
// Handle authentication
// Read out all input parameter values into variables
- $tone = $request->query->get('tone');
+ $tone = $request->query->get('tone', 'friendly');
$limit = $request->query->get('limit', 10);
|
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
Optional
php-symfonyquery parameters whose schema is an enum$refwith a siblingdefault(common undercomponents.parameters) used to get an emptyCodegenParameter.defaultValue.api_controller.mustachethen emitted$request->query->get('name')without the default argument, so the value stayednullandAssert\Type(<enum>)failed before the handler ran.Root cause
AbstractPhpCodegen.toDefaultValue(Schema)only handled defaults whenModelUtilsclassified the schema as boolean / number / integer / string. Schemas that are effectively “$ref+defaulton the same object” often do not match those branches, so the method returnednulleven whenschema.getDefault()was set.Fix
After the existing type-specific branches, add a small fallback: if
getDefault() != null, emit a PHP literal—Stringvalues as single-quoted strings viaescapeTextInSingleQuotes, other types viatoString(). That fillsCodegenParameter.defaultValue, so existing templates emitquery->get('<baseName>', <default>)(and related Elvis paths where applicable) without changing Mustache.Scope: all generators extending
AbstractPhpCodegen(client, php-symfony, Slim4, Flight, etc.).Tests
PhpSymfonyServerCodegenTesttestOptionalEnumRefQueryParameterWithDefaultAppliesOpenApiSemantics: generates fromsrc/test/resources/3_1/php-symfony/optional-enum-query-ref-default.yamland asserts the controller containsquery->get('tone', …)(or equivalent) alongside the existinglimitcontrol case.testPetstoreDottedEnumRefQueryParameterUsesShortClassInApiInterface: assertions updated so that when a default is present, the interface may use a non-nullablePetModelPetStatus $status(consistent withapi.mustachewhendefaultValueis set).AbstractPhpCodegenTest,PhpClientCodegenTest,PhpModelTest,PhpNextgenClientCodegenTest,PhpFlightServerCodegenTest,PhpSlim4ServerCodegenTest,PhpClientExampleTest,PhpClientOptionsTest,PhpLumenServerOptionsTest.Issue link
PR checklist
./mvnw clean package -DskipTests ./bin/generate-samples.sh bin/configs/php-*.yaml ./bin/utils/export_docs_generators.sh./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.shmasterSummary by cubic
Honor OpenAPI defaults when a schema uses
$refwith a siblingdefault, so optional enum query params inphp-symfonyreceive the default and pass validation. Uses safe PHP literals for defaults across generators extendingAbstractPhpCodegen.AbstractPhpCodegen.toDefaultValue, whenschema.getDefault()is set (even with$ref), map it viadefaultValueToPhpLiteralto a safe PHP scalar (strings escaped; booleans/numbers handled; unsupported values omitted) and setCodegenParameter.defaultValue.php-symfonynow passes defaults to$request->query->get()and uses non-nullable interface type hints/PHPDoc when a default exists, preventing early enumAssert\Typefailures.optional-enum-query-ref-default.yamland tightenedPhpSymfonyServerCodegenTestto assert non-nullable types and that the controller applies the default.Written for commit 1fa1b8c. Summary will update on new commits.