Skip to content

Commit

Permalink
Merge pull request #170 from Korbeil/fix/deprecated-behavior
Browse files Browse the repository at this point in the history
Fix deprecated behavior in OpenAPI
  • Loading branch information
Korbeil committed Oct 31, 2019
2 parents 6805721 + faa3350 commit d13e21b
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/OpenApi/Generator/Model/ClassGenerator.php
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Comment\Doc;

trait ClassGenerator
{
Expand All @@ -24,7 +25,7 @@ trait ClassGenerator
*
* @return Stmt\Class_
*/
protected function createModel(string $name, $properties, $methods, bool $hasExtensions = false, $extends = null): Stmt\Class_
protected function createModel(string $name, $properties, $methods, bool $hasExtensions = false, bool $deprecated = false, $extends = null): Stmt\Class_
{
$classExtends = null;
if (null !== $extends) {
Expand All @@ -33,12 +34,24 @@ protected function createModel(string $name, $properties, $methods, bool $hasExt
$classExtends = new Name('\ArrayObject');
}

$attributes = [];
if ($deprecated) {
$attributes['comments'] = [new Doc(<<<EOD
/**
*
* @deprecated
*/
EOD
)];
}

return new Stmt\Class_(
new Name($this->getNaming()->getClassName($name)),
[
'stmts' => array_merge($properties, $methods),
'extends' => $classExtends,
]
],
$attributes
);
}
}
1 change: 1 addition & 0 deletions src/OpenApi/Generator/ModelGenerator.php
Expand Up @@ -36,6 +36,7 @@ protected function doCreateModel(BaseClassGuess $class, $properties, $methods):
$properties,
$methods,
\count($class->getExtensionsType()) > 0,
$class->isDeprecated(),
$extends
);

Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/Guesser/OpenApiSchema/SchemaGuesser.php
Expand Up @@ -29,7 +29,7 @@ protected function isPropertyNullable($property): bool
*/
protected function createClassGuess($object, $reference, $name, $extensions): BaseClassGuess
{
$classGuess = new ClassGuess($object, $reference, $this->naming->getClassName($name), $extensions);
$classGuess = new ClassGuess($object, $reference, $this->naming->getClassName($name), $extensions, $object->getDeprecated() ?? false);

if ($object->getDiscriminator() instanceof Discriminator &&
\is_array($object->getEnum()) && \count($object->getEnum()) > 0) {
Expand Down
8 changes: 8 additions & 0 deletions src/OpenApi/Tests/fixtures/deprecated/.jane-openapi
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => __DIR__ . '/swagger.yaml',
'namespace' => 'Jane\OpenApi\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
];
17 changes: 17 additions & 0 deletions src/OpenApi/Tests/fixtures/deprecated/expected/Client.php
@@ -0,0 +1,17 @@
<?php

namespace Jane\OpenApi\Tests\Expected;

class Client extends \Jane\OpenApiRuntime\Client\Psr7HttplugClient
{
public static function create($httpClient = null)
{
if (null === $httpClient) {
$httpClient = \Http\Discovery\HttpClientDiscovery::find();
}
$messageFactory = \Http\Discovery\MessageFactoryDiscovery::find();
$streamFactory = \Http\Discovery\StreamFactoryDiscovery::find();
$serializer = new \Symfony\Component\Serializer\Serializer(\Jane\OpenApi\Tests\Expected\Normalizer\NormalizerFactory::create(), array(new \Symfony\Component\Serializer\Encoder\JsonEncoder(new \Symfony\Component\Serializer\Encoder\JsonEncode(), new \Symfony\Component\Serializer\Encoder\JsonDecode())));
return new static($httpClient, $messageFactory, $serializer, $streamFactory);
}
}
71 changes: 71 additions & 0 deletions src/OpenApi/Tests/fixtures/deprecated/expected/Model/Foo.php
@@ -0,0 +1,71 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Model;

/**
*
* @deprecated
*/
class Foo
{
/**
*
*
* @var string
*/
protected $email;
/**
*
*
* @deprecated
*
* @var string
*/
protected $bar;
/**
*
*
* @return string
*/
public function getEmail() : string
{
return $this->email;
}
/**
*
*
* @param string $email
*
* @return self
*/
public function setEmail(string $email) : self
{
$this->email = $email;
return $this;
}
/**
*
*
* @deprecated
*
* @return string
*/
public function getBar() : string
{
return $this->bar;
}
/**
*
*
* @param string $bar
*
* @deprecated
*
* @return self
*/
public function setBar(string $bar) : self
{
$this->bar = $bar;
return $this;
}
}
@@ -0,0 +1,46 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Normalizer;

use Jane\JsonSchemaRuntime\Reference;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Foo';
}
public function supportsNormalization($data, $format = null)
{
return is_object($data) && get_class($data) === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Foo';
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_object($data)) {
throw new InvalidArgumentException();
}
$object = new \Jane\OpenApi\Tests\Expected\Model\Foo();
if (property_exists($data, 'email')) {
$object->setEmail($data->{'email'});
}
if (property_exists($data, 'bar')) {
$object->setBar($data->{'bar'});
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
$data->{'email'} = $object->getEmail();
$data->{'bar'} = $object->getBar();
return $data;
}
}
@@ -0,0 +1,14 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Normalizer;

class NormalizerFactory
{
public static function create()
{
$normalizers = array();
$normalizers[] = new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer();
$normalizers[] = new FooNormalizer();
return $normalizers;
}
}
15 changes: 15 additions & 0 deletions src/OpenApi/Tests/fixtures/deprecated/swagger.yaml
@@ -0,0 +1,15 @@
openapi: 3.0.0
info:
version: ''
title: ''
components:
schemas:
Foo:
type: object
deprecated: true
properties:
email:
type: string
bar:
type: string
deprecated: true

0 comments on commit d13e21b

Please sign in to comment.