Unverified Commit 78fa7224 authored by gabesullice's avatar gabesullice Committed by e0ipso

Issue #3025037 by gabesullice, Wim Leers: Make JSON:API Extras compatible with...

Issue #3025037 by gabesullice, Wim Leers: Make JSON:API Extras compatible with the upcoming 2.1 release
parent 53895e54
......@@ -19,7 +19,7 @@
],
"require": {
"drupal/core": "^8.5",
"drupal/jsonapi": "^2.0-rc1",
"drupal/jsonapi": "^2.1",
"e0ipso/shaper": "^1"
}
}
......@@ -125,7 +125,7 @@ class EntityToJsonApi {
return $this->serializer->normalize($document,
'api_json',
$this->calculateContext($entity, $includes)
)->rasterizeValue();
)->getNormalization();
}
/**
......
......@@ -2,12 +2,11 @@
namespace Drupal\jsonapi_extras\Normalizer;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\jsonapi\Normalizer\NormalizerBase;
use Drupal\jsonapi\Normalizer\FieldItemNormalizer as JsonapiFieldItemNormalizer;
use Drupal\jsonapi\Normalizer\Value\FieldItemNormalizerValue;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
......@@ -75,8 +74,8 @@ class FieldItemNormalizer extends NormalizerBase implements DenormalizerInterfac
return $normalized_output;
}
// Apply any enhancements necessary.
$processed = $enhancer->undoTransform($normalized_output->rasterizeValue());
$normalized_output = new FieldItemNormalizerValue([$processed], new CacheableMetadata());
$processed = $enhancer->undoTransform($normalized_output->getNormalization());
$normalized_output = new CacheableNormalization($normalized_output, $processed);
return $normalized_output;
}
......
......@@ -5,7 +5,7 @@ namespace Drupal\jsonapi_extras\Normalizer;
use Drupal\jsonapi\Normalizer\NormalizerBase;
use Drupal\jsonapi\Normalizer\RelationshipItem;
use Drupal\jsonapi\Normalizer\RelationshipItemNormalizer as RelationshipItemNormalizerJsonapi;
use Drupal\jsonapi\Normalizer\Value\RelationshipItemNormalizerValue;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
......@@ -67,16 +67,10 @@ class RelationshipItemNormalizer extends NormalizerBase implements SerializerAwa
return $normalized_output;
}
// Apply any enhancements necessary.
$transformed = $enhancer->undoTransform($normalized_output->rasterizeValue());
$transformed = $enhancer->undoTransform($normalized_output->getNormalization());
$target_id = $transformed['id'];
$target_resource_type = $this->resourceTypeRepository
->getByTypeName($transformed['type']);
return new RelationshipItemNormalizerValue(
['target_uuid' => $target_id, 'meta' => $transformed['meta']],
// @TODO: Enhancers should utilize the new CacheableNormalization to infer additional cacheability from the enhancer.
$normalized_output,
$target_resource_type
);
// @TODO: Enhancers should utilize CacheableNormalization to infer additional cacheability from the enhancer.
return new CacheableNormalization($normalized_output, ['target_uuid' => $target_id, 'meta' => $transformed['meta']]);
}
/**
......
......@@ -139,11 +139,12 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
$entity_type->isInternal() || (bool) $resource_config->get('disabled'),
static::isLocatableResourceType($entity_type, $bundle),
TRUE,
static::isVersionableResourceType($entity_type),
$this->overrideFieldMapping($resource_config)
);
// Inject additional services through setters. By using setter injection
// rather that constructor injection, we prevent future BC breaks.
// rather that constructor injection, we prevent most future BC breaks.
$resource_type->setJsonapiResourceConfig($resource_config);
$resource_type->setEnhancerManager($this->enhancerManager);
$resource_type->setConfigFactory($this->configFactory);
......
......@@ -30,6 +30,11 @@ class JsonApiExtrasFunctionalTest extends JsonApiFunctionalTestBase {
* {@inheritdoc}
*/
protected function setUp() {
// Check that the e0ipso/shaper library is available.
if (!class_exists("\\Shaper\\DataAdaptor\\DataAdaptorBase")) {
$this->fail('The e0ipso/shaper library is missing. You can install it with `composer require e0ipso/shaper`.');
}
parent::setUp();
// Add vocabs field to the tags.
$this->createEntityReferenceField(
......
......@@ -4,6 +4,7 @@ namespace Drupal\Tests\jsonapi_extras\Kernel\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigException;
use Drupal\jsonapi\Access\EntityAccessChecker;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\Controller\EntityResource;
use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel as JsonApiDocumentTopLevel2;
......@@ -68,7 +69,13 @@ class EntityResourceTest extends KernelTestBase {
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('renderer'),
$this->container->get('entity.repository'),
$this->container->get('jsonapi.include_resolver')
$this->container->get('jsonapi.include_resolver'),
new EntityAccessChecker(
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('router.no_access_checks'),
$this->container->get('current_user'),
$this->container->get('entity.repository')
)
);
$response = $entity_resource->createIndividual($resource_type, $node_type, new Request());
// As a side effect, the node type will also be saved.
......@@ -116,7 +123,13 @@ class EntityResourceTest extends KernelTestBase {
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('renderer'),
$this->container->get('entity.repository'),
$this->container->get('jsonapi.include_resolver')
$this->container->get('jsonapi.include_resolver'),
new EntityAccessChecker(
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('router.no_access_checks'),
$this->container->get('current_user'),
$this->container->get('entity.repository')
)
);
$response = $entity_resource->patchIndividual($resource_type, $node_type, $parsed_node_type, $request);
......@@ -189,7 +202,13 @@ class EntityResourceTest extends KernelTestBase {
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('renderer'),
$this->container->get('entity.repository'),
$this->container->get('jsonapi.include_resolver')
$this->container->get('jsonapi.include_resolver'),
new EntityAccessChecker(
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('router.no_access_checks'),
$this->container->get('current_user'),
$this->container->get('entity.repository')
)
);
$response = $entity_resource->deleteIndividual($node_type, new Request());
// As a side effect, the node will also be deleted.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment