OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python-Flask] Python parameter 'id' gets suffixed with an underscore when pythonic_params=True.

Open
#3,304 3 comments 5 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
  • 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

The generated controller code uses 'id' as argument name, as specified in openapi.yaml. Connexion however, with option pythonic_params=True, mangles that parameter to 'id_' during invocation. As a result, the invocation fails:

TypeError: examples_id_extra_get() got an unexpected keyword argument 'id_

(missing closing quote at the end literally copied from output)

It doesn't happen when 'id' is replaced with something like 'ab'. And 'thingId' does get translated properly into 'thing_id'. Maybe this happens because id() is an inbuilt function in Python.

openapi-generator version

4.0.2

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Flask Generator Issue #new
  description: |
    This API provides an example of the problem short parameter names, which
    get an underscore appended to become 'pythonic'.
  version: 0.1.0
servers:
- url: http://localhost:8080
paths:
  /examples/{id}/extra:
    get:
      summary: Example GET.
      parameters:
        - name: id
          in: path
          description: The id of the example required.
          required: true
          schema:
            type: string
            format: uuid
      responses:
        200:
          description: OK
Steps to reproduce

Simply generate with -g python-flask.

Related issues/PRs

#1671
#1856

Suggest a fix

It would appear 'id' is considered reserved, while it should be considered a valid argument name. Either this bug should be addressed by the Connexion team, or the OpenAPI generator has to generate matching controller code.

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 Python Flask server from the supplied OpenAPI declaration with the python-flask generator and pythonic_params=True. Compare the generated controller signature with the invocation for the path parameter named id, then determine whether the matching change belongs in OpenAPI Generator or Connexion. Done means the generated controller and invocation agree without the id_ TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python
Domain
api, backend
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.