Skip to content

[BUG][PHP] Serializer incorrectly tries to set 'mixed' as a type whilst deserializing. #8943

@jras-gh

Description

@jras-gh

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)

Description

ObjectSerializer::deserialize incorrectly tries to set the type of data to mixed when it's handling a property with mixed type. This results in a warning by the PHP runtime because 'mixed' is not supported by settype.

OpenAPI Generator Version

5.0.0

OpenAPI declaration file content or url

openapi: 3.0.3

info:
    title: Foo
    version: 1.0.0


paths:
    /foo:
        get:
            tags:
                - foo
            operationId: foo
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: "#/components/schemas/Foo"
components:
    schemas:
        Foo:
            type: object
            properties:
                bar: {}

Generation Details

java -jar openapi-generator-cli.jar generate -g php -i foo.yaml -o foo

Steps to reproduce

Run the following script:

<?php

include __DIR__.'/foo/lib/ObjectSerializer.php';
include __DIR__.'/foo/lib/Model/ModelInterface.php';
include __DIR__.'/foo/lib/Model/Foo.php';

use \OpenAPI\Client\ObjectSerializer;
use \OpenAPI\Client\Model\Foo;

$foo      = (new Foo())->setBar([123, '123']);
$foo_json = \json_encode($foo);

echo $foo_json;

// Error will occur trying to deserialize from JSON into Foo.
$footwo = ObjectSerializer::deserialize($foo_json, Foo::class);

var_dump($footwo->getBar());

Will output

~ php test.php                    

{"bar":[123,"123"]}PHP Warning:  settype(): Invalid type in /home/jras/foo/lib/ObjectSerializer.php on line 335
PHP Stack trace:
PHP   1. {main}() /home/jras/test.php:0
PHP   2. OpenAPI\Client\ObjectSerializer::deserialize() /home/jras/test.php:18
PHP   3. OpenAPI\Client\ObjectSerializer::deserialize() /home/jras/foo/lib/ObjectSerializer.php:385
PHP   4. settype() /home/jras/foo/lib/ObjectSerializer.php:335
/home/jras/test.php:20:
array(2) {
  [0] =>
  int(123)
  [1] =>
  string(3) "123"
}

After applying the fix, the warning should be gone, e.g;

~ php test.php

{"bar":[123,"123"]}/home/jras/test.php:20:
array(2) {
  [0] =>
  int(123)
  [1] =>
  string(3) "123"
}

Related issues/PRs

None, that I could find.

Suggest a fix

Modify the default template for the ObjectSerializer to handle 'mixed' data early and let it just return the $data untyped.

As a side note; it seems that languageSpecificPrimitves array contains a lot of "primitives" that arent' really primitives, including but not limited to 'mixed'. This is confusing, but might be a BC-break to change because custom templates may be depending on it..

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions