swagger-api / swagger-api/swagger-codegen

[python] Bug - Duplicated required params when generating python client with parameters in : body

Open
#10,830 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Using parameters/in/body in swagger 2.0 generating python client with swagger-codegen 3.0.22 creates a client with an extra required parameter body and "parameter name".

No matter if you specify one or both parameters, the client call fails.

Swagger-codegen version

3.0.22

Swagger declaration file content or url

https://gist.github.com/albertsola/5dd4d84cfbcb7c45d4585dcde75f26b1

"paths": {
    "/call": {
      "post": {
          "parameters": [
              {
                  "name": "uuid",
                  "in": "body",
                  "required": true,
                  "schema": {
                    "$ref": "#/definitions/RequestObject"
                  }
            }
        ]
    }
}
Command line used for generation

swagger-codegen generate -l python -i bug.json -DpackageName=bug_api -o packages/bug_api

Steps to reproduce
  1. Generate the client
  2. Code generated has what looks like a bug:
def buggy_method(self, body, uuid, **kwargs):  # noqa: E501
    [...]
    if kwargs.get('async_req'):
        return self.buggy_method_with_http_info(body, uuid, **kwargs)  # noqa: E501
    else:
        (data) = self.buggy_method_with_http_info(body, uuid, **kwargs)  # noqa: E501
        return data

def buggy_method_with_http_info(self, body, uuid, **kwargs):  # noqa: E501
    [...]
    if ('body' not in params or
            params['body'] is None):
            raise ValueError("Missing the required parameter `body` when calling `buggy_method`")  # noqa: E501
    # verify the required parameter 'uuid' is set
    if ('uuid' not in params or
            params['uuid'] is None):
            raise ValueError("Missing the required parameter `uuid` when calling `buggy_method`")  # noqa: E501
    [...]
    form_params = []
    local_var_files = {}
    if 'uuid' in params:
        form_params.append(('uuid', params['uuid']))  # noqa: E501

    body_params = None
    if 'body' in params:
        body_params = params['body']
    [...]
    return self.api_client.call_api(
          '/call', 'POST',
          path_params,
          query_params,
          header_params,
          body=body_params,
          post_params=form_params,
          files=local_var_files,
          response_type='ResponseObject',  # noqa: E501
          auth_settings=auth_settings,
          async_req=params.get('async_req'),
          _return_http_data_only=params.get('_return_http_data_only'),
          _preload_content=params.get('_preload_content', True),
          _request_timeout=params.get('_request_timeout'),
          collection_formats=collection_formats)

both body and uuid paramaters are required.

If you use only one parameter (body=None, uuid=valid-uuid) or (body={json}, uuid=None), API client raise a required parameter is missing.

ValueError: Missing the required parameter `body` when calling ...

If you use both:

ValueError: body parameter cannot be used with post_params parameter.
Related issues/PRs

Not found.

Suggest a fix/enhancement

@todo / Open for suggestions.

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 provided Swagger declaration and the swagger-codegen Python generation command. Inspect the generated buggy_method and buggy_method_with_http_info handling of the body and uuid parameters; done means the generated client no longer duplicates required parameters and valid body-only, uuid-only, and combined calls behave correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.