OpenAPITools / OpenAPITools/openapi-generator

[BUG] Missing imports from generated API client

Open
#5,231 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

Generator failing to include import when an Operation's response type is Map<List<Type>>.

For instance a function that returns Map<List<Option>> fails to include Option as part of the imports and the class becomes unable to compile.

See the yaml below for the input.

The following code is generated. There is a reference to Option in the code, but the import is not included.

/*
 * OpenAPI Test
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


package com.test.resources;

import com.test.ApiCallback;
import com.test.ApiClient;
import com.test.ApiException;
import com.test.ApiResponse;
import com.test.Configuration;
import com.test.Pair;
import com.test.ProgressRequestBody;
import com.test.ProgressResponseBody;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;



import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OptionsApi {
    private ApiClient localVarApiClient;

    public OptionsApi() {
        this(Configuration.getDefaultApiClient());
    }

    public OptionsApi(ApiClient apiClient) {
        this.localVarApiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return localVarApiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.localVarApiClient = apiClient;
    }

    /**
     * Build call for getOptions
     * @param type  (optional)
     * @param _callback Callback for upload/download progress
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 0 </td><td> default response </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call getOptionsCall(List<String> type, final ApiCallback _callback) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/options";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        if (type != null) {
            localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("multi", "type", type));
        }

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        Map<String, String> localVarCookieParams = new HashMap<String, String>();
        Map<String, Object> localVarFormParams = new HashMap<String, Object>();
        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }

        final String[] localVarContentTypes = {
            
        };
        final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] {  };
        return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
    }

    @SuppressWarnings("rawtypes")
    private okhttp3.Call getOptionsValidateBeforeCall(List<String> type, final ApiCallback _callback) throws ApiException {
        

        okhttp3.Call localVarCall = getOptionsCall(type, _callback);
        return localVarCall;

    }

    /**
     * Get available options
     * 
     * @param type  (optional)
     * @return Map&lt;String, List&lt;Option&gt;&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 0 </td><td> default response </td><td>  -  </td></tr>
     </table>
     */
    public Map<String, List<Option>> getOptions(List<String> type) throws ApiException {
        ApiResponse<Map<String, List<Option>>> localVarResp = getOptionsWithHttpInfo(type);
        return localVarResp.getData();
    }

    /**
     * Get available options
     * 
     * @param type  (optional)
     * @return ApiResponse&lt;Map&lt;String, List&lt;Option&gt;&gt;&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 0 </td><td> default response </td><td>  -  </td></tr>
     </table>
     */
    public ApiResponse<Map<String, List<Option>>> getOptionsWithHttpInfo(List<String> type) throws ApiException {
        okhttp3.Call localVarCall = getOptionsValidateBeforeCall(type, null);
        Type localVarReturnType = new TypeToken<Map<String, List<Option>>>(){}.getType();
        return localVarApiClient.execute(localVarCall, localVarReturnType);
    }

    /**
     * Get available options (asynchronously)
     * 
     * @param type  (optional)
     * @param _callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 0 </td><td> default response </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call getOptionsAsync(List<String> type, final ApiCallback<Map<String, List<Option>>> _callback) throws ApiException {

        okhttp3.Call localVarCall = getOptionsValidateBeforeCall(type, _callback);
        Type localVarReturnType = new TypeToken<Map<String, List<Option>>>(){}.getType();
        localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
        return localVarCall;
    }
}

openapi-generator version

4.3.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: OpenAPI Test
  version: 1.0.0
servers:
- url: http://localhost:8080
paths:
  "/options":
    get:
      tags:
      - options
      summary: Get available options
      operationId: getOptions
      parameters:
      - name: type
        in: query
        schema:
          type: array
          items:
            type: string
      responses:
        default:
          description: default response
          content:
            application/json:
              schema:
                type: object
                additionalProperties:
                  type: array
                  items:
                    "$ref": "#/components/schemas/Option"
components:
  schemas:
    Option:
      type: object
      properties:
        value:
          type: string
          readOnly: true
        description:
          type: string
          readOnly: true
Command line used for generation
openapi-generator-cli generate -g kotlin-client -i test.yaml
Steps to reproduce
Related issues/PRs
Suggest a fix

Maybe pass in the Operations imports Set reference to the fromResponse method and add any arbitrary imports as needed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied test.yaml using the kotlin-client command, then inspect DefaultCodegen.java at the linked fromResponse method and how operation imports are collected. Done means the generated Kotlin client imports Option for the Map<String, List> response and compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, openapi
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.