Unverified Commit 80c415f6 authored by e0ipso's avatar e0ipso Committed by Mateu Aguiló Bosch

Issue #3022475 by e0ipso: Drop support for JSON:API 2.x in JSON:API Extras 2.x

parent f1e6fe2f
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
], ],
"require": { "require": {
"drupal/core": "^8.5", "drupal/core": "^8.5",
"drupal/jsonapi": "^1.22 || ^2.0-rc1", "drupal/jsonapi": "^1.22",
"e0ipso/shaper": "^1" "e0ipso/shaper": "^1"
} }
} }
...@@ -6,20 +6,18 @@ services: ...@@ -6,20 +6,18 @@ services:
- '@entity_type.manager' - '@entity_type.manager'
- '@plugin.manager.resource_field_enhancer' - '@plugin.manager.resource_field_enhancer'
tags: tags:
- { name: normalizer, priority: 1025, format: api_json } - { name: jsonapi_normalizer_do_not_use_removal_imminent, priority: 25, format: api_json }
serializer.normalizer.entity.jsonapi_extras: serializer.normalizer.entity.jsonapi_extras:
class: Drupal\jsonapi_extras\Normalizer\ContentEntityNormalizer class: Drupal\jsonapi_extras\Normalizer\ContentEntityNormalizer
parent: serializer.normalizer.entity.jsonapi parent: serializer.normalizer.entity.jsonapi
calls:
- [setSerializer, ['@jsonapi.serializer_do_not_use_removal_imminent']]
tags: tags:
- { name: normalizer, priority: 1022, format: api_json } - { name: jsonapi_normalizer_do_not_use_removal_imminent, priority: 22, format: api_json }
serializer.normalizer.config_entity.jsonapi_extras: serializer.normalizer.config_entity.jsonapi_extras:
class: Drupal\jsonapi_extras\Normalizer\ConfigEntityNormalizer class: Drupal\jsonapi_extras\Normalizer\ConfigEntityNormalizer
parent: serializer.normalizer.config_entity.jsonapi parent: serializer.normalizer.config_entity.jsonapi
tags: tags:
- { name: normalizer, priority: 1022, format: api_json } - { name: jsonapi_normalizer_do_not_use_removal_imminent, priority: 22, format: api_json }
plugin.manager.resource_field_enhancer: plugin.manager.resource_field_enhancer:
class: Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager class: Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
...@@ -35,9 +33,3 @@ services: ...@@ -35,9 +33,3 @@ services:
jsonapi_extras.entity.to_jsonapi: jsonapi_extras.entity.to_jsonapi:
class: Drupal\jsonapi_extras\EntityToJsonApi class: Drupal\jsonapi_extras\EntityToJsonApi
arguments: ['@jsonapi.serializer_do_not_use_removal_imminent', '@jsonapi.resource_type.repository', '@current_user', '@request_stack', '%jsonapi.base_path%'] arguments: ['@jsonapi.serializer_do_not_use_removal_imminent', '@jsonapi.resource_type.repository', '@current_user', '@request_stack', '%jsonapi.base_path%']
jsonapi.serializer_do_not_use_removal_imminent.jsonapi_extras:
class: \Drupal\jsonapi_extras\SerializerDecorator
public: false
decorates: jsonapi.serializer_do_not_use_removal_imminent
arguments: ['@jsonapi.serializer_do_not_use_removal_imminent.jsonapi_extras.inner']
...@@ -102,31 +102,4 @@ class JsonapiResourceConfig extends ConfigEntityBase { ...@@ -102,31 +102,4 @@ class JsonapiResourceConfig extends ConfigEntityBase {
} }
} }
/**
* Returns a field mapping as expected by JSON API 2.x' ResourceType class.
*
* @see \Drupal\jsonapi\ResourceType\ResourceType::__construct()
*/
public function getFieldMapping() {
$resource_fields = $this->get('resourceFields') ?: [];
$mapping = [];
foreach ($resource_fields as $resource_field) {
$field_name = $resource_field['fieldName'];
if ($resource_field['disabled'] === TRUE) {
$mapping[$field_name] = FALSE;
continue;
}
if (($alias = $resource_field['publicName']) && $alias !== $field_name) {
$mapping[$field_name] = $alias;
continue;
}
$mapping[$field_name] = TRUE;
}
return $mapping;
}
} }
...@@ -4,11 +4,10 @@ namespace Drupal\jsonapi_extras; ...@@ -4,11 +4,10 @@ namespace Drupal\jsonapi_extras;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\jsonapi\JsonApiResource\EntityCollection; use Drupal\jsonapi\Resource\JsonApiDocumentTopLevel;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface; use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\jsonapi\Routing\Routes; use Drupal\jsonapi\Routing\Routes;
use Drupal\jsonapi\Serializer\Serializer; use Drupal\jsonapi\Serializer\Serializer;
use Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
...@@ -58,7 +57,7 @@ class EntityToJsonApi { ...@@ -58,7 +57,7 @@ class EntityToJsonApi {
/** /**
* EntityToJsonApi constructor. * EntityToJsonApi constructor.
* *
* @param \Drupal\jsonapi\Serializer\Serializer|\Drupal\jsonapi_extras\SerializerDecorator $serializer * @param \Symfony\Component\Serializer\SerializerInterface $serializer
* The serializer. * The serializer.
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
* The resource type repository. * The resource type repository.
...@@ -70,7 +69,7 @@ class EntityToJsonApi { ...@@ -70,7 +69,7 @@ class EntityToJsonApi {
* The JSON API base path. * The JSON API base path.
*/ */
public function __construct(SerializerInterface $serializer, ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $current_user, RequestStack $request_stack, $jsonapi_base_path) { public function __construct(SerializerInterface $serializer, ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $current_user, RequestStack $request_stack, $jsonapi_base_path) {
assert($serializer instanceof Serializer || $serializer instanceof SerializerDecorator); assert($serializer instanceof Serializer);
$this->serializer = $serializer; $this->serializer = $serializer;
$this->resourceTypeRepository = $resource_type_repository; $this->resourceTypeRepository = $resource_type_repository;
$this->currentUser = $current_user; $this->currentUser = $current_user;
...@@ -80,9 +79,6 @@ class EntityToJsonApi { ...@@ -80,9 +79,6 @@ class EntityToJsonApi {
assert(isset($jsonapi_base_path[1])); assert(isset($jsonapi_base_path[1]));
assert(substr($jsonapi_base_path, -1) !== '/'); assert(substr($jsonapi_base_path, -1) !== '/');
$this->jsonApiBasePath = $jsonapi_base_path; $this->jsonApiBasePath = $jsonapi_base_path;
$this->classToUse = ConfigurableResourceTypeRepository::isJsonApi2x()
? '\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel'
: '\Drupal\jsonapi\Resource\JsonApiDocumentTopLevel';
} }
/** /**
...@@ -101,9 +97,7 @@ class EntityToJsonApi { ...@@ -101,9 +97,7 @@ class EntityToJsonApi {
foreach ($includes as $field_name) { foreach ($includes as $field_name) {
$referenced_entities = array_merge($referenced_entities, $entity->get($field_name)->referencedEntities()); $referenced_entities = array_merge($referenced_entities, $entity->get($field_name)->referencedEntities());
} }
$document = ConfigurableResourceTypeRepository::isJsonApi2x() $document = new JsonApiDocumentTopLevel($entity);
? new $this->classToUse($entity, new EntityCollection($referenced_entities), [])
: new $this->classToUse($entity);
return $this->serializer->serialize($document, return $this->serializer->serialize($document,
'api_json', 'api_json',
$this->calculateContext($entity, $includes) $this->calculateContext($entity, $includes)
...@@ -126,9 +120,7 @@ class EntityToJsonApi { ...@@ -126,9 +120,7 @@ class EntityToJsonApi {
foreach ($includes as $field_name) { foreach ($includes as $field_name) {
$referenced_entities = array_merge($referenced_entities, $entity->get($field_name)->referencedEntities()); $referenced_entities = array_merge($referenced_entities, $entity->get($field_name)->referencedEntities());
} }
$document = ConfigurableResourceTypeRepository::isJsonApi2x() $document = new JsonApiDocumentTopLevel($entity);
? new $this->classToUse($entity, new EntityCollection($referenced_entities), [])
: new $this->classToUse($entity);
return $this->serializer->normalize($document, return $this->serializer->normalize($document,
'api_json', 'api_json',
$this->calculateContext($entity, $includes) $this->calculateContext($entity, $includes)
......
...@@ -28,24 +28,6 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase { ...@@ -28,24 +28,6 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase {
$definition->addMethodCall('setConfigFactory', [new Reference('config.factory')]); $definition->addMethodCall('setConfigFactory', [new Reference('config.factory')]);
} }
// Make all three of the normalizers that JSON API Extras overrides private
// untagged services, to ensure that JSON API Extras' overrides continue to
// work in JSON API 2.x, using core's @serializer service.
if ($container->has('serializer.normalizer.field_item.jsonapi')) {
$container->getDefinition('serializer.normalizer.field_item.jsonapi')->setPrivate(TRUE)->clearTags();
}
if ($container->has('serializer.normalizer.entity.jsonapi')) {
$container->getDefinition('serializer.normalizer.entity.jsonapi')->setPrivate(TRUE)->clearTags();
}
if ($container->has('serializer.normalizer.config_entity.jsonapi')) {
$container->getDefinition('serializer.normalizer.config_entity.jsonapi')->setPrivate(TRUE)->clearTags();
}
// Break a circular dependency.
// @see \Drupal\jsonapi_extras\SerializerDecorator::lazilyInitialize()
$definition = $container->getDefinition('jsonapi.serializer_do_not_use_removal_imminent');
$definition->removeMethodCall('setFallbackNormalizer');
$settings = BootstrapConfigStorageFactory::get() $settings = BootstrapConfigStorageFactory::get()
->read('jsonapi_extras.settings'); ->read('jsonapi_extras.settings');
......
...@@ -16,19 +16,11 @@ trait EntityNormalizerTrait { ...@@ -16,19 +16,11 @@ trait EntityNormalizerTrait {
* The input data to modify. * The input data to modify.
* @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
* Contains the info about the resource type. * Contains the info about the resource type.
* @param string $format
* (optional) Format from which the given data was extracted. Only required
* for JSON API 2.x.
* @param array $context
* (optional) Options available to the denormalizer. Only required for JSON
* API 2.x.
* *
* @return array * @return array
* The modified input data. * The modified input data.
*
* @todo Make the last 2 args non-optional when JSON API 2.x is required.
*/ */
protected function prepareInput(array $data, ResourceType $resource_type, $format = NULL, array $context = []) { protected function prepareInput(array $data, ResourceType $resource_type) {
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions */ /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions */
$field_storage_definitions = \Drupal::service('entity_field.manager') $field_storage_definitions = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions( ->getFieldStorageDefinitions(
...@@ -70,7 +62,7 @@ trait EntityNormalizerTrait { ...@@ -70,7 +62,7 @@ trait EntityNormalizerTrait {
$data_internal[$internal_name] = $field_value; $data_internal[$internal_name] = $field_value;
} }
return parent::prepareInput($data_internal, $resource_type, $format, $context); return parent::prepareInput($data_internal, $resource_type);
} }
/** /**
......
...@@ -5,7 +5,6 @@ namespace Drupal\jsonapi_extras\ResourceType; ...@@ -5,7 +5,6 @@ namespace Drupal\jsonapi_extras\ResourceType;
use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository; use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
...@@ -64,34 +63,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository { ...@@ -64,34 +63,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
*/ */
protected $resourceConfigs; protected $resourceConfigs;
/**
* Detects whether this site has JSON API 1.x or 2.x installed.
*
* @return bool
* TRUE if JSON API 2.x is installed. FALSE otherwise.
*
* @todo Remove this when JSON API Extras drops support for JSON API 1.x.
*/
public static function isJsonApi2x() {
static $is_2x = NULL;
if ($is_2x !== NULL) {
return $is_2x;
}
$extension = \Drupal::service('extension.list.module')->get('jsonapi');
$version = $extension->info['version'];
if ($version === NULL) {
// When running a git clone of the module we will not get a version number
// here. In this case we resort to checking for a 2.x only service.
$is_2x = \Drupal::hasService('jsonapi.include_resolver');
}
else {
$dependency = ModuleHandler::parseDependency('jsonapi(>= 8.x-2.0-beta1)');
$is_2x = drupal_check_incompatibility($dependency, $version) === NULL;
}
return $is_2x;
}
/** /**
* Injects the entity repository. * Injects the entity repository.
* *
...@@ -124,14 +95,8 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository { ...@@ -124,14 +95,8 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @todo Remove this when JSON API Extras drops support for JSON API 1.x.
*/ */
public function all() { public function all() {
if (static::isJsonApi2x()) {
return parent::all();
}
if (empty($this->all)) { if (empty($this->all)) {
$all = parent::all(); $all = parent::all();
array_walk($all, [$this, 'injectAdditionalServicesToResourceType']); array_walk($all, [$this, 'injectAdditionalServicesToResourceType']);
......
<?php
namespace Drupal\jsonapi_extras;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Drupal\jsonapi\Serializer\Serializer;
/**
* A decorated JSON API serializer, with lazily initialized fallback serializer.
*/
class SerializerDecorator implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface {
/**
* The decorated JSON API serializer service.
*
* @var \Drupal\jsonapi\Serializer\Serializer
*/
protected $decoratedSerializer;
/**
* Whether the lazy dependency has been initialized.
*
* @var bool
*/
protected $isInitialized = FALSE;
/**
* Constructs a SerializerDecorator.
*
* @param \Drupal\jsonapi\Serializer\Serializer $serializer
* The decorated JSON API serializer.
*/
public function __construct(Serializer $serializer) {
$this->decoratedSerializer = $serializer;
}
/**
* Lazily initializes the fallback serializer for the JSON API serializer.
*
* Breaks circular dependency.
*/
protected function lazilyInitialize() {
if (!$this->isInitialized) {
$core_serializer = \Drupal::service('serializer');
$this->decoratedSerializer->setFallbackNormalizer($core_serializer);
$this->isInitialized = TRUE;
}
}
/**
* Relays a method call to the decorated service.
*
* @param string $method_name
* The method to invoke on the decorated serializer.
* @param array $args
* The arguments to pass to the invoked method on the decorated serializer.
*
* @return mixed
* The return value.
*/
protected function relay($method_name, array $args) {
$this->lazilyInitialize();
return call_user_func_array([$this->decoratedSerializer, $method_name], $args);
}
/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function deserialize($data, $type, $format, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function serialize($data, $format, array $context = []) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = NULL) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function supportsEncoding($format) {
return $this->relay(__FUNCTION__, func_get_args());
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = NULL) {
return $this->relay(__FUNCTION__, func_get_args());
}
}
...@@ -8,7 +8,6 @@ use Drupal\Core\Url; ...@@ -8,7 +8,6 @@ use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig; use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType; use Drupal\node\Entity\NodeType;
use Drupal\Tests\jsonapi\Functional\JsonApiFunctionalTestBase; use Drupal\Tests\jsonapi\Functional\JsonApiFunctionalTestBase;
...@@ -182,14 +181,8 @@ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase { ...@@ -182,14 +181,8 @@ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase {
// 13. Test a disabled related resource of single cardinality. // 13. Test a disabled related resource of single cardinality.
$this->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]->uuid() . '/vid'); $this->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]->uuid() . '/vid');
$this->assertSession()->statusCodeEquals(404); $this->assertSession()->statusCodeEquals(404);
$output = Json::decode($this->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]->uuid() . '/relationships/vid')); Json::decode($this->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]->uuid() . '/relationships/vid'));
if (ConfigurableResourceTypeRepository::isJsonApi2x()) { $this->assertSession()->statusCodeEquals(404);
$this->assertSession()->statusCodeEquals(200);
$this->assertSame(NULL, $output['data']);
}
else {
$this->assertSession()->statusCodeEquals(404);
}
// 14. Test a disabled related resource of multiple cardinality. // 14. Test a disabled related resource of multiple cardinality.
$this->tags[1]->vocabs->set(0, 'tags'); $this->tags[1]->vocabs->set(0, 'tags');
...@@ -275,12 +268,7 @@ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase { ...@@ -275,12 +268,7 @@ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase {
$this->assertEquals(201, $response->getStatusCode()); $this->assertEquals(201, $response->getStatusCode());
$this->assertArrayHasKey('internalId', $created_response['data']['attributes']); $this->assertArrayHasKey('internalId', $created_response['data']['attributes']);
$this->assertCount(2, $created_response['data']['relationships']['tags']['data']); $this->assertCount(2, $created_response['data']['relationships']['tags']['data']);
if (ConfigurableResourceTypeRepository::isJsonApi2x()) { $this->assertSame($created_response['data']['links']['self'], $response->getHeader('Location')[0]);
$this->assertSame($created_response['data']['links']['self']['href'], $response->getHeader('Location')[0]);
}
else {
$this->assertSame($created_response['data']['links']['self'], $response->getHeader('Location')[0]);
}
$date = new \DateTime($body['data']['attributes']['timestamp']); $date = new \DateTime($body['data']['attributes']['timestamp']);
$created_node = Node::load($created_response['data']['attributes']['internalId']); $created_node = Node::load($created_response['data']['attributes']['internalId']);
$this->assertSame((int) $date->format('U'), (int) $created_node->get('field_timestamp')->value); $this->assertSame((int) $date->format('U'), (int) $created_node->get('field_timestamp')->value);
......
...@@ -7,8 +7,6 @@ use Drupal\Core\Config\ConfigException; ...@@ -7,8 +7,6 @@ use Drupal\Core\Config\ConfigException;
use Drupal\jsonapi\ResourceType\ResourceType; use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\Controller\EntityResource; use Drupal\jsonapi\Controller\EntityResource;
use Drupal\jsonapi\Resource\JsonApiDocumentTopLevel; use Drupal\jsonapi\Resource\JsonApiDocumentTopLevel;
use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel as JsonApiDocumentTopLevel2;
use Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository;
use Drupal\KernelTests\KernelTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType; use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
...@@ -21,10 +19,6 @@ use Symfony\Component\HttpFoundation\Request; ...@@ -21,10 +19,6 @@ use Symfony\Component\HttpFoundation\Request;
* @group jsonapi_extras * @group jsonapi_extras
* @group legacy * @group legacy
* *
* When upgrading JSON API Extras to work with JSON API 2.x revert to this patch
* instead.
* @see https://www.drupal.org/project/jsonapi_extras/issues/2995804#comment-12752336
*
* @internal * @internal
*/ */
class EntityResourceTest extends KernelTestBase { class EntityResourceTest extends KernelTestBase {
...@@ -67,39 +61,19 @@ class EntityResourceTest extends KernelTestBase { ...@@ -67,39 +61,19 @@ class EntityResourceTest extends KernelTestBase {
->grantPermission('administer content types') ->grantPermission('administer content types')
->save(); ->save();
$resource_type = new ResourceType('node', 'article', NULL); $resource_type = new ResourceType('node', 'article', NULL);
if (ConfigurableResourceTypeRepository::isJsonApi2x()) { $entity_resource = new EntityResource(
$entity_resource = new EntityResource( $resource_type,
$this->container->get('entity_type.manager'), $this->container->get('entity_type.manager'),
$this->container->get('entity_field.manager'), $this->container->get('entity_field.manager'),
$this->container->get('jsonapi.link_manager'), $this->container->get('plugin.manager.field.field_type'),
$this->container->get('jsonapi.resource_type.repository'), $this->container->get('jsonapi.link_manager'),
$this->container->get('renderer'), $this->container->get('jsonapi.resource_type.repository'),
$this->container->get('entity.repository'), $this->container->get('renderer')
$this->container->get('jsonapi.include_resolver'), );
$this->container->get('renderer') $response = $entity_resource->createIndividual($node_type, new Request());
);
$response = $entity_resource->createIndividual($resource_type, $node_type, new Request());
}
else {
$entity_resource = new EntityResource(
$resource_type,
$this->container->get('entity_type.manager'),
$this->container->get('entity_field.manager'),
$this->container->get('plugin.manager.field.field_type'),
$this->container->get('jsonapi.link_manager'),
$this->container->get('jsonapi.resource_type.repository'),
$this->container->get('renderer')
);
$response = $entity_resource->createIndividual($node_type, new Request());
}
// As a side effect, the node type will also be saved. // As a side effect, the node type will also be saved.
$this->assertNotEmpty($node_type->id()); $this->assertNotEmpty($node_type->id());
if (ConfigurableResourceTypeRepository::isJsonApi2x()) { $this->assertInstanceOf(JsonApiDocumentTopLevel::class, $response->getResponseData());
$this->assertInstanceOf(JsonApiDocumentTopLevel2::class, $response->getResponseData());
}
else {
$this->assertInstanceOf(JsonApiDocumentTopLevel::class, $response->getResponseData());
}
$this->assertEquals('test', $response->getResponseData()->getData()->id()); $this->assertEquals('test', $response->getResponseData()->getData()->id());
$this->assertEquals(201, $response->getStatusCode()); $this->assertEquals(201, $response->getStatusCode());
} }
...@@ -135,39 +109,19 @@ class EntityResourceTest extends KernelTestBase { ...@@ -135,39 +109,19 @@ class EntityResourceTest extends KernelTestBase {
$request = new Request([], [], [], [], [], [], $payload); $request = new Request([], [], [], [], [], [], $payload);
$resource_type = new ResourceType('node', 'article', NULL); $resource_type = new ResourceType('node', 'article', NULL);
if (ConfigurableResourceTypeRepository::isJsonApi2x()) { $entity_resource = new EntityResource(
$entity_resource = new EntityResource( $resource_type,
$this->container->get('entity_type.manager'), $this->container->get('entity_type.manager'),