Islandora / jsonld

JSON-LD Serializer for Drupal and Islandora
GNU General Public License v2.0
9 stars 19 forks source link

[BUG] Undefined index: @id in deduplicateArrayOfIds() #61

Closed seth-shaw-unlv closed 2 years ago

seth-shaw-unlv commented 2 years ago

I've started getting the following errors popping up in my error queue:

Notice: Undefined index: @id in Drupal\jsonld\Normalizer\NormalizerBase::deduplicateArrayOfIds() (line 110 of /data/www/special/drupal/modules/contrib/jsonld/src/Normalizer/NormalizerBase.php)
#0 /data/www/special/drupal/core/includes/bootstrap.inc(346): _drupal_error_handler_real()
#1 /data/www/special/drupal/modules/contrib/jsonld/src/Normalizer/NormalizerBase.php(110): _drupal_error_handler()
#2 /data/www/special/drupal/modules/contrib/jsonld/src/Normalizer/NormalizerBase.php(86): Drupal\jsonld\Normalizer\NormalizerBase::deduplicateArrayOfIds()
#3 /data/www/special/drupal/modules/contrib/jsonld/src/Normalizer/ContentEntityNormalizer.php(174): Drupal\jsonld\Normalizer\NormalizerBase::deduplicateTypesAndReferences()
#4 /data/www/special/drupal/vendor/symfony/serializer/Serializer.php(153): Drupal\jsonld\Normalizer\ContentEntityNormalizer->normalize()
#5 /data/www/special/drupal/vendor/symfony/serializer/Serializer.php(126): Symfony\Component\Serializer\Serializer->normalize()
#6 /data/www/special/drupal/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php(160): Symfony\Component\Serializer\Serializer->serialize()
#7 /data/www/special/drupal/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php(75): Drupal\rest\EventSubscriber\ResourceResponseSubscriber->renderResponseBody()
#8 [internal function]: Drupal\rest\EventSubscriber\ResourceResponseSubscriber->onResponse()
#9 /data/www/special/drupal/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func()
#10 /data/www/special/drupal/vendor/symfony/http-kernel/HttpKernel.php(191): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
#11 /data/www/special/drupal/vendor/symfony/http-kernel/HttpKernel.php(179): Symfony\Component\HttpKernel\HttpKernel->filterResponse()
#12 /data/www/special/drupal/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#13 /data/www/special/drupal/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#14 /data/www/special/drupal/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#15 /data/www/special/drupal/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#16 /data/www/special/drupal/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#17 /data/www/special/drupal/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#18 /data/www/special/drupal/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#19 /data/www/special/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#20 /data/www/special/drupal/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle()
#21 /data/www/special/drupal/index.php(19): Drupal\Core\DrupalKernel->handle()
#22 {main}
.

I'm not sure what it is complaining about, because none of the referenced objects in the JSON-LD that triggered it are missing an @id property:

{
  "@graph": [
    {
      "@id": "https://special.library.unlv.edu/node/338459",
      "@type": [
        "http://pcdm.org/models#Object",
        "http://purl.org/coar/resource_type/c_c513"
      ],
      "http://purl.org/dc/terms/title": [
        {
          "@value": "Photographs of Highland Girl Scout Cookout, Foster Parent's home opening, Reverend Walker's church, adult education graduation at Madison, Las Vegas (Nev.), 1967",
          "@language": "en"
        }
      ],
      "http://schema.org/dateCreated": [
        {
          "@value": "2020-11-09T21:56:37+00:00",
          "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
        }
      ],
      "http://schema.org/dateModified": [
        {
          "@value": "2022-06-06T20:45:58+00:00",
          "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
        }
      ],
      "http://purl.org/dc/terms/description": [
        {
          "@value": "<p>Unidentified men at the grand opening of Foster Parents Home. Two men shaking hands in front of a church. A graduation ceremony.</p>\r\n",
          "@language": "en"
        }
      ],
      "http://purl.org/dc/terms/isPartOf": [
        {
          "@id": "https://special.library.unlv.edu/node/297751"
        }
      ],
      "http://purl.org/dc/terms/identifier": [
        {
          "@id": "http://n2t.net/ark:/62930/d1dj58n38"
        },
        {
          "@value": "pho022087",
          "@type": "http://www.w3.org/2001/XMLSchema#string"
        }
      ],
      "http://schema.org/sameAs": [
        {
          "@id": "http://n2t.net/ark:/62930/d1dj58n38"
        },
        {
          "@id": "https://special.library.unlv.edu/node/338459"
        }
      ],
      "http://purl.org/dc/terms/bibliographicCitation": [
        {
          "@value": "<p>pho022087. Clinton Wright Photographic Negatives Collection. PH-00379. Special Collections and Archives, University Libraries, University of Nevada, Las Vegas. Las Vegas, Nevada. http://n2t.net/ark:/62930/d1dj58n38.</p>\r\n",
          "@type": "http://www.w3.org/2001/XMLSchema#string"
        }
      ],
      "http://purl.org/dc/terms/creator": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/22388"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/22388"
        }
      ],
      "http://purl.org/dc/terms/rights": [
        {
          "@value": "This material is made available to facilitate private study, scholarship, or research. It may be protected by copyright, trademark, privacy, publicity rights, or other interests not owned by UNLV. Users are responsible for determining whether permissions are necessary from rights owners for any intended use and for obtaining all required permissions. Acknowledgement of the UNLV University Libraries is requested. For more information, please see the UNLV Special Collections policies on reproduction and use (https://www.library.unlv.edu/speccol/research_and_services/reproductions) or contact us at special.collections@unlv.edu.",
          "@type": "http://www.w3.org/2001/XMLSchema#string"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/12267"
        }
      ],
      "http://purl.org/dc/terms/provenance": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/638"
        }
      ],
      "http://purl.org/dc/terms/date": [
        {
          "@value": "1967",
          "@type": "http://www.w3.org/2001/XMLSchema#string"
        },
        {
          "@value": "1967",
          "@type": "http://www.w3.org/2001/XMLSchema#gYear"
        }
      ],
      "http://purl.org/dc/terms/language": [
        {
          "@value": "en",
          "@type": "http://www.w3.org/2001/XMLSchema#string"
        }
      ],
      "http://purl.org/dc/terms/spatial": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/1010"
        }
      ],
      "http://purl.org/dc/terms/medium": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/114"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/115"
        }
      ],
      "http://purl.org/dc/terms/format": [
        {
          "@value": "image/tiff",
          "@type": "http://www.w3.org/2001/XMLSchema#string",
          "@container": "@list"
        }
      ],
      "http://purl.org/dc/terms/publisher": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/117"
        }
      ],
      "http://purl.org/dc/terms/type": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/107"
        }
      ],
      "http://purl.org/dc/terms/source": [
        {
          "@id": "https://special.library.unlv.edu/node/311097"
        }
      ],
      "http://purl.org/dc/terms/subject": [
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/12271"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/22008"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/155"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/20534"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/647"
        },
        {
          "@id": "https://special.library.unlv.edu/taxonomy/term/20466"
        }
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/node/297751",
      "@type": [
        "http://schema.org/ArchiveComponent",
        "http://schema.org/Collection"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/22388",
      "@type": [
        "http://schema.org/Person"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/638",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/1010",
      "@type": [
        "http://purl.org/dc/terms/Location",
        "http://schema.org/Place"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/114",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/115",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/117",
      "@type": [
        "http://schema.org/Organization"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/107",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/node/311097",
      "@type": [
        "http://schema.org/ArchiveComponent"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/12267",
      "@type": "https://special.library.unlv.edu/rest/type/taxonomy_term/standardized_rights_statement"
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/12271",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/22008",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/155",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/20534",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/647",
      "@type": [
        "http://schema.org/Thing"
      ]
    },
    {
      "@id": "https://special.library.unlv.edu/taxonomy/term/20466",
      "@type": [
        "http://schema.org/Thing"
      ]
    }
  ]
}
whikloj commented 2 years ago

This is a bad assumption on my part that if the first array (in a set of arrays) has an element with key @id then they all would. Instead I probably need to check for the key and if it doesn't exist then include that value too.

seth-shaw-unlv commented 2 years ago

@whikloj, something like this?

  private static function deduplicateArrayOfIds(array $array): array {
    $temp_array = [];
    foreach ($array as $val) {
      if (! array_key_exists('@id', $val) || array_search($val['@id'], array_column($temp_array, '@id')) === FALSE) {
        $temp_array[] = $val;
      }
    }
    return $temp_array;
  }
seth-shaw-unlv commented 2 years ago

Resolved with https://github.com/Islandora/jsonld/commit/3686e943193b541dbd8e0b36f4f94fdc0b273189