Array/Object mismatch in generated stories

Created on 21 February 2024, 10 months ago
Updated 22 February 2024, 10 months ago

Problem/Motivation

Using

  • Drupal 10.2
  • PHP 8.2
  • Core SDC.
  • slc_story_generate
  • governor

When running `drush generate story` if produces output like the following:

title: foo/bar
argTypes:
  image:
    name: Image
    type: object
    table:
      category: Props
      type:
        summary: object

However, the core SDC ComponentValidator (core/modules/sdc/src/Component/ComponentValidator.php) throws an error on that component.

This seems to be due to the fact that the YML-to-JSON interpreter actually returns a string:

object(stdClass)#1762 (5) {
  ["title"]=>
  string(24) "Item with link and image"
  ["image"]=>
  string(163) "{"type":"url","url":"https://designsystem.digital.gov/img/introducing-uswds-2-0/built-to-grow--alt.jpg","alt":"USWDS Card alt text","title":"USWDS Card alt title"}"
  ["url"]=>
  string(11) "my-test-url"
  ["date"]=>
  string(24) "2024-02-04T15:00:00.000Z"
  ["attributes"]=>
  NULL

Which seems accurate, given how JSON casting works in PHP.

Steps to reproduce

Proposed resolution

Cast the item to string instead?

Remaining tasks

Figure out the best solution to the problem.

User interface changes

None,

API changes

None.

Data model changes

Arrays/objects are typed as strings (and possibly annotated) to note the JSON typecasting.

πŸ› Bug report
Status

Closed: duplicate

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

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

Comments & Activities

  • Issue created by @agentrickard
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    Fix typo in title.

  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    I suspect this is a core issue, since the JsonSchema/Validator component says:

    Validates the given data against the schema and returns an object containing the results Both the php object and the schema are supposed to be a result of a json_decode call. The validation works as defined by the schema proposal in http://json-schema.org.

  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    Here's what is being returned from core:

        // Validator::arrayToObjectRecursive stringifies the props using the JSON
        // encoder. Before that happens, we want to validate classes. Once the
        // classes are validated, we remove them as potential problems for the JSON
        // Schema validation.
        [
          $schema,
          $props_raw,
        ] = $this->validateClassProps($schema, $props_raw, $component_id);
        $schema = Validator::arrayToObjectRecursive($schema);
        $props = Validator::arrayToObjectRecursive($props_raw);
    

    $props_raw:

    array(6) {
      ["title"]=>
      string(24) "Item with link and image"
      ["title_suffix"]=>
      string(13) "["foo","bar"]"
      ["image"]=>
      string(163) "{"type":"url","url":"https://designsystem.digital.gov/img/introducing-uswds-2-0/built-to-grow--alt.jpg","alt":"USWDS Card alt text","title":"USWDS Card alt title"}"
      ["url"]=>
      string(11) "my-test-url"
      ["date"]=>
      string(24) "2024-02-04T15:00:00.000Z"
      ["attributes"]=>
      NULL
    }
    

    $props:

    object(stdClass)#1762 (6) {
      ["title"]=>
      string(24) "Item with link and image"
      ["title_suffix"]=>
      string(13) "["foo","bar"]"
      ["image"]=>
      string(163) "{"type":"url","url":"https://designsystem.digital.gov/img/introducing-uswds-2-0/built-to-grow--alt.jpg","alt":"USWDS Card alt text","title":"USWDS Card alt title"}"
      ["url"]=>
      string(11) "my-test-url"
      ["date"]=>
      string(24) "2024-02-04T15:00:00.000Z"
      ["attributes"]=>
      NULL
    }
    

    So it appears that we may need to transform the string to JSON prior to that call.

  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    Just taking notes at this point. This almost works -- the schema now validates, but the $image element no longer renders.

        // Validator::arrayToObjectRecursive stringifies the props using the JSON
        // encoder. Before that happens, we want to validate classes. Once the
        // classes are validated, we remove them as potential problems for the JSON
        // Schema validation.
        [
          $schema,
          $props_raw,
        ] = $this->validateClassProps($schema, $props_raw, $component_id);
    
        foreach ($props_raw as $id => $prop) {
          if (is_string($prop)) {
            $decoded = json_decode($prop);
            if ($decoded !== NULL && $decoded !== $prop) {
              $props_raw[$id] = $decoded;
            }
          }
        }
    
        $schema = Validator::arrayToObjectRecursive($schema);
        $props = Validator::arrayToObjectRecursive($props_raw);
    
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    This may be a duplicate of πŸ› String value found, but an array or an object is required RTBC but it is very difficult to see where the issue should be fixed.

  • Status changed to Closed: duplicate 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
Production build 0.71.5 2024