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

fix(rack): ensure OPTIONS preflight requests are handled #18

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

paulsturgess
Copy link
Contributor

@paulsturgess paulsturgess commented Nov 2, 2023

This fix, to handle OPTIONS requests, allows us to use the swagger-editor user interface to test out OpenAPI generated specs from Apia.

To test this locally you'll need an API running with an OpenAPI spec for that API. Import the spec into swagger-editor and then use the "try it out" feature. It will send an OPTIONS preflight request, followed by the actual API request.

@paulsturgess paulsturgess self-assigned this Nov 2, 2023
lib/apia/rack.rb Outdated
Comment on lines 82 to 83
route = find_route(request_method, api_path)
access_control_request_method = env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']&.upcase
route = find_route((access_control_request_method || request_method), api_path)
Copy link
Contributor Author

@paulsturgess paulsturgess Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the options preflight request it tells us the request method of the actual API request in HTTP_ACCESS_CONTROL_REQUEST_METHOD, so we use that to find the corresponding route. Otherwise we just get a 404.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only use this header when requests themselves are OPTIONS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is done

@paulsturgess paulsturgess marked this pull request as ready for review November 2, 2023 15:16
@paulsturgess paulsturgess requested a review from jimeh November 2, 2023 15:17
@paulsturgess paulsturgess merged commit 8142479 into main Nov 9, 2023
8 checks passed
@paulsturgess paulsturgess deleted the handle-options branch November 9, 2023 14:09
@replease replease bot mentioned this pull request Nov 9, 2023
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

Successfully merging this pull request may close these issues.

3 participants