Skip to content

Commit

Permalink
Merge pull request #1622 from goetas/jms-inline
Browse files Browse the repository at this point in the history
Add support for JMS Serializer inline property feature
  • Loading branch information
GuilhemN authored May 15, 2020
2 parents be89b2b + 2fd95e2 commit 2a78b42
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ModelDescriber/JMSModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,20 @@ public function describe(Model $model, Schema $schema)
$reflection = new \ReflectionProperty($item->class, $item->name);
}

$property = $properties->get($annotationsReader->getPropertyName($reflection, $name));
$groups = $this->computeGroups($context, $item->type);

if (true === $item->inline && isset($item->type['name'])) {
// currently array types can not be documented :-/
if (!in_array($item->type['name'], ['array', 'ArrayCollection'], true)) {
$inlineModel = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $item->type['name']), $groups);
$this->describe($inlineModel, $schema);
}
$context->popPropertyMetadata();

continue;
}

$property = $properties->get($annotationsReader->getPropertyName($reflection, $name));
$annotationsReader->updateProperty($reflection, $property, $groups);
} catch (\ReflectionException $e) {
$property = $properties->get($name);
Expand Down
34 changes: 34 additions & 0 deletions Tests/Functional/Entity/JMSNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;

use JMS\Serializer\Annotation as Serializer;

/**
* JMSNote.
*
* @Serializer\ExclusionPolicy("all")
*/
class JMSNote
{
/**
* @Serializer\Type("string")
* @Serializer\Expose
*/
private $long;

/**
* @Serializer\Type("int")
* @Serializer\Expose
*/
private $short;
}
7 changes: 7 additions & 0 deletions Tests/Functional/Entity/JMSUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ class JMSUser
*/
private $deepFreeFormObjectCollection;

/**
* @Serializer\Type("Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNote")
* @Serializer\Inline()
* @Serializer\Expose
*/
private $notes;

public function setRoles($roles)
{
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/Functional/JMSFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public function testModelDocumentation()
],
],
],
'long' => [
'type' => 'string',
],
'short' => [
'type' => 'integer',
],
],
], $this->getModel('JMSUser')->toArray());

Expand Down

0 comments on commit 2a78b42

Please sign in to comment.