Skip to content

Commit f0995c3

Browse files
committed
Merge branch 2.7 into 3.0
2 parents 00db2cd + 80c3f3c commit f0995c3

File tree

50 files changed

+334
-931
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+334
-931
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ jobs:
2222
- name: Run commitlint
2323
run: |
2424
commit=$(gh api \
25-
-H "Accept: application/vnd.github+json" \
2625
/repos/${{ github.repository }}/pulls/${{github.event.number}}/commits \
27-
| jq -r '.[0].commit.message')
26+
| jq -r '.[0].commit.message' \
27+
| head -n 1)
2828
# we can't use npx see https://github.com/conventional-changelog/commitlint/issues/613
2929
echo '{}' > package.json
3030
npm install --no-fund --no-audit @commitlint/config-conventional @commitlint/cli

features/jsonld/non_resource.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Feature: JSON-LD non-resource handling
3838
}
3939
}
4040
"""
41+
And the JSON node "notAResource.@id" should not exist
4142

4243
Scenario: Get a resource containing a raw object with selected properties
4344
Given there are 1 dummy objects with relatedDummy and its thirdLevel
@@ -123,3 +124,11 @@ Feature: JSON-LD non-resource handling
123124
"id": 1
124125
}
125126
"""
127+
128+
@php8
129+
Scenario: Get a generated id
130+
When I send a "GET" request to "/genids/1"
131+
Then the response status code should be 200
132+
And the response should be in JSON
133+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
134+
And the JSON node "totalPrice.@id" should exist

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function supportsNormalization(mixed $data, string $format = null, array
5959
/**
6060
* {@inheritdoc}
6161
*
62+
* @param array<string, mixed> $context
63+
*
6264
* @throws UnexpectedValueException
6365
*/
6466
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function normalize(mixed $object, string $format = null, array $context =
6868
{
6969
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
7070
$context = $this->initContext($resourceClass, $context);
71+
$context['api_collection_sub_level'] = true;
7172
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
7273
$data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
7374
$data['@type'] = 'hydra:Collection';

src/JsonLd/ContextBuilder.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use ApiPlatform\Api\IriConverterInterface;
1717
use ApiPlatform\Api\UrlGeneratorInterface;
18-
use ApiPlatform\Exception\RuntimeException;
1918
use ApiPlatform\Metadata\Get;
2019
use ApiPlatform\Metadata\HttpOperation;
2120
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -123,12 +122,10 @@ public function getAnonymousResourceContext(object $object, array $context = [],
123122
'@type' => $shortName,
124123
];
125124

126-
if (!isset($context['iri']) || false !== $context['iri']) {
127-
if (!isset($context['iri']) && !$this->iriConverter) {
128-
throw new RuntimeException('Can not guess an anonymous resource IRI without IRI Converter.');
129-
}
130-
131-
$jsonLdContext['@id'] = $context['iri'] ?? $this->iriConverter->getIriFromResource($object);
125+
if (isset($context['iri'])) {
126+
$jsonLdContext['@id'] = $context['iri'];
127+
} elseif (true === ($context['gen_id'] ?? true) && $this->iriConverter) {
128+
$jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
132129
}
133130

134131
if ($context['has_context'] ?? false) {

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public function normalize(mixed $object, string $format = null, array $context =
7979
$context = $this->initContext($resourceClass, $context);
8080
$metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
8181
} elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
82+
if ($context['api_collection_sub_level'] ?? false) {
83+
unset($context['api_collection_sub_level']);
84+
$context['output']['genid'] = true;
85+
$context['output']['iri'] = null;
86+
}
87+
8288
// We should improve what's behind the context creation, its probably more complicated then it should
8389
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
8490
}

src/JsonLd/Serializer/JsonLdContextTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ private function createJsonLdContext(AnonymousContextBuilderInterface $contextBu
5151
{
5252
// We're in a collection, don't add the @context part
5353
if (isset($context['jsonld_has_context'])) {
54-
return $contextBuilder->getAnonymousResourceContext($object, ['api_resource' => $context['api_resource'] ?? null, 'has_context' => true]);
54+
return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null, 'has_context' => true]);
5555
}
5656

5757
$context['jsonld_has_context'] = true;
5858

59-
return $contextBuilder->getAnonymousResourceContext($object, ['api_resource' => $context['api_resource'] ?? null]);
59+
return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null]);
6060
}
6161
}

src/JsonLd/Serializer/ObjectNormalizer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\JsonLd\Serializer;
1515

1616
use ApiPlatform\Api\IriConverterInterface;
17-
use ApiPlatform\Exception\InvalidArgumentException;
1817
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
1918
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2019
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -74,11 +73,6 @@ public function normalize(mixed $object, string $format = null, array $context =
7473
}
7574

7675
if (isset($originalResource)) {
77-
try {
78-
$context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
79-
} catch (InvalidArgumentException) {
80-
// The original resource has no identifiers
81-
}
8276
$context['api_resource'] = $originalResource;
8377
}
8478

src/Metadata/ApiProperty.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function __construct(
6868
private ?array $schema = null,
6969
private ?bool $initializable = null,
7070
private $iris = null,
71+
private ?bool $genId = null,
7172
private array $extraProperties = []
7273
) {
7374
if (\is_string($types)) {
@@ -403,4 +404,20 @@ public function withIris(string|array $iris): self
403404

404405
return $metadata;
405406
}
407+
408+
/**
409+
* Whether to generate a skolem iri on anonymous resources.
410+
*/
411+
public function getGenId()
412+
{
413+
return $this->genId;
414+
}
415+
416+
public function withGenId(bool $genId): self
417+
{
418+
$metadata = clone $this;
419+
$metadata->genId = $genId;
420+
421+
return $metadata;
422+
}
406423
}

src/Metadata/Extractor/XmlPropertyExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ protected function extractPath(string $path): void
7171
'initializable' => $this->phpize($property, 'initializable', 'bool'),
7272
'extraProperties' => $this->buildExtraProperties($property, 'extraProperties'),
7373
'iris' => $this->buildArrayValue($property, 'iri'),
74+
'genId' => $this->phpize($property, 'genId', 'bool'),
7475
];
7576
}
7677
}

0 commit comments

Comments
 (0)