-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[php-nextgen] oneof polymorphism #23985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wing328
merged 10 commits into
OpenAPITools:master
from
coffeemakr:php-nextgen-oneof-polymorphism
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
724c811
[php-nextgen]: Add oneof polymorphism
coffeemakr 5684d5e
[php-nextgen]: Regenerate sample with new models
coffeemakr ed9b1a5
[php-nextgen]: Update generated files file
coffeemakr de53f6e
[php-nextgen]: add polymorphism to docs
coffeemakr 392fa08
[php-nextgen]: Add oneOf properties (currently not working)
coffeemakr 35160fc
[php-nextgen]: Unify doc and signature types for params, properties a…
coffeemakr 20da863
[php-nextgen]: Make api responses nullable only on nullable SUCCESS r…
coffeemakr 49152bc
[php-nextgen]: Try to use correct model for api documentation
coffeemakr 41b0b69
[php-nextgen] Regenerate docs
coffeemakr 918ac70
[php-nextgen] Improve and link javadocs
coffeemakr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -496,6 +496,11 @@ class ObjectSerializer | |
| $data = (object) $data; | ||
| } | ||
|
|
||
| // A oneOf schema is not a value object: resolve the data to one of its member types. | ||
| if (is_subclass_of($class, '\{{modelPackage}}\OneOfInterface')) { | ||
| return self::deserializeOneOf($data, $class, $httpHeaders); | ||
| } | ||
|
|
||
| // If a discriminator is defined and points to a valid subclass, use it. | ||
| $discriminator = $class::DISCRIMINATOR; | ||
| if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) { | ||
|
|
@@ -531,6 +536,49 @@ class ObjectSerializer | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deserialize data into one of the member types of a `oneOf` schema. | ||
| * | ||
| * When the schema declares a discriminator, its value selects the member type directly. | ||
| * Otherwise each member type is tried in turn and the first one that yields a valid model | ||
| * (or a non-null primitive) is returned. | ||
| * | ||
| * @param mixed $data the data already decoded to an object | ||
| * @param string $class a class name implementing OneOfInterface | ||
| * @param string[]|null $httpHeaders HTTP headers | ||
| * | ||
| * @return mixed an instance of one of the `oneOf` member types | ||
| */ | ||
| private static function deserializeOneOf(mixed $data, string $class, ?array $httpHeaders): mixed | ||
| { | ||
| $discriminator = $class::getOneOfDiscriminator(); | ||
| if ($discriminator !== null && isset($data->{$discriminator}) && is_string($data->{$discriminator})) { | ||
| $mappings = $class::getOneOfDiscriminatorMappings(); | ||
| if (isset($mappings[$data->{$discriminator}])) { | ||
| return self::deserialize($data, $mappings[$data->{$discriminator}], $httpHeaders); | ||
| } | ||
| } | ||
|
|
||
| foreach ($class::getOneOfTypes() as $type) { | ||
| try { | ||
| $instance = self::deserialize($data, $type, $httpHeaders); | ||
| } catch (\Throwable $e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Overly broad Prompt for AI agents |
||
| // The data does not match this member type, try the next one. | ||
| continue; | ||
| } | ||
|
|
||
| if ($instance instanceof ModelInterface) { | ||
| if ($instance->valid()) { | ||
| return $instance; | ||
| } | ||
| } elseif ($instance !== null) { | ||
| return $instance; | ||
| } | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException(sprintf('No matching schema in oneOf %s for the given data', $class)); | ||
| } | ||
|
|
||
| /** | ||
| * Build a query string from an array of key value pairs. | ||
| * | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.