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

version 0.5.0 get error in openai 'GET Request cannot have a body' #415

Open
horizon365 opened this issue May 20, 2024 · 2 comments
Open

Comments

@horizon365
Copy link

horizon365 commented May 20, 2024

version 0.5.0
when use in openapi, it get error.
图片
it may effect all url support by ninja-crud.

@horizon365 horizon365 changed the title version 0.5.0 get error 'GET Request cannot have a body' version 0.5.0 get error in openai 'GET Request cannot have a body' May 20, 2024
@robinsandstrom
Copy link

robinsandstrom commented Jun 24, 2024

Seems to be in this code segment:

    def view_func(self) -> ViewFunction:
        """
        Return the wrapped view function with path, query, and request body parameters
        annotated, and any decorators applied.

        Returns:
            ViewFunction: The wrapped view function.
        """

        def wrapped_view_function(
            request: django.http.HttpRequest,
            path_parameters: Any = ninja.Path(default=None, include_in_schema=False),
            query_parameters: Any = ninja.Query(default=None, include_in_schema=False),
            request_body: Any = ninja.Body(default=None, include_in_schema=False),
        ) -> Any:
            return self.view_function(request, path_parameters, query_parameters, request_body)

        wrapped_view_function.__annotations__.update(
            {
                "path_parameters": self.path_parameters,
                "query_parameters": self.query_parameters,
                "request_body": self.request_body,
            }
        )
        if self.view_function_name is not None:
            wrapped_view_function.__name__ = self.view_function_name

        for decorator in reversed(self.decorators):
            wrapped_view_function = decorator(wrapped_view_function)

        return wrapped_view_function

If I remove request_body from the argument of wrapped_view_function the error disappears. But not sure how that will work on post requests. Any PRs?

@jboutros
Copy link

jboutros commented Jul 2, 2024

Also ran into this issue - all GET requests in my OpenAPI config specify an optional request body which is tripping up some clients.

As a temporary workaround, I'm using the openapi_extra mechanism in ninja to override the request body definition. This creates a technically invalid openapi spec but solves my problem at the client level. YMMV.

Defining a view in this fashion looks something like this:

retrieve_profile_by_user_id = views.ReadView(
        get_model=lambda request, path_parameters: Profile.objects.get(user_id=path_parameters.id),
        path="/user/{id}",
        operation_kwargs={
            "openapi_extra": {
                "requestBody": None
            }
        }
    )

Ideally we'd be able to suppress the inclusion of a request body on methods such as GET, DELETE, and HEAD as it is no longer allowed on those methods as of openapi 3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants