"Array of empty object." error for array of objects

Created on 5 March 2025, 28 days ago

Problem/Motivation

I have a property with this definition (see original):

    options:
      type: array
      items:
        type: object

This raises the error "Array of empty object."

What is the problem with this? The property needs to be an array of object. Do I need to give a more specific definition, such a the class or an interface?

💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

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

Comments & Activities

  • Issue created by @Liam Morland
  • 🇫🇷France pdureau Paris

    This is an important error to raise in my humble opinion.

    This prop schema doesn't tell enough about the prop and the expected data. It is not useful for the component users and for tools loading the component into Drupal, like UI Patterns 2 or Experience Builder .

    What kind of data is expected in each object item? Which properties ?

    Do I need to give a more specific definition, such a the class or an interface?

    No PHP classes. Let's stick with legit JSON schema ;)

    Do you know what to add?

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    So I suppose I need to use JSON schema to declare the schema of the PHP object.

  • 🇫🇷France pdureau Paris

    Which PHP object are you sending as prop?

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    On further analysis, I think that I should replace type: object with type: array. The PHP type is an associative array. When I do so, it works and sdc_devel raises no issues about it. Thanks for your help.

    It would help to change the error message. It's meaning is not clear to me.

  • 🇫🇷France pdureau Paris

    Hi Liam,

    I am a little worried about this sentence:

    I think that I should replace type: object with type: array (because) the PHP type is an associative array.

    AFAIK, here is the data primitives mapping between the 3 formats we are manipulating:

    So, an associative array seems to be type: object.

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    OK, so how do I resolve the "Array of empty object" message? Do I need to somehow define what the keys are in the associative array?

  • 🇫🇷France pdureau Paris

    Do I need to somehow define what the keys are in the associative array?

    I guess you have to. You need to describe the data you expect, so someone (or something) using your component knows what to inject here.

    So, you had this:

        options:
          type: array
          items:
            type: object
    

    Processed like that in your template:

    {% for option in options %}
          {% if option.type == 'optgroup' %}
            <optgroup label="{{ option.label }}">
              {% for sub_option in option.options %}
                <option value="{{ sub_option.value }}">{{ sub_option.label }}</option>
              {% endfor %}
            </optgroup>
          {% elseif option.type == 'option' %}
            <option value="{{ option.value }}">{{ option.label }}</option>
          {% endif %}
        {% endfor %}
    

    So, your schema may be (non tested proposal):

        options:
          type: array
          items:
            type: object
           properties:
             label: {type: string}
             value: {type: string}
            options: {type: object}
    

    I know there is still an undefined options object but it is already usable like that.

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    Thanks. It seems to be working and sdc_devel raises no errors with it like this:

        options:
          type: array
          items:
            type: object
            properties:
              label: {type: string}
              type: {type: string}
              value: {type: string}
              options:
                type: object
                properties:
                  label: {type: string}
                  value: {type: string}
    
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024