Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in @endpoint decorator #180

Closed
nimitbhardwaj opened this issue Mar 25, 2024 · 4 comments · Fixed by #182
Closed

Issue in @endpoint decorator #180

nimitbhardwaj opened this issue Mar 25, 2024 · 4 comments · Fixed by #182
Assignees
Labels
bug Something isn't working

Comments

@nimitbhardwaj
Copy link

Hi,
I am using version 3.5.0 of fastapi-class, when using @endpoint decorator in my view I am not able to get the endpoint available for me to use.

I am using following code.

router = APIRouter()

@View(router)
class UserView:
    @endpoint('GET', path='/kapa')
    def kapa(self):
        return {"hello": "world"}
    

And I am not able to get kapa endpoint, neither in swagger doc nor from API.

I explored the internal code of fastapi-class, I believe I know the issue, but correct me if I am wrong.

In file fastapi_class/views.py, in method View, in _decorator(line 56)

    def _decorator(cls) -> None:
        obj = cls()
        cls_based_response_model = getattr(obj, RESPONSE_MODEL_ATTRIBUTE_NAME, {})
        cls_based_response_class = getattr(obj, RESPONSE_CLASS_ATTRIBUTE_NAME, {})
        common_exceptions = getattr(obj, EXCEPTIONS_ATTRIBUTE_NAME, {}).get(
            COMMON_KEYWORD, ()
        )
        for _callable_name in dir(obj):
            _callable = getattr(obj, _callable_name)
            if _callable_name in set(Method) or hasattr(
                _callable, ENDPOINT_METADATA_ATTRIBUTE_NAME
            ):
                metadata: Metadata = getattr(
                    _callable,
                    ENDPOINT_METADATA_ATTRIBUTE_NAME,
                    Metadata([_callable_name]),
                )

here we have a condition if _callable_name in set(Method) or hasattr(_callable, ENDPOINT_METADATA_ATTRIBUTE_NAME ): which means if method names of view are either (get, put, post, delete...) or if they have ENDPOINT_METADATA_ATTRIBUTE_NAME as an attribute and this constant literal's value is ENDPOINT_METADATA_ATTRIBUTE_NAME = "ENDPOINT_METADATA" from line 15 views.py.

But the @endpoint decorator sets the attribute name to __endpoint_metadata instead of ENDPOINT_METADATA (line 106 in fastapi_class/routers.py).

        _wrapper.__endpoint_metadata = Metadata(  # type: rignore
            methods=parsed_method,
            name=name,
            path=path,
            status_code=status_code,
            response_class=response_class,
            response_model=response_model,
        )

So I believe, fix here is we should replace ENDPOINT_METADATA_ATTRIBUTE_NAME = "ENDPOINT_METADATA" with ENDPOINT_METADATA_ATTRIBUTE_NAME = "__endpoint_metadata"

As I tried above fix for my local, it worked.

May be wrong for fix, I just read the code today, but @endpoint decorator seems not to be working

@timok19
Copy link

timok19 commented Apr 15, 2024

Is this still alive? I am facing the same issue

@timok19
Copy link

timok19 commented Apr 15, 2024

@yezz123, please take a look at this issue.

@yezz123
Copy link
Owner

yezz123 commented Apr 20, 2024

Hello @timok19 @nimitbhardwaj I will try to follow up with this Issue 🙏🏻

@yezz123 yezz123 linked a pull request Apr 20, 2024 that will close this issue
@yezz123 yezz123 self-assigned this Apr 20, 2024
@yezz123 yezz123 added the bug Something isn't working label Apr 20, 2024
@yezz123
Copy link
Owner

yezz123 commented Apr 20, 2024

Thank you @nimitbhardwaj @timok19 Its fixed in 3.6.0 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants