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

Swagger can't handle exceptions on HTTP delete #30

Closed
cnizzardini opened this issue Feb 6, 2021 · 3 comments
Closed

Swagger can't handle exceptions on HTTP delete #30

cnizzardini opened this issue Feb 6, 2021 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@cnizzardini
Copy link
Member

cnizzardini commented Feb 6, 2021

What Happened
Tried deleting a record that doesn't exist via swagger interface. An exception is generated which causes swagger to crash.

Expected Behavior
404 response

Steps to Reproduce

  1. Send HTTP delete for a recrod that does not exist

Version and Platform (please complete the following information):

  • OS/Platform: Linux/Docker
  • CakePHP: 4.2.*
  • MixerApi Version: 0.2.4
@cnizzardini cnizzardini added the bug Something isn't working label Feb 6, 2021
@cnizzardini cnizzardini added this to the v0.2.5 milestone Feb 6, 2021
@cnizzardini cnizzardini self-assigned this Feb 6, 2021
@cnizzardini cnizzardini added documentation Improvements or additions to documentation and removed bug Something isn't working labels Feb 14, 2021
@cnizzardini cnizzardini changed the title [MixerApi/Bake] - Swagger can't handle exceptions on HTTP delete Swagger can't handle exceptions on HTTP delete Feb 14, 2021
@cnizzardini
Copy link
Member Author

cnizzardini commented Feb 14, 2021

This is a limitation of swagger/openapi. There is no way that I've seen to define the accepts header on DELETE operations.

A CakePHP Middleware can be added like this to force wildcard accept headers to application/json.

# src/Application.php

    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue
            // Force wildcard accept header to application/json responses
            ->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler){
                /*if ($request->getMethod() !== 'DELETE') {
                    return $handler->handle($request);
                }*/

                $accept = $request->getHeader('accept');
                $item = reset($accept);
                if ($item === '*/*') {
                    return $handler->handle($request->withHeader('accept', 'application/json'));
                }

                return $handler->handle($request);
            })
        // ... additional middleware
        return $middlewareQueue;
    }

This will handle the default swagger functionality:

curl -X DELETE "http://localhost:8080/admin/actors/123123123" -H  "accept: */*"

@cnizzardini
Copy link
Member Author

cnizzardini commented Feb 14, 2021

If desired, this can be isolated specifically to Swagger by adding a custom header.

See: swagger-api/swagger-ui#2793

Example:

<script>
    window.onload = function() {
        // Begin Swagger UI call region
        const ui = SwaggerUIBundle({
            url: '<?php echo $url ?>',
            dom_id: '#swagger-ui',
            deepLinking: true,
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],
            layout: "StandaloneLayout"
        })
        // End Swagger UI call region
        ui.getConfigs().requestInterceptor = (req) => { req.headers['X-API-CLIENT']= 'SWAGGER'; return req; }
        window.ui = ui
    }
</script>

@cnizzardini cnizzardini modified the milestones: v0.2.5, v0.2.6 Apr 8, 2021
@cnizzardini
Copy link
Member Author

Added to demo application

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

No branches or pull requests

1 participant