OpenAPITools / OpenAPITools/openapi-generator

[BUG] Installation Code from Generated README Missing Import Statements

Open
#9,929 0 comments 0 reactions 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When working with the generated python client, Installation Code from Generated README are missing the Import Statements "Category" and "Tag" which does not allow that code to compile and run. Only after adding those import statements in, the code is runnable.

openapi-generator version

5.2.0-SNAPSHOT

OpenAPI declaration file content or url

https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml

Generation Details

Using latest openapitools/openapi-generator-online:
Trying to generate a python client

Steps to reproduce

In Cgwin Terminal, run:

docker run -p 8888:8080 --entrypoint java openapitools/openapi-generator-online  -Dio.swagger.parser.util.RemoteUrl.trustAll=true -Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true -jar /generator/openapi-generator-online.jar

In another Cgwin Terminal, run:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"openAPIUrl": "https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml"}' 'http://localhost:8888/api/gen/clients/python'

Get Generated zip file from given URL

Follow Installation Instructions in Readme:

pip install git+https:////.git

Create installation.py in the python client based on README Installation Code:


import time
import openapi_client
from pprint import pprint
from openapi_client.api import pet_api
from openapi_client.model.api_response import ApiResponse
from openapi_client.model.pet import Pet
# Defining the host is optional and defaults to http://petstore.swagger.io/v2
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
    host = "http://petstore.swagger.io/v2"
)

# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure OAuth2 access token for authorization: petstore_auth
configuration = openapi_client.Configuration(
    host = "http://petstore.swagger.io/v2"
)
configuration.access_token = 'YOUR_ACCESS_TOKEN'


# Enter a context with an instance of the API client
with openapi_client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = pet_api.PetApi(api_client)
    pet = Pet(
        id=1,
        category=Category(
            id=1,
            name="CbUUGjjNSwg0_bs9ZayIMrKdgNvb6gvxmPb9GcsM61ate1RA89q3w1l4eH4XxEz.5awLMdeXylwK0lMGUSM4jsrh4dstlnQUN5vVdMLPA",
        ),
        name="doggie",
        photo_urls=[
            "photo_urls_example",
        ],
        tags=[
            Tag(
                id=1,
                name="name_example",
            ),
        ],
        status="available",
    ) # Pet | Pet object that needs to be added to the store

    try:
        # Add a new pet to the store
        api_response = api_instance.add_pet(pet)
        pprint(api_response)
    except openapi_client.ApiException as e:
        print("Exception when calling PetApi->add_pet: %s\n" % e)

Compiler Complains about missing imports:
image

Only runs when These Two lines are Added:

from openapi_client.model.category import Category
from openapi_client.model.tag import Tag
Related issues/PRs
Suggest a fix

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 petstore.yaml specification and the Python client generation steps in the report. Inspect the generated README installation example and the generated Python models it uses; done means the example runs without manually adding the Category and Tag imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.