Skip to content

[Bug]: Inferred data wrapping on nested arrays of Laravel Data objects #1270

Description

@ccrosby-super

What happened?

With config('data.wrap') set to 'data', we're seeing Laravel Data objects with nested arrays of Laravel Data objects that have data wrapping in schema, despite their responses not being wrapped. i.e.:

class ProductData extends Data
{
    /**
     * Initialize a new ProductData (plans) instance.
     *
     * @param array<int, PriceData> $prices The collection of prices associated with the product
     */
    public function __construct(
        public readonly array $prices,
    ) {
    }
}

generates schema as:

"prices": {
    "type": "object",
    "properties": {
      "data": {
        "type": "array",

despite the response having prices not wrapped in 'data'.

The outer data wrapper (on ProductData as a whole) is expected.

How to reproduce the bug

Create a Laravel Data object that contains an array of Data Objects within it like my example above with ProductData and PriceData[].

Generate api.json with php artisan scramble:export

Observe schema that wraps PriceData[] in data, and observe responses that do not wrap PriceData[].

Package Version

Scramble PRO: 0.9.15, Scramble: 0.13.42

PHP Version

8.5.2

Laravel Version

13.26.1

Which operating systems does with happen with?

No response

Notes

We have attempted a few workarounds (using my example):

  1. Using array<int, PriceData> and/or PriceData[] in the PHPDoc for the constructor of ProductData
  2. Using @var annotations:
       /**
         * @var PriceData[]
        */
        public readonly array $prices,
  1. Using #[DataCollectionOf(PriceData::class)]
  2. Combinations of the above attempts

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions