owncloud / libre-graph-api-php

PHP Client for the LibreGraph API
Apache License 2.0
1 stars 0 forks source link

Generated PHP has strange if-statements #1

Open phil-davis opened 1 year ago

phil-davis commented 1 year ago

Example from https://github.com/owncloud/libre-graph-api-php/blob/main/lib/Api/TagsApi.php getTagsWithHttpInfo()

            switch($statusCode) {

                case 200:
                    if ('\OpenAPI\Client\Model\CollectionOfTags' === '\SplFileObject') {
                        $content = $response->getBody(); //stream goes to serializer
                    } else {
                        $content = (string) $response->getBody();
                        if ('\OpenAPI\Client\Model\CollectionOfTags' !== 'string') {
                            $content = json_decode($content);
                        }
                    }

                    return [
                        ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\CollectionOfTags', []),
                        $response->getStatusCode(),
                        $response->getHeaders()
                    ];

                default:
                    if ('\OpenAPI\Client\Model\OdataError' === '\SplFileObject') {
                        $content = $response->getBody(); //stream goes to serializer
                    } else {
                        $content = (string) $response->getBody();
                        if ('\OpenAPI\Client\Model\OdataError' !== 'string') {
                            $content = json_decode($content);
                        }
                    }

                    return [
                        ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\OdataError', []),
                        $response->getStatusCode(),
                        $response->getHeaders()
                    ];

            }

            $returnType = '\OpenAPI\Client\Model\CollectionOfTags';
            if ($returnType === '\SplFileObject') {
                $content = $response->getBody(); //stream goes to serializer
            } else {
                $content = (string) $response->getBody();
                if ($returnType !== 'string') {
                    $content = json_decode($content);
                }
            }

            return [
                ObjectSerializer::deserialize($content, $returnType, []),
                $response->getStatusCode(),
                $response->getHeaders()
            ];

Note: the first PHP was generated by https://github.com/owncloud/libre-graph-api/pull/113

phil-davis commented 1 year ago

A check like this:

if ('\OpenAPI\Client\Model\CollectionOfTags' === '\SplFileObject')

compares 2 string literals. It will never be true.

And all branches of the switch statement return from the function. So the later code starting with

            $returnType = '\OpenAPI\Client\Model\CollectionOfTags';
            if ($returnType === '\SplFileObject') {
...

Is unreachable.

Are there things that need to be tweaked for the code generator?

phil-davis commented 1 year ago

The generated go (for comparison) is in: https://github.com/owncloud/libre-graph-api-go/blob/main/api_tags.go

GetTagsExecute looks OK. It tests for if localVarHTTPResponse.StatusCode >= 300 { to decide if there was a problem. The code does not do any of the "strange" if-statements that are in the PHP.

phil-davis commented 8 months ago

The code is still like this, as at the date of this comment. I suppose that the "happy path" that is always executed is OK. We are not finding problems when using this.