简体   繁体   中英

Generating Python code from OpenAPI spec that recieves a "handler" object

I have an OpenAPI spec that I successfully turned into Server/Client Python code using OpenAPI generator. However the structure of the server does not really fit my needs. To specify how to handle request (say method.foo()), I need to modify a file inside the generated code called "default_controller.py". I'd much rather generate a Python object that specifies the method.foo() and give this to the generated code which turns this into a valid server.


class HandlerClass:
    
    def foo(self):
        print("Performing foo")
    
    def bar(self):
        print("Performing bar")
        

handler = HandlerClass()

server = GenerateServerFromHandler(handler)

I know this is possible within the Thrift API-spec but unfortunately this is not quite the right tool for the job. Is it possible to generate code like this?

The openapi-generator generated code can be customized in several ways as described here , depending on which generator you are using:

  • You can download one or more of the default template files (eg see here for the python-flask generator) and modify them, then point openapi-generator to them with the --template-dir argument.
  • You can customize each generator in several ways via a generator-specific config file.
  • If you want an entirely different generated structure, you can write your own custom generator as described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM