Skip to content

Event::constructFrom with empty data->previous_attributes creates array instead of StripeObject #1785

@msbit

Description

@msbit

Describe the bug

Passing an event payload like so:

[
    'data' => [
        'previous_attributes' => [],
    ],  
    'object' => 'event',
]

to Event::constructFrom will create an Event whose event->data->previous_attributes member is an array, whereas passing an event payload like so:

[
    'data' => [
        'previous_attributes' => [
            'foo' => 'bar',
        ],
    ],  
    'object' => 'event',
]

will create an Event whose event->data->previous_attributes member is a StripeObject.

It feels like this should be consistent, with this member being a StripeObject in both cases.

To Reproduce

assert(Stripe\Event::constructFrom(['data'=>['previous_attributes'=>[]],'object'=>'event'])->data->previous_attributes instanceof Stripe\StripeObject);

Expected behavior

The assertion to be upheld.

Code snippets

<?php

use PHPUnit\Framework\TestCase;

use Stripe\Event;
use Stripe\StripeObject;

class Test extends TestCase
{
    /** 
     * @dataProvider previousAttributesProvider
     */
    function test_constructFrom(array $previousAttributes): void
    {   
        $event = Event::constructFrom([
            'data' => [
                'previous_attributes' => $previousAttributes,
            ],  
            'object' => 'event',
        ]); 

        $this->assertInstanceOf(
            StripeObject::class,
            $event->data->previous_attributes,
        );  
    }   

    function previousAttributesProvider(): iterable
    {   
        yield [['foo' => 'bar']];
        yield [[]];
    }   
}

OS

macOS

PHP version

8.3

Library version

v7.128.0

API version

na

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions