Update method `getAsset` signature with appropriate changes

Created on 5 May 2025, 11 days ago

Problem/Motivation

Currently the method `getAsset()` have incorrect signature which breaks when calling method without `version_id`

Steps to reproduce

Call method without `version_id`

Proposed resolution

Update method signature with following changes:

/**
   * Get asset data from Widen via the "Retrieve by ID" endpoint.
   *
   * @param string $asset_id
   *   The asset ID.
   * @param string|null $version_id
   *   (optional) The asset version ID.
   *
   * @return array|int
   *   The asset data as an array. 404 as a number if the DAM asset is deleted.
   *
   * @throws DamClientException
   * @throws DamConnectException
   * @throws DamServerException
   * @throws GuzzleException
   * @see https://widenv2.docs.apiary.io/#reference/assets/assets/retrieve-by-id
   */
  public function getAsset(string $asset_id, string $version_id = null): array|int {
    $cache_key = 'asset:';
    $raw_cached_data = $this->readFromCache($cache_key . $asset_id);

Remaining tasks

Create MR with require changes.

๐Ÿ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia chandu7929 Pune

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @chandu7929
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vipin.mittal18 Greater Noida
  • Pipeline finished with Canceled
    11 days ago
    #488913
  • Pipeline finished with Failed
    11 days ago
    #488924
  • Pipeline finished with Running
    11 days ago
    #489007
  • Pipeline finished with Canceled
    8 days ago
    Total: 154s
    #492119
  • Pipeline finished with Success
    8 days ago
    Total: 2450s
    #492122
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chandu7929 Pune
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States japerry KVUO

    I'm going to need more detail here. The existing parameter 'version_id' is already optional, so it should not be throwing an error. Is there a backtrace showing where omission of the version_id parameter within getAsset is throwing an error vs using NULL?

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chandu7929 Pune

    Hello @Japerry,

    The optional parameter should be set to null rather than an empty stringโ€”unless there's a specific reason to use an empty string in the method. Using an empty string can cause an error when the function is called without the second parameter, as it expects a value of type string. This results in the following error:

    "Argument #2 ($version_id) must be of type string",

    which is inaccurate in this context.

    Please refer to the error trace from the getAsset() call in media_acquiadam where version_id is not passed.

     [error]  Client error: `GET https://api.widencollective.com/v2/assets/0c772875-c010-47af-bf7e-2a2080587d50/versions?expand=asset_properties,embeds,file_properties,metadata,metadata_info,metadata_vocabulary,security,thumbnails` resulted in a `404 Not Found` response:
    > {
    >   "error": true,
    >   "response_code": 404,
    >   "error_message": "Asset with UUID 0c772875-c010-47af-bf7e-2a2080587d50 not  (truncated...)
    >  [error]  TypeError: Drupal\acquia_dam\Client\AcquiaDamClient::getAsset(): Argument #2 ($version_id) must be of type string, null given, called in /var/www/html/web/modules/contrib/acquia_dam/src/Plugin/media/Source/Asset.php on line 234 in Drupal\acquia_dam\Client\AcquiaDamClient->getAsset() (line 170 of /var/www/html/web/modules/contrib/acquia_dam/src/Client/AcquiaDamClient.php) #0 /var/www/html/web/modules/contrib/acquia_dam/src/Plugin/media/Source/Asset.php(234): Drupal\acquia_dam\Client\AcquiaDamClient->getAsset()
    
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States japerry KVUO

    No, the problem is that
    $asset = $this->clientFactory->getSiteClient()->getAsset($asset_id, $version_id); is passing an NULL, which it shouldn't be doing -- but it appears that public function getFinalizedVersion(string $asset_id): ?string { is returning NULL or string, which it shouldn't be doing -- it should always return a string.

    However, changing that response from NULL|String could cause other issues, so the easiest fix is just change line Asset.php:234 to check null coalesce $asset = $this->clientFactory->getSiteClient()->getAsset($asset_id, $version_id ?? '');

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chandu7929 Pune

    Passing a blank string serves as a workaround to bypass the error. Ideally, we should update the method signature to accept null as an optional parameter. However, it's unclear what the implications of passing null might be, although all tests are currently passing with this change.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States japerry KVUO

    After discussion with mglaman, the original cause of this is fixed in #3524222. Any change to the signature here is a bandaid over the underlying issue, which is $version_id, if set, should actually be a string and not null.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vipin.mittal18 Greater Noida
Production build 0.71.5 2024