Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
If class guessed in body, consider it serializable, no matter the con…
Browse files Browse the repository at this point in the history
…sume value
  • Loading branch information
joelwurtz committed Feb 13, 2018
1 parent 711ce50 commit 521e498
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions Generator/EndpointGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function createEndpointClass(Operation $operation, Context $context, bool
}, $this->getTrait())),
$this->getGetMethod($operation),
$this->getGetUri($operation),
$this->getGetBody($operation),
$this->getGetBody($operation, $context),
]),
]);

Expand Down Expand Up @@ -334,26 +334,27 @@ private function getOptionsResolverMethod(Operation $operation, $class, $methodN
]);
}

private function getGetBody(Operation $operation): Stmt\ClassMethod
private function getGetBody(Operation $operation, Context $context): Stmt\ClassMethod
{
$hasBody = false;
$isSerializableBody = false;
$isFormBody = false;
$hasFileInForm = false;
$consumes = is_array($operation->getOperation()->getConsumes()) ? $operation->getOperation()->getConsumes() : [$operation->getOperation()->getConsumes()];

foreach ($operation->getParameters() as $parameter) {
foreach ($operation->getParameters() as $key => $parameter) {
if ($parameter instanceof BodyParameter && $parameter->getSchema() !== null) {
$hasBody = true;
$schema = $parameter->getSchema();

if ($schema instanceof Reference) {
[$_, $schema] = $this->resolve($schema, Schema::class);
}
[$classGuess, $array, $schema] = $this->guessClass($parameter->getSchema(), $operation->getReference() . '/parameters/' . $key, $context);

if (\in_array('application/json', $consumes, true)) {
$isSerializableBody = true;
}

if (null !== $classGuess) {
$isSerializableBody = true;
}
}

if ($parameter instanceof FormDataParameterSubSchema) {
Expand Down Expand Up @@ -502,7 +503,7 @@ private function getTransformResponseBody(Operation $operation, string $endpoint
], ]), $outputTypes, $throwTypes];
}

private function createResponseDenormalizationStatement(string $name, string $status, $schema, Context $context, string $reference, string $description)
private function guessClass($schema, string $reference, Context $context)
{
$jsonReference = $reference;
$array = false;
Expand All @@ -522,13 +523,20 @@ private function createResponseDenormalizationStatement(string $name, string $st
}

$classGuess = $context->getRegistry()->getClass($jsonReference);

return [$classGuess, $array, $schema];
}

private function createResponseDenormalizationStatement(string $name, string $status, $schema, Context $context, string $reference, string $description)
{
[$classGuess, $array, $schema] = $this->guessClass($schema, $reference, $context);
$returnType = 'null';
$throwType = null;
$serializeStmt = new Expr\ConstFetch(new Name('null'));
$class = null;

if (null !== $classGuess) {
$class = $context->getRegistry()->getSchema($jsonReference)->getNamespace() . '\\Model\\' . $classGuess->getName();
$class = $context->getRegistry()->getSchema($classGuess->getReference())->getNamespace() . '\\Model\\' . $classGuess->getName();

if ($array) {
$class .= '[]';
Expand Down

0 comments on commit 521e498

Please sign in to comment.