-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Union types complex #1549
Closed
Closed
Union types complex #1549
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
41d1789
non disciminated working and disciminated not working.
idbentley d2a5090
iter
idbentley 5a4128c
phpcs
idbentley 1013bd8
iter
idbentley 27adca8
add tests for serialization
idbentley 8fe8a7c
iter
idbentley 08f9152
iter
idbentley 72a2124
handle deserialization failures.
idbentley a86562c
better handle non discriminated unions, and primitives
idbentley 0cd13a6
better handling of primitives
idbentley f96f768
style
idbentley 4f4e238
stan
idbentley 75c2aff
fix tests
idbentley f92f276
add tests for mapped union discriminators
idbentley af18df8
missed chnages
idbentley c0f8cbc
remnants
idbentley 15b3244
code quality
idbentley 186d526
more code cleanup
idbentley 405bf93
revert debug change
idbentley 2ed4057
clean
idbentley 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 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Annotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final class UnionDiscriminator implements SerializerAttribute | ||
{ | ||
use AnnotationUtilsTrait; | ||
|
||
/** @var array<string> */ | ||
public $map = []; | ||
|
||
/** @var string */ | ||
public $field = 'type'; | ||
|
||
public function __construct(array $values = [], string $field = 'type', array $map = []) | ||
{ | ||
$this->loadAnnotationParameters(get_defined_vars()); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Exception; | ||
|
||
final class PropertyMissingException extends RuntimeException | ||
{ | ||
} |
This file contains 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 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we should handle this here... could you please explain why do you need this check at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this check to make the normal deserialization process is less permissive, resulting in more accurate type detection.
Specifically, if the object you are deserializing into contains a required field, and the JSON does not contain that field, the existing deserializer will deserialize without complaint. While, that behaviour could be desirable in some cases, and presumably must be maintained for backwards compatibility, it's undesirable for deserializing non-discriminated unions.
For non-discriminated unions, we use a "guess and check" approach to deserialization, and the absence of a required field is a strong signal that the data we're deserializing may better match a different type.
If we want to deserialize the following data:
Without such a rule, the resulting class is determined by the order of application. If you consider
SimplePerson
first, then all is well. If you considerSimpleCar
first, then the deserialization model skipsname
because it doesn't exist in the class, and deserialization will succeed returning a nullSimpleCar
.By adding the rule, when
SimpleCar
is tested, the requiredmodel
attribute is not present, so deserialization will fail.I agree that the implementation is awkward, and would be happy to refactor!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would really avoid implementing such usecase. It is too specific and I'm really not sure if it worth the complexity.
A
type
filed should be always present in order to match efficiently the class name to use.The same type of discussion were taken when implementing the
@DiscrminatorMap
for inheritance and even in that case any other solution was just not worth the effort.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth the effort, and I don't think it's a very specific usecase.
While, of course deserializing discriminated unions is easier, it's not the only type of data that exists. It's really easy to say "just change the data", but in many cases, that isn't possible - for example: if someone's deserializing data from an external API. Anecdotally in my situation, this case comes up regularly.
In order to provide good support for PHP 8, jms/serializer should have good support for union types,.
I'll note that other deserialization libraries do provide this functionality:
dataclasses_json
,pydantic
,zod
are just three examples.I've provided an implementation that is opt-out (by not using the
UnionHandler
altogether, or you can construct theUnionHandler
withrequireAllProperties=false
to avoid the new failure logic). Is the concern the added code complexity? I'm happy to refactor the code.However, if you still think this isn't an appropriate feature fro the jms/serializer - I would ask that at least we provide a workaround for users. If we could include the ability to
requireAllRequiredFields
when deserializing, it would allow me to write a custom UnionHandler outside of jms/serializer.