Skip to content
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

PHP discriminated unions proposal #5407

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

ajgateno
Copy link
Member

Our PHP SDK generator currently does not support discriminated unions. This PR is a proposal detailing how they should look.

@ajgateno ajgateno force-pushed the alberto/php-discriminated-unions-proposal branch from 61b4b03 to da8ce2a Compare December 17, 2024 20:41
Copy link
Member

@amckinney amckinney left a comment

Choose a reason for hiding this comment

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

This looks great - mostly just thinking out loud on my comments, but I think this is shaping up really well.

seed/php-sdk/unions/src/Core/Json/JsonSerializableType.php Outdated Show resolved Hide resolved
seed/php-sdk/unions/src/Core/Json/JsonSerializableType.php Outdated Show resolved Hide resolved
Comment on lines +179 to +182
$dateType = null;
$arrayType = null;
$unionType = null;
$typeName = null;
Copy link
Member

Choose a reason for hiding this comment

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

nit/optional: I wonder if we could extract this into a helper function that we can reuse here and above. As long as this is well tested it's OK if we keep things as-is for now, just thinking about the long term structure of these core utilities.

seed/php-sdk/unions/src/Types/Types/UnionWithLiteral.php Outdated Show resolved Hide resolved
Comment on lines +78 to +82
if ($this->value !== 'fern') {
throw new \Exception(
"Expected value to be \"fern\"."
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Depending on the phpstan literal support and whether or not we make this always be the literal value, we might not need this check.

With that said, we should definitely validate against literal values in the [de]serialization layer. For example, if this is a response type and the server sends the following, we should fail to deserialize the type:

{
  "fern": "something"
}

All that to say - the more we can move this validation logic to the serialization layer the better. That way when users have a constructed instance of the type, it's guaranteed to be valid.

seed/php-sdk/unions/src/Union/Types/Shape.php Outdated Show resolved Hide resolved
seed/php-sdk/unions/src/Union/Types/Shape.php Show resolved Hide resolved
[
'base' => 'base',
'type' => 'fern',
'fern' => 'fern',
Copy link
Member

Choose a reason for hiding this comment

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

Depending on the result of the conversation above, would be great to include a test that throws when the literal doesn't match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants