OpenAPITools / OpenAPITools/openapi-generator

[BUG] requestBody parameter must be omitted in parameters list of method in Python code generation

Open
#6,039 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
Description

Bug in the python code generator. Method signature must not have any parameters if requestBody is required.

Generated Source

def create_resource(resource_spec):  # noqa: E501
    """Create a new resource

    Create a new Resource  # noqa: E501

    :param resource_spec: 
    :type resource_spec: dict | bytes

    :rtype: resource_spec
    """
    if connexion.request.is_json:
        resource_spec = ResourceSpec.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
openapi-generator version

openapitools/openapi-generator-cli:latest

    "Labels": {
      "org.opencontainers.image.created": "2020-03-30T12:48:48Z",
      "org.opencontainers.image.revision": "dd9d1dc0473c084d0091c1011676cf23c057bde8",
      "org.opencontainers.image.title": "openapi-generator-cli",
      "org.opencontainers.image.version": ""
    }
OpenAPI declaration file content or url
openapi: '3.0.2'
info:
  title: lt-frontend
  version: '0.0.1'
  description: RESTful API Service to Start and Stop Load Tests templated as HELM charts
servers:
  - url: https://{server}/{apiRoot}
    variables:
      server:
        default: localhost
      apiRoot:
        default: v1
components:
  schemas:
    ResourceSpec:
      type: object
      required:
        - name
      properties:
        name:
          type: string
paths:
  /resource:
    post:
      operationId: createResource
      description: |-
        Create a new resource. 
      summary: Create a new Resource
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref : '#/components/schemas/ResourceSpec'
      responses:
        '202':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResourceSpec'
Command line used for generation

I generate the server using this command:

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/src/api/frontend-api.yml  \
    -g python-flask \
    --enable-post-process-file \
    -o /local/src/python/frontend 
Steps to reproduce

Start server

FLASK_ENV=development python3 -m openapi_server

Try to create the resource

curl -X POST "http://127.0.0.1:8080/v1/resource" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{\"name\":\"test\"}"

Last item in stack trace in console


    File "/.venv/lib/python3.8/site-packages/connexion/decorators/parameter.py", line 121, in wrapper

     

            # attempt to provide the request context to the function

            if pass_context_arg_name and (has_kwargs or pass_context_arg_name in arguments):

                kwargs[pass_context_arg_name] = request.context

     

            return function(**kwargs)

     

        return wrapper

    TypeError: create_test_job() missing 1 required positional argument: 'report_spec'
Related issues/PRs
Suggest a fix

Workaround Fixed Generated Source
Just remove the parameter from the method signature

def create_resource():  # noqa: E501
    """Create a new resource

    Create a new Resource  # noqa: E501

    :param resource_spec: 
    :type resource_spec: dict | bytes

    :rtype: resource_spec
    """
    if connexion.request.is_json:
        resource_spec = ResourceSpec.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'

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

Start by generating the provided OpenAPI declaration with the python-flask generator using the documented Docker command, then inspect the generated create_resource entry point and reproduce the request. Compare the generated method signature with the required requestBody behavior; done means the request body is handled without requiring a matching function parameter and the curl request no longer triggers the missing-argument error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.