From 521e49898289eec7f3a73b9d59b921c9b641bba8 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Tue, 13 Feb 2018 21:47:39 +0100 Subject: [PATCH] If class guessed in body, consider it serializable, no matter the consume value --- Generator/EndpointGenerator.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Generator/EndpointGenerator.php b/Generator/EndpointGenerator.php index 3f2d4d7..66fe9a6 100644 --- a/Generator/EndpointGenerator.php +++ b/Generator/EndpointGenerator.php @@ -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), ]), ]); @@ -334,7 +334,7 @@ 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; @@ -342,18 +342,19 @@ private function getGetBody(Operation $operation): Stmt\ClassMethod $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) { @@ -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; @@ -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 .= '[]';