Overview

It's currently possible to create a JavaScript component config entity with an invalid example string prop value by using an empty string.

An error was reported that happened at cache clear, but there might be other cases where this causes issues:

In StaticPropSource.php line 242:

[LogicException]
Drupal\canvas\PropSource\StaticPropSource::withValue called with invalid value for field type field_item:string.

component.yml shape to reproduce the issue:

name: Empty string repro
machineName: empty_string_repro
props:
  properties:
    delta:
      type: string
      title: Delta
      examples:
        - ''

Proposed resolution

User interface changes

n/a

Issue fork canvas-3587211

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

balintbrews created an issue. See original summary.

wim leers’s picture

wim leers’s picture

longwave made their first commit to this issue’s fork.

longwave’s picture

Assigned: wim leers » Unassigned
Status: Active » Needs review
Issue tags: -Needs tests

I thought this would end up in the same place as #3586958: Relative image paths are incorrectly allowed as example image prop values in SDCs and code component example values, but it's not quite so can be committed separately.

penyaskito’s picture

I fail to remember why an empty example is not valid iff the prop is not required.

wim leers’s picture

#7: Because:

  1. Conceptually: how is an empty string an actual example value? Why even provide that as an example?
  2. Field types will actually filter away empty values, see \Drupal\Core\Field\FieldItemListInterface::filterEmptyItems() + \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). For example \Drupal\Core\Field\Plugin\Field\FieldType\UriItem::isEmpty() does:
      public function isEmpty() {
        $value = $this->getValue();
        if (!isset($value['value']) || $value['value'] === '') {
          return TRUE;
        }
        return parent::isEmpty();
      }
    

    … which is directly leading to the exception this issue reported.

wim leers’s picture

#7: I suspect you're thinking of "empty string is not a valid string enum value" which you worked on.

          // The empty string is not a sensible enum value. To indicate
          // optionality, the prop should be made optional.
wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Parent issue: » #3520484: [META] Production-ready ComponentSource plugins
penyaskito’s picture

I definitely had #9 in mind, I failed to see why I wouldn't accept an empty string for a regular string.
So in this case is a core limitation.


Re: Conceptually: I disagree. An empty string would be a good example for reenforcing that it's an optional prop. The problem is that we are using the first one. Wouldn't be an issue if we took the first-not-empty-one as default or if #3514672: [Needs design] DX & authoring experience: support `default` in addition to `examples[0]`, enables updating a component input across all existing instances was completed.

penyaskito’s picture

+1 to RTBC btw. #11 is out of scope here.

wim leers’s picture

#11:

An empty string would be a good example for reenforcing that it's an optional prop.

You're looking at it from a Canvas end user/Content Author POV. You should look at it here from a component developer POV, because that's the person who is now going to be told to do something differently.

When a SDC or code component prop is marked as optional, it's optional from a JSON Schema POV. Which means "it can be omitted". The absence of a value is not the same as a value considered empty. Which value would you consider "empty" for type: integer? And which one for type: string, format: uri?

Therefore this is IMHO a slippery slope that will lead to more confusion. I disagree with allowing '' as a valid example value.

IOW: this is about code/metadata hygiene. And Canvas' ability to reason about the JSON schema for a given component. This MR has zero impact on end users.

penyaskito’s picture

The absence of a value is not the same as a value considered empty.

True that.

  • wim leers committed 1369625b on 1.x authored by longwave
    fix(Component sources): #3587211 Empty strings are incorrectly allowed...
wim leers’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.