Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #1036
Closed
Open
Issue created Sep 14, 2018 by Administrator@rootContributor

[PHP] array of objects as parameter not supported

Created by: thomasphansen

Description

PHP generator is not able to handle an array of objects as parameter to a path.

This is the code that handles the $httpBody, inside the {{operationId}}Request method, in /modules/openapi-generator/src/main/resources/php/api.mustache:

        // for model (json/xml)
        if (isset($_tempBody)) {
            // $_tempBody is the method argument, if present
            $httpBody = $_tempBody;
            // \stdClass has no __toString(), so we should encode it manually
            if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
                $httpBody = \GuzzleHttp\json_encode($httpBody);
            }
        } elseif (count($formParams) > 0) {
       (...)

As you can see, this code will json_encode strClass objects, relay on the "toString()" method for normal object and simply add the parameter as the body otherwise. But if the parameter is an array of model objects, it will be given to the GuzzleHttp\Psr7\Request constructor, which will refuse it as invalid (the http body must be a string, of course).

Running json_encode on the array is useless, since all the properties inside the model object are protected.

I suggest using a recursive method to properly convert $httpBody to json. I'm creating a PR implementing this fix.

openapi-generator version

master (3.3.0)

OpenAPI declaration file content or url

This is a snippet from lines 3000-3013 of the yaml definition I'm working with. Please refer to the Gist link below, for checking the entire file.

        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "JSON representing a list of new object to be created. Should not have ID and version set.",
            "required": false,
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Customer"
              }
            }
          }
        ],

tripletex.yaml

Command line used for generation
docker run \
        --user $(id -u):$(id -g) \
        --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.0.0 generate \
        -i https://tripletex.no/v2/swagger.json \
        -g php \
        -o /local \
        -c /local/config.json
Steps to reproduce

try to use the method postList() from the generated CustomerApi

Related issues/PRs
Suggest a fix/enhancement

See PR #1037

Assignee
Assign to
Time tracking