Skip to content

PHP body param of name "body" gets nullified #628

@who

Description

@who

When using a parameter of name "body", the PHP generator nullifies it.

This is caused by variable name collision in the default PHP mustache template.

This Swagger 2.0 schema snippet will repro the bug:

{
...
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "Body object",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Body"
            }
          }
...
}

Resulting generated code:

public function createBody($body) {
    // parse inputs
    $resourcePath = "/bodies";
    $resourcePath = str_replace("{format}", "json", $resourcePath);
    $method = "POST";
    $httpBody = '';
    $queryParams = array();
    $headerParams = array();
    $formParams = array();
    $_header_accept = 'application/json';
    if ($_header_accept !== '') {
        $headerParams['Accept'] = $_header_accept;
    }
    $_header_content_type = array('application/json',);
    $headerParams['Content-Type'] = count($_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';

    // body params
    $body = null;
    if (isset($body)) {
        $body = $body;
    }

    // for model (json/xml)
    if (isset($body)) {
        $httpBody = $body; // $body is the method argument, if present
    }

    // for HTTP post (form)
    if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") > -1) {
        $httpBody = http_build_query($formParams);
    }

    // make the API Call
    $response = $this->apiClient->callAPI($resourcePath, $method,
        $queryParams, $httpBody,
        $headerParams);

    if(! $response) {
        return null;
    }

    $responseObject = $this->apiClient->deserialize($response, 'Body');
    return $responseObject;
}

As you can see, $body = null will wipe out the passed-in value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions