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

Add oneOf schema support #103

Merged
merged 12 commits into from
Jan 2, 2024

Conversation

oleg-docler
Copy link

No description provided.

@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this file is needed, is it?

@@ -235,6 +234,17 @@ protected function generateMapStatementForFreeFormObject(Field $root, Variable $
return $this->builder->return($schemaInit);
}

protected function hasComposite(array $fields): bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this being used?

$ifStmt = $this->builder->expr(
}

if ($root->getDiscriminator()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you please add in tests example when there's oneOf but no discriminator? because discriminator is optional so it should work even without it...

);

$assignMethodName = $this->builder->expr(
$this->builder->assign($this->builder->var('methodName'), $this->builder->concat($this->builder->val('set'), $this->builder->funcCall('ucfirst', [$payloadDiscriminator])))
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use multiple lines for better readability, here and below

$field->getName(),
$payloadVariable,
]
if (!$root->hasOneOf()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment I made on the other PR, I think this code is modelling the anyOf, not the oneOf (the difference being oneOf must match only 1 schema)

Copy link
Collaborator

Choose a reason for hiding this comment

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

we could add support for both anyOf and oneOf here IMO, and just do the extra check/constraint about the matching 1,N schemas, wdyt?

@oleg-docler oleg-docler force-pushed the one-of-with-discriminator branch from d4a9a77 to 2af3b3e Compare December 22, 2023 08:31
$schema->setMachine($this->machineMapper->toSchema($payload));
} catch (UnexpectedResponseBodyException $exception) {
}
if (empty($schema->toArray())) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if we add a $matches = 0 before all the tries, and inside the try after the $schema->set... we $matches ++

we know when generating this Mapper if it's oneOf or anyOf, so we can already add the code when it's anyOf, we check if ($matches === 0) throw UnexpectedResponseBodyException, when it's oneOf we check if $matches !== 1 throw UnexpectedResponseBodyException

Copy link
Collaborator

Choose a reason for hiding this comment

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

and then we can remove this if (empty(toarray()

- type: number
- type: string
- $ref: '#/components/schemas/EmbeddedObject'
- $ref: '#/components/schemas/EmbeddedObject'
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks a bit weird anyOf X and X... should be X and Y

}

/** @var Reference $anyOfSchema */
foreach ($schema->anyOf as $anyOfSchema) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe to avoid duplicates we could have just 1 block
if (isset($schema->oneOf) || isset($schema->anyOf)) {
...
then in the foreach ($schema->oneOf ?? $schema->anyOf or smth like that

OR use some private method if you prefer, sending the $schema->oneOf or $schema->anyOf, typehinting with Reference wdyt?

),
];
if ($root->hasOneOf()) {
$tryStatements[] = $this->builder->return($schemaVar);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this return is doing much, my suggestions with that $matches would already discard the need for this return when it's oneOf

'/SchemaMapper/GetExampleResponseBodyMapperWithoutDiscriminator.php',
'/SchemaMapper/OneOfResponseBodyMapperWithoutDiscriminator.php',
self::BASE_NAMESPACE . SchemaMapperGenerator::NAMESPACE_SUBPATH . '\\GetExampleResponseBodyMapper',
ConfigurationBuilder::fake()->build(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you please extend the all these generator tests you added with the php 8.0 version too?

@vsouz4
Copy link
Collaborator

vsouz4 commented Dec 23, 2023

please add test for anyOf + discriminator also

@vsouz4 vsouz4 merged commit e663eaf into DoclerLabs:master Jan 2, 2024
1 of 2 checks passed
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.

3 participants