Skip to content

Commit

Permalink
Fix reference in endpoint parameter schema (#126)
Browse files Browse the repository at this point in the history
Fix reference in endpoint parameter schema
  • Loading branch information
Korbeil committed Sep 26, 2019
2 parents b3b1989 + d7a27eb commit a489095
Show file tree
Hide file tree
Showing 19 changed files with 556 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/OpenApi/Generator/EndpointGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ private function getConstructor(Operation $operation, Context $context): array
$parameter = $this->resolveParameter($parameter);
}

if (!$parameter instanceof \stdClass && $parameter->getSchema() instanceof Reference) {
[$_, $schema] = $this->resolve($parameter->getSchema(), Schema::class);
$parameter->setSchema($schema);
}

if ($parameter instanceof ParameterWithSchemaWithExampleInPath || $parameter instanceof ParameterWithSchemaWithExamplesInPath) {
$pathParams[] = $this->nonBodyParameterGenerator->generateMethodParameter($parameter, $context, $operation->getReference() . '/parameters/' . $key);
$pathParamsDoc[] = $this->nonBodyParameterGenerator->generateMethodDocParameter($parameter, $context, $operation->getReference() . '/parameters/' . $key);
Expand Down
8 changes: 8 additions & 0 deletions src/OpenApi/Tests/fixtures/model-type-reference/.jane-openapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => __DIR__ . '/schema.json',
'namespace' => 'Jane\OpenApi\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
];
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Model;

class Model
{
/**
*
*
* @var string
*/
protected $foo;
/**
*
*
* @var string
*/
protected $bar;
/**
*
*
* @return string
*/
public function getFoo() : string
{
return $this->foo;
}
/**
*
*
* @param string $foo
*
* @return self
*/
public function setFoo(string $foo) : self
{
$this->foo = $foo;
return $this;
}
/**
*
*
* @return string
*/
public function getBar() : string
{
return $this->bar;
}
/**
*
*
* @param string $bar
*
* @return self
*/
public function setBar(string $bar) : self
{
$this->bar = $bar;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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 ModelNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Model';
}
public function supportsNormalization($data, $format = null)
{
return get_class($data) === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Model';
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_object($data)) {
throw new InvalidArgumentException();
}
$object = new \Jane\OpenApi\Tests\Expected\Model\Model();
if (property_exists($data, 'foo')) {
$object->setFoo($data->{'foo'});
}
if (property_exists($data, 'bar')) {
$object->setBar($data->{'bar'});
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
if (null !== $object->getFoo()) {
$data->{'foo'} = $object->getFoo();
}
if (null !== $object->getBar()) {
$data->{'bar'} = $object->getBar();
}
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -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 ModelNormalizer();
return $normalizers;
}
}
26 changes: 26 additions & 0 deletions src/OpenApi/Tests/fixtures/model-type-reference/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {},
"components": {
"schemas": {
"Model": {
"type": "object",
"properties": {
"foo": {
"$ref": "#/components/schemas/Foo"
},
"bar": {
"type": "string"
}
}
},
"Foo": {
"type": "string"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => __DIR__ . '/schema.json',
'namespace' => 'Jane\OpenApi\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Jane\OpenApi\Tests\Expected;

class Client extends \Jane\OpenApiRuntime\Client\Psr7HttplugClient
{
/**
* caca
*
* @param array $queryParameters {
* @var string $bar
* }
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE)
*
* @return null|\Psr\Http\Message\ResponseInterface
*/
public function foo(array $queryParameters = array(), string $fetch = self::FETCH_OBJECT)
{
return $this->executePsr7Endpoint(new \Jane\OpenApi\Tests\Expected\Endpoint\Foo($queryParameters), $fetch);
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Endpoint;

class Foo extends \Jane\OpenApiRuntime\Client\BaseEndpoint implements \Jane\OpenApiRuntime\Client\Psr7Endpoint
{
/**
* caca
*
* @param array $queryParameters {
* @var string $bar
* }
*/
public function __construct(array $queryParameters = array())
{
$this->queryParameters = $queryParameters;
}
use \Jane\OpenApiRuntime\Client\Psr7EndpointTrait;
public function getMethod() : string
{
return 'GET';
}
public function getUri() : string
{
return '/foo';
}
public function getBody(\Symfony\Component\Serializer\SerializerInterface $serializer, $streamFactory = null) : array
{
return array(array(), null);
}
protected function getQueryOptionsResolver() : \Symfony\Component\OptionsResolver\OptionsResolver
{
$optionsResolver = parent::getQueryOptionsResolver();
$optionsResolver->setDefined(array('bar'));
$optionsResolver->setRequired(array('bar'));
$optionsResolver->setDefaults(array());
$optionsResolver->setAllowedTypes('bar', array('string'));
return $optionsResolver;
}
/**
* {@inheritdoc}
*
*
* @return null
*/
protected function transformResponseBody(string $body, int $status, \Symfony\Component\Serializer\SerializerInterface $serializer, ?string $contentType = null)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Jane\OpenApi\Tests\Expected\Normalizer;

class NormalizerFactory
{
public static function create()
{
$normalizers = array();
$normalizers[] = new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer();
return $normalizers;
}
}
37 changes: 37 additions & 0 deletions src/OpenApi/Tests/fixtures/parameter-type-reference/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {
"/foo": {
"get": {
"operationId": "foo",
"description": "caca",
"parameters": [
{
"name": "bar",
"in": "query",
"schema": {
"$ref": "#/components/schemas/Bar"
},
"required": true
}
],
"responses": {
"default": {
"description": "Default response"
}
}
}
}
},
"components": {
"schemas": {
"Bar": {
"type": "string"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => __DIR__ . '/schema.json',
'namespace' => 'Jane\OpenApi\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Jane\OpenApi\Tests\Expected;

class Client extends \Jane\OpenApiRuntime\Client\Psr7HttplugClient
{
/**
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE)
*
* @return null|\Jane\OpenApi\Tests\Expected\Model\FooGetResponse200|\Psr\Http\Message\ResponseInterface
*/
public function foo(string $fetch = self::FETCH_OBJECT)
{
return $this->executePsr7Endpoint(new \Jane\OpenApi\Tests\Expected\Endpoint\Foo(), $fetch);
}
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);
}
}

0 comments on commit a489095

Please sign in to comment.