openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [PHP] Object of class stdClass could not be converted to string

Open michabbb opened this issue 6 years ago • 2 comments

Bug Report Checklist

Description

generating the php classes of these open-api definitions: https://developer.ebay.com/api-docs/sell/analytics/overview.html

leads to a problem i have in ObjectSerializer.php
where i get the error:

Object of class stdClass could not be converted to string

openapi-generator version

tested: 3.3.4 (php5) and 4.3.1 (php7)

OpenAPI declaration file content or url

https://developer.ebay.com/api-docs/master/sell/analytics/openapi/3/sell_analytics_v1_oas3.yaml https://developer.ebay.com/api-docs/master/sell/analytics/openapi/3/sell_analytics_v1_oas3.json

Command line used for generation

generate -i /app/sell_analytics_v1_oas3.yaml -g php -o /app/ --config /app/openapi-config.php.ebay.analytics.json

Steps to reproduce

testing this code:

php7: https://github.com/michabbb/sdk-ebay-rest-analytics php5: https://github.com/michabbb/sdk-ebay-rest-analytics/tree/php5

for real testing, you need an eBay oauth code, then you can use the generated code like that:

$config = Configuration::getDefaultConfiguration()->setAccessToken($ebay_access_token);
        $apiInstance = new SellerStandardsProfileApi(null,$config);
        try {
            $result = $apiInstance->findSellerStandardsProfiles();
            +d($result);
        } catch (\Exception $e) {
            echo 'Exception when calling CustomerServiceMetricApi->getCustomerServiceMetric: ', $e->getMessage(), PHP_EOL;
        }

here you can see an example API result of findSellerStandardsProfiles

Suggest a fix

in ObjectSerializer.php line 299 where this code is in:

} elseif (in_array($class, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
            settype($data, $class);
            return $data;
        }

i have to change:

} elseif (in_array($class, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
            if ($data instanceof \stdClass) {
                settype($data,'array');
            } else {
                settype($data, $class);
            }
            return $data;
        }

that is a hack not a fix... i know... i don´t know whats wrong here.
thanks for any help!!!

michabbb avatar Jun 10 '20 22:06 michabbb

I just ran into this issue and @michabbb's fix worked for me

ryross avatar Mar 13 '23 17:03 ryross

Similar problem here. It looks like the code generator assumes properties with the name "date" have the type date-string.

MR4online avatar Mar 28 '25 19:03 MR4online