Swagger codegen - swift 3忽略了params

时间:2017-07-11 10:03:52

标签: swift3 http-headers swagger swagger-codegen

Swagger Codegen中的标题参数存在问题。我的RAML文件看起来像这样:

---...---
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
--- ....---- 

以后

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

直到这里一切都很好,验证还可以,参数效果很好。 但...

当我在swift(1,2或3)中导出它时出现问题:

 /**
     - 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)
    }

没有更多标头参数!

我尝试用PHP和打字稿导出它,参数在这里!!!!! :

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;
        }
    }

为什么CODEGEN失败?????请帮忙。有没有办法强制生成代码?

0 个答案:

没有答案