OpenAPITools / OpenAPITools/openapi-generator

[BUG] [python-flask] x-openapi-router-controller and defaultController are ignored

Open
#10,765 2 comments 2 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)?
  • 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

There are two issues when generating server python-flask code:

  • When setting the defaultController additional property, it still generates default_controller.py instead of the given name.
  • When setting x-openapi-router-controller for a method in the OpenAPI yaml, the generated handler function is created inside default_controller.py, although x-openapi-router-controller does not point there, resulting in a runtime import error.
openapi-generator version

5.3.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Petstore API
  description: Petstore
  version: 0.1.0
servers:
  - url: https://pet.example.com/v0
    description: Base endpoint

paths:
  /feed:
    post:
      summary: Feed a pet
      description: Feed a pet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                brand:
                  type: string
                  example: acme_pet_food
              required:
                - brand
      responses:
        '204':
          description: Fed
      x-openapi-router-controller: pet_food_handler
Steps to reproduce
java -jar openapi-generator-cli.jar generate --input-spec example_issue/exampleapi.yaml --generator-name python-flask --additional-properties controllerPackage=handlers,defaultController=misc_handlers,disallowAdditionalPropertiesIfNotPresent=false,featureCORS=true,packageName=pet_server --output example_issue/build/python_3

Controllers in the controller package (which we named handlers):

$ ll example_issue/build/python_3/pet_server/handlers/
total 6
drwxr-xr-x 1 user 197609   0 Nov  2 20:39 ./
drwxr-xr-x 1 user 197609   0 Nov  2 20:39 ../
-rw-r--r-- 1 user 197609   0 Nov  2 20:39 __init__.py
-rw-r--r-- 1 user 197609 466 Nov  2 20:39 default_controller.py
-rw-r--r-- 1 user 197609  25 Nov  2 20:39 security_controller_.py

The default controller is still named default_controller.py despite passing defaultController=misc_handlers.

More importantly, the x-openapi-router-controller for the method is the non-existent pet_food_handler:

$ tail -20 example_issue/build/python_3/pet_server/openapi/openapi.yaml | head -5
          description: Fed
      summary: Feed a pet
      x-openapi-router-controller: pet_food_handler
components:
  requestBodies:

Tests fail:

$ tox
... pytest output...
>       return _bootstrap._gcd_import(name[level:], package, level)
E       ModuleNotFoundError: No module named 'pet_food_handler'

/usr/lib/python3.8/importlib/__init__.py:127: ModuleNotFoundError
... output continues ...

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 with example_issue/exampleapi.yaml and run the documented openapi-generator-cli command using the python-flask generator, then run tox in the generated project. Compare the generated controller filename and handler import with defaultController=misc_handlers and x-openapi-router-controller: pet_food_handler; done means both settings produce an importable generated project without ModuleNotFoundError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.