Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
who opened this issue Apr 13, 2015 · 1 comment
Closed

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

who opened this issue Apr 13, 2015 · 1 comment

Comments

@who
Copy link
Contributor

who commented Apr 13, 2015

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.

@who
Copy link
Contributor Author

who commented Apr 22, 2015

Closing this, as it's fixed now.

@who who closed this as completed Apr 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant