Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from sagikazarmark/multiple_typehint
Browse files Browse the repository at this point in the history
Prevent the same typehint to appear multiple times in the return docblock
  • Loading branch information
joelwurtz committed Feb 1, 2016
2 parents 2415da4 + 520b052 commit 3410a87
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Generator/OperationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public function generate($name, Operation $operation, Context $context)
list($outputType, $ifStatus) = $this->createResponseDenormalizationStatement($status, $response->getSchema(), $context);

if (null !== $outputType) {
$outputTypes[] = $outputType;
if (!in_array($outputType, $outputTypes)) {
$outputTypes[] = $outputType;
}

$outputStatements[] = $ifStatus;
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/model-in-response/expected/Model/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Joli\Jane\OpenApi\Tests\Expected\Model;

class Error
{
/**
* @var string
*/
protected $message;

/**
* @return string
*/
public function getMessage()
{
return $this->message;
}

/**
* @param string $message
*
* @return self
*/
public function setMessage($message = null)
{
$this->message = $message;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Joli\Jane\OpenApi\Tests\Expected\Normalizer;

use Joli\Jane\Reference\Reference;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;

class ErrorNormalizer extends SerializerAwareNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization($data, $type, $format = null)
{
if ($type !== 'Joli\\Jane\\OpenApi\\Tests\\Expected\\Model\\Error') {
return false;
}

return true;
}

public function supportsNormalization($data, $format = null)
{
if ($data instanceof \Joli\Jane\OpenApi\Tests\Expected\Model\Error) {
return true;
}

return false;
}

public function denormalize($data, $class, $format = null, array $context = [])
{
if (empty($data)) {
return null;
}
if (isset($data->{'$ref'})) {
return new Reference($data->{'$ref'}, $context['rootSchema'] ?: null);
}
$object = new \Joli\Jane\OpenApi\Tests\Expected\Model\Error();
if (!isset($context['rootSchema'])) {
$context['rootSchema'] = $object;
}
if (isset($data->{'message'})) {
$object->setMessage($data->{'message'});
}

return $object;
}

public function normalize($object, $format = null, array $context = [])
{
$data = new \stdClass();
if (null !== $object->getMessage()) {
$data->{'message'} = $object->getMessage();
}

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static function create()
$normalizers[] = new NormalizerArray();
$normalizers[] = new SchemaNormalizer();
$normalizers[] = new ObjectPropertyNormalizer();
$normalizers[] = new ErrorNormalizer();

return $normalizers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestResource extends Resource
* @param array $parameters List of parameters
* @param string $fetch Fetch mode (object or response)
*
* @return \Psr\Http\Message\ResponseInterface|\Joli\Jane\OpenApi\Tests\Expected\Model\Schema
* @return \Psr\Http\Message\ResponseInterface|\Joli\Jane\OpenApi\Tests\Expected\Model\Schema|\Joli\Jane\OpenApi\Tests\Expected\Model\Error
*/
public function getTest($parameters = [], $fetch = self::FETCH_OBJECT)
{
Expand All @@ -26,6 +26,12 @@ public function getTest($parameters = [], $fetch = self::FETCH_OBJECT)
if ('200' == $response->getStatusCode()) {
return $this->serializer->deserialize($response->getBody()->getContents(), 'Joli\\Jane\\OpenApi\\Tests\\Expected\\Model\\Schema', 'json');
}
if ('400' == $response->getStatusCode()) {
return $this->serializer->deserialize($response->getBody()->getContents(), 'Joli\\Jane\\OpenApi\\Tests\\Expected\\Model\\Error', 'json');
}
if ('404' == $response->getStatusCode()) {
return $this->serializer->deserialize($response->getBody()->getContents(), 'Joli\\Jane\\OpenApi\\Tests\\Expected\\Model\\Error', 'json');
}
}

return $response;
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/model-in-response/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
"$ref": "#/definitions/Schema"
}
}
},
"Error": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
},
"paths": {
Expand All @@ -46,6 +54,18 @@
"schema": {
"$ref": "#/definitions/Schema"
}
},
"400": {
"description": "bad request",
"schema": {
"$ref": "#/definitions/Error"
}
},
"404": {
"description": "not found",
"schema": {
"$ref": "#/definitions/Error"
}
}
},
"tags": [
Expand Down

0 comments on commit 3410a87

Please sign in to comment.