openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] Add the 'impl' directory to explain how to use python-fastapi

Open carmanzhang opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

Thanks for the amazing project, I am a Python user, I want to use the python-fastapi generator to generate some Python code with the fastapi framework supported. But I failed to run it, the error log is something like "AttributeError: module 'openapi_server' has no attribute 'impl'". This might be a hint that my own implementation is needed to override the default (empty) implementations generated, However, I have tried many implementation strategies, and my own implementation still can not be successfully invoked. I am wondering if it is possible to provide a 'impl' directory with a basic implementation under samples/server/petstore/python-fastapi/src/openapi_server to demonstrate how it works.

Many thanks!

carmanzhang avatar Sep 26 '23 05:09 carmanzhang

Another issue #15962 also points out the same problem

carmanzhang avatar Sep 26 '23 05:09 carmanzhang

It seems some underlying issues are fixed now (e.g., in #15962 and #17820), however, the example code still does not demonstrate how to overwrite an implementation.

In the issue it is mentioned:

...That will create a "sub-module" that is scanned for additional implementation functions or override functions.

I can't confirm that this is actually working though...

AlexanderWillner avatar Feb 28 '24 13:02 AlexanderWillner

I can conform that if you create a subclass in impl/__init__.py it will get detected via some __init_subclass__ hooks.

The exact names below might be slightly wrong, I use a lot of --prefix this and that in my project but the principals apply.

Basically, apis/default.py will import impl and apis/default_base.py.

In apis/default_base.py the base class will:

class BaseDefault:
    subclasses: ClassVar[Tuple] = ()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        BaseDefault.subclasses = BaseDefault.subclasses + (cls,)

so when the functions in apis/default.py call BaseDefault.subclasses[0]().function() they get the subclass.

nigelsim avatar May 16 '24 12:05 nigelsim

Hi @nigelsim

Thanks for providing the guide above. It validated my current direction––I am hopeful that you can help me with the final steps to get it working.

I have implemented a subclass of the BaseDefault class in the impl package. However, when I hit the corresponding endpoint, I just get a null response.

N.B.: I assumed that all the generated files should not get modified, only modifying the impl package should suffice for adding the endpoints' logics. I hope that is the right thing!

Thank you as you kindly help.

ridwan-salau avatar Jul 01 '24 18:07 ridwan-salau