All credit goes to @effulgentsia for this brilliant idea and proof of concept.

We both pair programmed on this patch.

This will eliminate a lot of complexity in both modules, at the cost of a very dirty trick.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gabesullice created an issue. See original summary.

gabesullice’s picture

Status: Active » Needs review
FileSize
22.11 KB
gabesullice’s picture

Fixing a CS violation.

Wim Leers’s picture

Wow, really impressive work here!

Of course, the person to review this, is @e0ipso.

  1. +++ b/jsonapi_extras.services.yml
    @@ -1,39 +1,33 @@
    +    decorates: serializer.normalizer.field_item.jsonapi
    ...
    +    decorates: serializer.normalizer.resource_identifier.jsonapi
    

    👏 Decoration FTW!

  2. +++ b/src/JsonapiExtrasServiceProvider.php
    @@ -28,37 +28,21 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase {
    +    // Enable normalizers in the "src-impostor-normalizers" directory to be
    +    // within the \Drupal\jsonapi\Normalizer namespace in order to circumvent
    +    // the encapsulation enforced by
    +    // \Drupal\jsonapi\Serializer\Serializer::__construct().
    +    $container_namespaces = $container->getParameter('container.namespaces');
    +    $container_modules = $container->getParameter('container.modules');
    +    $container_namespaces['Drupal\jsonapi\Normalizer\ImpostorFrom\jsonapi_extras'][] = dirname($container_modules['jsonapi_extras']['pathname']) . '/src-impostor-normalizers';
    +    $container->setParameter('container.namespaces', $container_namespaces);
    

    😮 → 🤔 → 🥳

  3. +++ b/src/Normalizer/JsonApiNormalizerDecoratorBase.php
    similarity index 50%
    rename from src/Normalizer/RelationshipItemNormalizer.php
    
    rename from src/Normalizer/RelationshipItemNormalizer.php
    rename to src/Normalizer/ResourceIdentifierNormalizer.php
    

    This rename should've already happened before, it just hasn't yet. No huge deal, but if you don't know that this is simply a "forgotten rename", then this is of course very confusing.

effulgentsia’s picture

+++ b/src/JsonapiExtrasServiceProvider.php
@@ -28,37 +28,21 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase {
+    $container_namespaces['Drupal\jsonapi\Normalizer\ImpostorFrom\jsonapi_extras'][] = dirname($container_modules['jsonapi_extras']['pathname']) . '/src-impostor-normalizers';

I think we can remove the [] from here. It's a remnant from earlier work that I did on this where I was adding to the Drupal\jsonapi namespace rather than defining a more specific namespace.

e0ipso’s picture

I'll be looking into this soon.

e0ipso’s picture

This is a neat trick! I don't see any big issues that prevent from merging, then refining if necessary.

However, it makes me doubt the effectiveness of the prevention measures we put in JSON:API. I think this all rely on hopefully people won't notice that you can do it like this.

I will re-roll de patch and then merge.

e0ipso’s picture

Kicking tests. No interdiff because re-roll.

Status: Needs review » Needs work

The last submitted patch, 8: 3036904--poison-namespaces-allow-serializer--8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Wim Leers’s picture

Title: Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior. » [PP-1] Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior.
Status: Needs work » Needs review
FileSize
777 bytes
22.99 KB

There's two failures in #8:

  1. ::testWrite() fails because as of JSON:API 2.4, "read-only mode" is turned on by default: #3039568: Add a read-only mode to JSON:API
  2. jsonapi.link_manager no longer exists.

Attached is the fix for the first failure. For the second failure, we should commit #3035045: Make EntityToJsonApi use subrequests so that it never breaks again. Marking this issue as postponed on that.

e0ipso’s picture

Title: [PP-1] Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior. » Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior.
Wim Leers’s picture

Title: Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior. » [PP-1] Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior.
Status: Needs review » Postponed
Wim Leers’s picture

Wim Leers’s picture

Title: [PP-1] Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior. » Decorate JSON:API's normalizers and fake its namespace so JSON:API can eliminate its Serializer's fallback behavior.
Status: Postponed » Needs review
FileSize
22.24 KB
e0ipso’s picture

You beat me to it @Wim Leers!

@Wim Leers++

  • e0ipso committed 6b5ea57 on 8.x-3.x authored by gabesullice
    Issue #3036904 by Wim Leers, gabesullice, e0ipso, effulgentsia: Decorate...
e0ipso’s picture

Status: Needs review » Fixed

Fixed! Thanks all.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.