Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/JsonSchema/DefinitionNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ public function create(string $className, string $format = 'json', ?string $inpu
$name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);
} else {
$groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
$name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
$attributes = (array) ($serializerContext[AbstractNormalizer::ATTRIBUTES] ?? []);

$parts = [];

if ($groups) {
$parts[] = implode('_', $groups);
}

if ($attributes) {
$parts[] = $this->getAttributesAsString($attributes);
}

$name = $parts ? \sprintf('%s-%s', $prefix, implode('_', $parts)) : $prefix;
}

if (false === ($serializerContext['gen_id'] ?? true)) {
Expand Down Expand Up @@ -99,4 +111,26 @@ private function createPrefixFromClass(string $fullyQualifiedClassName, int $nam

return $name;
}

private function getAttributesAsString(array $attributes): string
{
$parts = [];

foreach ($attributes as $key => $value) {
if (\is_array($value)) {
$childString = $this->getAttributesAsString($value);
$children = explode('_', $childString);

foreach ($children as $child) {
$parts[] = $key.'.'.$child;
}
} elseif (\is_string($key)) {
$parts[] = $key.'_'.$value;
} else {
$parts[] = $value;
}
}

return implode('_', $parts);
}
}
36 changes: 34 additions & 2 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,24 @@ private function buildLegacyPropertySchema(Schema $schema, string $definitionNam
continue;
}

$childSerializerContext = $serializerContext + [self::FORCE_SUBSCHEMA => true, 'gen_id' => $propertyMetadata->getGenId() ?? true];
if (isset($serializerContext[AbstractNormalizer::ATTRIBUTES])) {
$attributes = $serializerContext[AbstractNormalizer::ATTRIBUTES];
if (\is_array($attributes) && \array_key_exists($normalizedPropertyName, $attributes) && \is_array($attributes[$normalizedPropertyName])) {
$childSerializerContext[AbstractNormalizer::ATTRIBUTES] = $attributes[$normalizedPropertyName];
} else {
unset($childSerializerContext[AbstractNormalizer::ATTRIBUTES]);
}
}

$subSchemaFactory = $this->schemaFactory ?: $this;
$subSchema = $subSchemaFactory->buildSchema(
$className,
$format,
$parentType,
null,
$subSchema,
$serializerContext + [self::FORCE_SUBSCHEMA => true, 'gen_id' => $propertyMetadata->getGenId() ?? true],
$childSerializerContext,
false,
);

Expand Down Expand Up @@ -365,6 +375,16 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
$className = $valueType->getWrappedType()->getClassName();
}

$childSerializerContext = $serializerContext + [self::FORCE_SUBSCHEMA => true, 'gen_id' => $propertyMetadata->getGenId() ?? true];
if (isset($serializerContext[AbstractNormalizer::ATTRIBUTES])) {
$attributes = $serializerContext[AbstractNormalizer::ATTRIBUTES];
if (\is_array($attributes) && \array_key_exists($normalizedPropertyName, $attributes) && \is_array($attributes[$normalizedPropertyName])) {
$childSerializerContext[AbstractNormalizer::ATTRIBUTES] = $attributes[$normalizedPropertyName];
} else {
unset($childSerializerContext[AbstractNormalizer::ATTRIBUTES]);
}
}

$subSchemaInstance = new Schema($version);
$subSchemaInstance->setDefinitions($schema->getDefinitions());
$subSchemaFactory = $this->schemaFactory ?: $this;
Expand All @@ -374,7 +394,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
$parentType,
null,
$subSchemaInstance,
$serializerContext + [self::FORCE_SUBSCHEMA => true, 'gen_id' => $propertyMetadata->getGenId() ?? true],
$childSerializerContext,
false,
);
if (!isset($subSchemaResult['$ref'])) {
Expand Down Expand Up @@ -448,6 +468,18 @@ private function getFactoryOptions(array $serializerContext, array $validationGr
$options['denormalization_groups'] = $denormalizationGroups;
}

if (isset($serializerContext[AbstractNormalizer::ATTRIBUTES])) {
$options['serializer_attributes'] = (array) $serializerContext[AbstractNormalizer::ATTRIBUTES];
}

if ($operation && ($normalizationAttributes = $operation->getNormalizationContext()['attributes'] ?? null)) {
$options['normalization_attributes'] = $normalizationAttributes;
}

if ($operation && ($denormalizationAttributes = $operation->getDenormalizationContext()['attributes'] ?? null)) {
$options['denormalization_attributes'] = $denormalizationAttributes;
}

if ($validationGroups) {
$options['validation_groups'] = $validationGroups;
}
Expand Down
35 changes: 35 additions & 0 deletions src/JsonSchema/Tests/DefinitionNameFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,41 @@ public static function providerDefinitions(): iterable
yield ['Bar.DtoOutput.jsonapi-read_write', Dummy::class, 'jsonapi', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::GROUPS => ['read', 'write']]];
yield ['Bar.DtoOutput.jsonhal-read_write', Dummy::class, 'jsonhal', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::GROUPS => ['read', 'write']]];
yield ['Bar.DtoOutput.jsonld-read_write', Dummy::class, 'jsonld', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::GROUPS => ['read', 'write']]];

yield ['Dummy-id', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['id']]];
yield ['Dummy.jsonapi-id', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['id']]];
yield ['Dummy.jsonhal-id', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['id']]];
yield ['Dummy.jsonld-id', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['id']]];

yield ['Dummy-id_name', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['id', 'name']]];
yield ['Dummy.jsonapi-id_name', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['id', 'name']]];
yield ['Dummy.jsonhal-id_name', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['id', 'name']]];
yield ['Dummy.jsonld-id_name', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['id', 'name']]];

yield ['Dummy-title_author.name', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name']]]];
yield ['Dummy.jsonapi-title_author.name', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name']]]];
yield ['Dummy.jsonhal-title_author.name', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name']]]];
yield ['Dummy.jsonld-title_author.name', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name']]]];

yield ['Dummy-title_author_name', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author', 'name']]];
yield ['Dummy.jsonapi-title_author_name', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author', 'name']]];
yield ['Dummy.jsonhal-title_author_name', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author', 'name']]];
yield ['Dummy.jsonld-title_author_name', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author', 'name']]];

yield ['Dummy-title_author.name_name', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Dummy.jsonapi-title_author.name_name', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Dummy.jsonhal-title_author.name_name', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Dummy.jsonld-title_author.name_name', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];

yield ['Dummy-title_author.name_author.id_name', Dummy::class, 'json', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name', 'id'], 'name']]];
yield ['Dummy.jsonapi-title_author.name_author.id_name', Dummy::class, 'jsonapi', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name', 'id'], 'name']]];
yield ['Dummy.jsonhal-title_author.name_author.id_name', Dummy::class, 'jsonhal', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name', 'id'], 'name']]];
yield ['Dummy.jsonld-title_author.name_author.id_name', Dummy::class, 'jsonld', null, null, [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name', 'id'], 'name']]];

yield ['Bar.DtoOutput-title_author.name_name', Dummy::class, 'json', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Bar.DtoOutput.jsonapi-title_author.name_name', Dummy::class, 'jsonapi', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Bar.DtoOutput.jsonhal-title_author.name_name', Dummy::class, 'jsonhal', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
yield ['Bar.DtoOutput.jsonld-title_author.name_name', Dummy::class, 'jsonld', DtoOutput::class, new Get(shortName: 'Bar'), [AbstractNormalizer::ATTRIBUTES => ['title', 'author' => ['name'], 'name']]];
}

#[\PHPUnit\Framework\Attributes\DataProvider('providerDefinitions')]
Expand Down
62 changes: 62 additions & 0 deletions src/JsonSchema/Tests/Fixtures/ApiResource/ChildAttributeDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource]
class ChildAttributeDummy
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\Column(length: 255)]
private ?string $name = null;

#[ORM\Column(length: 50)]
private ?string $code = null;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getCode(): ?string
{
return $this->code;
}

public function setCode(string $code): self
{
$this->code = $code;

return $this;
}
}
84 changes: 84 additions & 0 deletions src/JsonSchema/Tests/Fixtures/ApiResource/ParentAttributeDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\JsonSchema\Tests\Fixtures\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource(
operations: [
new Get(
normalizationContext: ['attributes' => ['title', 'child' => ['name']]]
),
]
)]
class ParentAttributeDummy
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\Column(length: 255)]
private ?string $title = null;

#[ORM\Column(type: 'text')]
private ?string $description = null;

#[ORM\ManyToOne(targetEntity: ChildAttributeDummy::class)]
private ?ChildAttributeDummy $child = null;

public function getId(): ?int
{
return $this->id;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function getDescription(): ?string
{
return $this->description;
}

public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}

public function getChild(): ?ChildAttributeDummy
{
return $this->child;
}

public function setChild(?ChildAttributeDummy $child): self
{
$this->child = $child;

return $this;
}
}
Loading
Loading