Skip to content

[php-nextgen] Discriminator class detection uses wrong namespace#23287

Open
wing328 wants to merge 3 commits intomasterfrom
lukascernydis-master
Open

[php-nextgen] Discriminator class detection uses wrong namespace#23287
wing328 wants to merge 3 commits intomasterfrom
lukascernydis-master

Conversation

@wing328
Copy link
Member

@wing328 wing328 commented Mar 19, 2026

based on #22811 with resolved merge conflicts

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.
  • 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)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • 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 discriminator subclass detection in php-nextgen by using the configured model namespace during deserialization to prevent wrong-class instantiation. Adds tests and sample models to cover discriminator behavior.

  • Bug Fixes
    • Use modelPackage namespace for discriminator subclass lookup in ObjectSerializer.
    • Added testDiscriminatorUsesModelPackageNamespace to enforce correct namespace (\MyApp\Entities\ vs \MyApp\Model\).
    • Updated test spec and regenerated php-nextgen samples with DiscriminatorBase and DiscriminatorChild models.

Written for commit 3f42e23. Summary will update on new commits.

@wing328 wing328 changed the title Lukascernydis master [php-nextgen] Discriminator class detection uses wrong namespace Mar 19, 2026
@wing328 wing328 marked this pull request as ready for review March 19, 2026 13:10
Copy link
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.

4 issues found across 11 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/main/resources/php-nextgen/ObjectSerializer.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache:502">
P2: Resolve discriminator values through the generated mapping before building the subclass name. Custom discriminator mappings will otherwise deserialize to the base model instead of the mapped subtype.</violation>
</file>

<file name="samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorBase.php">

<violation number="1" location="samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorBase.php:249">
P1: Passing `null` as the fallback here clears the discriminator default, so a freshly constructed `DiscriminatorBase` is invalid and `getType(): string` can return `null`.</violation>
</file>

<file name="samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorChild.php">

<violation number="1" location="samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorChild.php:28">
P1: Add the missing imports for `InvalidArgumentException`, `ReturnTypeWillChange`, and `ObjectSerializer`; this subclass currently resolves them into the model namespace and will break on serialization, attributes, or the null-check exception path.</violation>
</file>

<file name="samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/DiscriminatorChild.md">

<violation number="1" location="samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/DiscriminatorChild.md:1">
P3: Use a single Markdown heading marker for the model title.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

// Initialize discriminator property with the model name.
$this->container['type'] = static::$openAPIModelName;

$this->setIfExists('type', $data ?? [], null);
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 19, 2026

Choose a reason for hiding this comment

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

P1: Passing null as the fallback here clears the discriminator default, so a freshly constructed DiscriminatorBase is invalid and getType(): string can return null.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorBase.php, line 249:

<comment>Passing `null` as the fallback here clears the discriminator default, so a freshly constructed `DiscriminatorBase` is invalid and `getType(): string` can return `null`.</comment>

<file context>
@@ -0,0 +1,414 @@
+        // Initialize discriminator property with the model name.
+        $this->container['type'] = static::$openAPIModelName;
+
+        $this->setIfExists('type', $data ?? [], null);
+    }
+
</file context>
Fix with Cubic

Copy link
Contributor

Choose a reason for hiding this comment

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

Discriminator type should be logically always required, but nothing in OpenApi documentation states it explicitly.

Currently there is no hard check in generated code, it is up to developer to design the API correctly.

* Do not edit the class manually.
*/

namespace OpenAPI\Client\Model;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 19, 2026

Choose a reason for hiding this comment

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

P1: Add the missing imports for InvalidArgumentException, ReturnTypeWillChange, and ObjectSerializer; this subclass currently resolves them into the model namespace and will break on serialization, attributes, or the null-check exception path.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/DiscriminatorChild.php, line 28:

<comment>Add the missing imports for `InvalidArgumentException`, `ReturnTypeWillChange`, and `ObjectSerializer`; this subclass currently resolves them into the model namespace and will break on serialization, attributes, or the null-check exception path.</comment>

<file context>
@@ -0,0 +1,397 @@
+ * Do not edit the class manually.
+ */
+
+namespace OpenAPI\Client\Model;
+
+/**
</file context>
Suggested change
namespace OpenAPI\Client\Model;
namespace OpenAPI\Client\Model;
use InvalidArgumentException;
use ReturnTypeWillChange;
use OpenAPI\Client\ObjectSerializer;
Fix with Cubic

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bug and should be fixed, but it is not tied to current problem fixed.

$discriminator = $class::DISCRIMINATOR;
if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
$subclass = '\{{invokerPackage}}\Model\\' . $data->{$discriminator};
$subclass = '\{{modelPackage}}\\' . $data->{$discriminator};
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 19, 2026

Choose a reason for hiding this comment

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

P2: Resolve discriminator values through the generated mapping before building the subclass name. Custom discriminator mappings will otherwise deserialize to the base model instead of the mapped subtype.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache, line 502:

<comment>Resolve discriminator values through the generated mapping before building the subclass name. Custom discriminator mappings will otherwise deserialize to the base model instead of the mapped subtype.</comment>

<file context>
@@ -499,7 +499,7 @@ class ObjectSerializer
             $discriminator = $class::DISCRIMINATOR;
             if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
-                $subclass = '\{{invokerPackage}}\Model\\' . $data->{$discriminator};
+                $subclass = '\{{modelPackage}}\\' . $data->{$discriminator};
                 if (is_subclass_of($subclass, $class)) {
                     $class = $subclass;
</file context>
Fix with Cubic

Copy link
Contributor

Choose a reason for hiding this comment

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

In this scenario (where discriminator is limited to existing class), the use case is valid.

@@ -0,0 +1,9 @@
# # DiscriminatorChild
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 19, 2026

Choose a reason for hiding this comment

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

P3: Use a single Markdown heading marker for the model title.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/DiscriminatorChild.md, line 1:

<comment>Use a single Markdown heading marker for the model title.</comment>

<file context>
@@ -0,0 +1,9 @@
+# # DiscriminatorChild
+
+## Properties
</file context>
Suggested change
# # DiscriminatorChild
# DiscriminatorChild
Fix with Cubic

Copy link
Contributor

Choose a reason for hiding this comment

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

the line in template modules/openapi-generator/src/main/resources/php-nextgen/model_doc.mustache

is currently written as:

# {{#models}}{{#model}}# {{classname}}

should be only

# {{classname}}

@wing328
Copy link
Member Author

wing328 commented Mar 19, 2026

@lukascernydis can you please review the feedback by cubic-dev-ai?

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.

2 participants