0

There is a problem with header params in Swagger Codegen. my RAML file looks like that :

---...---
parameters:
  timeParam_begin:
    in: header
    name: timeframe_begin
    type: string
  timeParam_end:
    name: timeframe_end
    in: header
    type: string
  recommendationsParam:
    name: recommendations
    in: header
    default: 3
    type: number
--- ....---- 

and later

---...----
    post:
      summary: Search for possible coordinates given an address
      parameters:
        - $ref: '#/parameters/timeParam_begin'
        - $ref: '#/parameters/timeParam_end'
        - $ref: '#/parameters/recommendationsParam'
---...----

Until here EVERYTHING IS GOOD, the validation is ok and the parameters works well. But...

when I export it in swift (1,2 or3) there is a problem :

 /**
     - POST /tasks
     - Add 'Task' object. 
     - examples: [{contentType=application/json, example="Task added succesfully"}]

     - parameter task: (body) task object 

     - returns: RequestBuilder<String> 
     */
    open class func tasksPostWithRequestBuilder(task: Task) -> RequestBuilder<String> {
        let path = "/tasks"
        let URLString = SwaggerClientAPI.basePath + path
        let parameters = task.encodeToJSON() as? [String:AnyObject]

        let convertedParameters = APIHelper.convertBoolToString(parameters)

        let requestBuilder: RequestBuilder<String>.Type = SwaggerClientAPI.requestBuilderFactory.getBuilder()

        return requestBuilder.init(method: "POST", URLString: URLString, parameters: convertedParameters, isBody: true)
    }

No more headers parameters !

I try to export it in PHP and typescript, the parameters are here !!!!! :

Typescript Angular2 :

 /**
     * 
     * Add &#39;Task&#39; object. 
     * @param task task object
     * @param timeframeBegin 
     * @param timeframeEnd 
     * @param recommendations 
     */
    public tasksPostWithHttpInfo(task: models.Task, timeframeBegin?: string, timeframeEnd?: string, recommendations?: number, extraHttpRequestParams?: any): Observable<Response> {
        const path = this.basePath + `/tasks`;

        let queryParameters = new URLSearchParams();
        let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
        // verify required parameter 'task' is not null or undefined
        if (task === null || task === undefined) {
            throw new Error('Required parameter task was null or undefined when calling tasksPost.');
        }


        // to determine the Content-Type header
        let consumes: string[] = [
            'application/json'
        ];

        // to determine the Accept header
        let produces: string[] = [
            'application/x-www-form-urlencoded'
        ];



        headers.set('Content-Type', 'application/json');


        let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: RequestMethod.Post,
            headers: headers,
            body: task == null ? '' : JSON.stringify(task), // https://github.com/angular/angular/issues/10612
            search: queryParameters
        });

        // https://github.com/swagger-api/swagger-codegen/issues/4037
        if (extraHttpRequestParams) {
            requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
        }

        return this.http.request(path, requestOptions);
    }

PHP :

/**
     * Operation tasksPostWithHttpInfo
     *
     * 
     *
     * @param \Swagger\Client\Model\Task $task task object (required)
     * @param string $timeframe_begin  (optional)
     * @param string $timeframe_end  (optional)
     * @param float $recommendations  (optional, default to 3.0)
     * @throws \Swagger\Client\ApiException on non-2xx response
     * @return array of string, HTTP status code, HTTP response headers (array of strings)
     */
    public function tasksPostWithHttpInfo($task, $timeframe_begin = null, $timeframe_end = null, $recommendations = null)
    {
        // verify the required parameter 'task' is set
        if ($task === null) {
            throw new \InvalidArgumentException('Missing the required parameter $task when calling tasksPost');
        }
        // parse inputs
        $resourcePath = "/tasks";
        $httpBody = '';
        $queryParams = [];
        $headerParams = [];
        $formParams = [];
        $_header_accept = $this->apiClient->selectHeaderAccept(['application/x-www-form-urlencoded']);
        if (!is_null($_header_accept)) {
            $headerParams['Accept'] = $_header_accept;
        }
        $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(['application/json']);

        // header params
        if ($timeframe_begin !== null) {
            $headerParams['timeframe_begin'] = $this->apiClient->getSerializer()->toHeaderValue($timeframe_begin);
        }
        // header params
        if ($timeframe_end !== null) {
            $headerParams['timeframe_end'] = $this->apiClient->getSerializer()->toHeaderValue($timeframe_end);
        }
        // header params
        if ($recommendations !== null) {
            $headerParams['recommendations'] = $this->apiClient->getSerializer()->toHeaderValue($recommendations);
        }
        // default format to json
        $resourcePath = str_replace("{format}", "json", $resourcePath);

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

        // for model (json/xml)
        if (isset($_tempBody)) {
            $httpBody = $_tempBody; // $_tempBody is the method argument, if present
        } elseif (count($formParams) > 0) {
            $httpBody = $formParams; // for HTTP post (form)
        }
        // make the API Call
        try {
            list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
                $resourcePath,
                'POST',
                $queryParams,
                $httpBody,
                $headerParams,
                'string',
                '/tasks'
            );

            return [$this->apiClient->getSerializer()->deserialize($response, 'string', $httpHeader), $statusCode, $httpHeader];
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'string', $e->getResponseHeaders());
                    $e->setResponseObject($data);
                    break;
                case 500:
                    $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'string', $e->getResponseHeaders());
                    $e->setResponseObject($data);
                    break;
            }

            throw $e;
        }
    }

WHY CODEGEN FAILS ????? please help. is there a way to force the generation of code ?

xeonarno
  • 15
  • 7
  • Which version of codegen are you using? Your example works fine for me in http://editor.swagger.io, and the generated Swift 3 client contains `open class func postPostWithRequestBuilder(task: Task, timeframeBegin: String? = nil, timeframeEnd: String? = nil, recommendations: Double? = nil) -> RequestBuilder` – Helen Jul 11 '17 at 13:57
  • Yes, in editor.swagger.io it works but not the good way to for other reason. The only answer that codegen version 2.2.3 – xeonarno Aug 04 '17 at 02:55

0 Answers0