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

Change return codes to new Responders defaults #918

Merged

Conversation

tagliala
Copy link
Contributor

@tagliala tagliala commented Jun 1, 2024

Change default error and redirect status codes
Update responder class to the current defaults for responder gem

Close: #862

Ref: heartcombo/responders#240

Supersedes #863


I'm doing some tests with this branch and $ bin/rake local server with Active Admin main

Action Method Status Before Status After Match Rails
Create category POST 302 Found 303 See Other ❌ (302, but 303 is better)
Create category (fail validations) POST 200 OK 422 Unprocessable Entity
Update category POST 302 Found 303 See Other ❌ (302, but 303 is better)
Update category (fail validations) POST 200 OK 422 Unprocessable Entity
Destroy category POST 302 Found 303 See Other

Rails by default returns 302 on successful create and update, and 303 only on destroy. However, according to HTTP/1.1 standard, it appears that 303 is the correct status code to redirect from a POST/PATCH/PUT/DELETE to a GET and clients do treat 302 like a 303, by using GET instead of using the original method

References that motivate the decision:

  • Add redirect_code_for_unsafe_http_methods config rails/rails#45393

  • Clarification on redirect status code (303) hotwired/turbo#84

  • https://github.com/heartcombo/responders?tab=readme-ov-file#configuring-error-and-redirect-statuses

    Note: the application responder generated for new apps already configures a different set of defaults: 422 Unprocessable Entity for errors, and 303 See Other for redirects. Responders may change the defaults to match these in a future major release.

  • https://en.wikipedia.org/wiki/HTTP_302

    Many web browsers implemented this code in a manner that violated this standard, changing the request type of the new request to GET, regardless of the type employed in the original request (e.g. POST).[1] For this reason, HTTP/1.1 (RFC 2616) added the new status codes 303 and 307 to disambiguate between the two behaviours, with 303 mandating the change of request type to GET, and 307 preserving the request type as originally sent. Despite the greater clarity provided by this disambiguation, the 302 code is still employed in web frameworks to preserve compatibility with browsers that do not implement the HTTP/1.1 specification.[2]

    (emphasis mine)

  • https://datatracker.ietf.org/doc/html/rfc2616#section-10.3.4

    If the 302 status code is received in response to a request other
    than GET or HEAD, the user agent MUST NOT automatically redirect the
    request unless it can be confirmed by the user, since this might
    change the conditions under which the request was issued.

    Note: RFC 1945 and RFC 2068 specify that the client is not allowed
    to change the method on the redirected request. However, most
    existing user agent implementations treat 302 as if it were a 303
    response, performing a GET on the Location field-value regardless
    of the original request method
    . The status codes 303 and 307 have
    been added for servers that wish to make unambiguously clear which
    kind of reaction is expected of the client.

    (emphasis mine)

Default responder created by rails g responders:install: https://github.com/heartcombo/responders/blob/9bdc60dfbfa8001641c1c4df7bc73c3fc2a4cf41/lib/generators/responders/install_generator.rb#L10-L25

Copy link

codecov bot commented Jun 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.92%. Comparing base (6d716df) to head (bf8be73).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #918   +/-   ##
=======================================
  Coverage   98.91%   98.92%           
=======================================
  Files          14       14           
  Lines         555      557    +2     
=======================================
+ Hits          549      551    +2     
  Misses          6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tagliala
Copy link
Contributor Author

tagliala commented Jun 1, 2024

@javierjulio

As per https://github.com/heartcombo/responders?tab=readme-ov-file#configuring-error-and-redirect-statuses, newly generated application responders default to

self.error_status = :unprocessable_entity
self.redirect_status = :see_other

(and also include include Responders::HttpCacheResponder)

So it may have sense to change those fields, regardless of Turbo support, and release a major version

@tagliala tagliala force-pushed the feature/862-improve-return-codes branch 2 times, most recently from 5c6b992 to 4486c67 Compare June 6, 2024 12:42
@tagliala tagliala force-pushed the feature/862-improve-return-codes branch from 4486c67 to 94ee97a Compare June 12, 2024 10:54
@tagliala tagliala changed the title [WIP] Feature/862 improve return codes Improve return codes Jun 12, 2024
@tagliala tagliala changed the title Improve return codes Change return codes to new responder defaults Jun 12, 2024
@tagliala tagliala marked this pull request as ready for review June 12, 2024 10:55
@tagliala tagliala changed the title Change return codes to new responder defaults Change return codes to new responder defaults [BREAKING CHANGE] Jun 12, 2024
@tagliala tagliala changed the title Change return codes to new responder defaults [BREAKING CHANGE] Change return codes to new responder defaults [POTENTIAL BREAKING CHANGE] Jun 12, 2024
@tagliala tagliala force-pushed the feature/862-improve-return-codes branch from 94ee97a to 3868d00 Compare June 14, 2024 07:34
@tagliala tagliala requested a review from javierjulio June 14, 2024 21:24
@javierjulio javierjulio changed the title Change return codes to new responder defaults [POTENTIAL BREAKING CHANGE] Change return codes to new Responders defaults Jun 15, 2024
@javierjulio javierjulio force-pushed the feature/862-improve-return-codes branch from 3868d00 to c7df3eb Compare June 15, 2024 14:37
Copy link
Member

@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

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

Can you please retest this without the Responders::HttpCacheResponder inclusion? When retesting, can you please make sure that forms for both new/create and edit/update, work as expected? Both success and failure scenarios. I just want to make sure there isn't any regressions there for ActiveAdmin. I don't believe there would be.

lib/inherited_resources/responder.rb Outdated Show resolved Hide resolved
@tagliala tagliala force-pushed the feature/862-improve-return-codes branch 2 times, most recently from 211035f to f4eabf5 Compare June 15, 2024 21:23
@tagliala tagliala requested a review from javierjulio June 15, 2024 21:23
@tagliala
Copy link
Contributor Author

Updated with a ref to a9fb8e1

Copy link
Member

@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

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

Thank you for including the reference. It makes sense why it was changed. Referencing the "why" is always helpful so I appreciate it.

How did retesting this go with ActiveAdmin? I'd like to ensure that submitting forms, both successful and with validation errors, works as expected. Essentially, no regressions.

Do you think we should release this in a major version? ActiveAdmin is locked to v1.x for InheritedResources so earlier version won't be able to use it so releasing a major version of this is safe. We could permit it in ActiveAdmin v4 though since it's still beta.

@tagliala
Copy link
Contributor Author

How did retesting this go with ActiveAdmin? I'd like to ensure that submitting forms, both successful and with validation errors, works as expected. Essentially, no regressions.

According to my tests in the default test app, there are no visible changes. I will run active admin specs against this branch

Do you think we should release this in a major version?

It is a potential breaking change with some custom javascript expecting a 200 from the server, so in any case it makes sense to release in a major version

ActiveAdmin is locked to v1.x for InheritedResources so earlier version won't be able to use it so releasing a major version of this is safe. We could permit it in ActiveAdmin v4 though since it's still beta.

Probably it is a good choice

@javierjulio javierjulio force-pushed the feature/862-improve-return-codes branch from f4eabf5 to 70abc8f Compare July 2, 2024 21:28
Copy link
Member

@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

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

Thanks! Let's release as v2 then.

@tagliala
Copy link
Contributor Author

tagliala commented Jul 3, 2024

All Active Admin specs (both master and 3-0-stable) pass against this branch


Screenshots

Before

image

After

image

@tagliala tagliala force-pushed the feature/862-improve-return-codes branch 3 times, most recently from a239d91 to cd6c395 Compare July 28, 2024 08:02
Update responder class to the current defaults for responder gem.

`HttpCacheResponder` was deliberately excluded in version 1.8.0

Close: activeadmin#862

Ref:
- heartcombo/responders#240
- a9fb8e1
@tagliala tagliala force-pushed the feature/862-improve-return-codes branch from cd6c395 to bf8be73 Compare August 20, 2024 08:18
@tagliala
Copy link
Contributor Author

@javierjulio rebased

  • Should I bump the major in this branch?
  • Should we release a beta with this change?

@javierjulio
Copy link
Member

@tagliala awesome thanks. I think this is safe as a major release, no beta release needed. You are welcome to bump the major version here or in another PR, I'm fine with either. Thank you.

@tagliala tagliala merged commit 1eb5233 into activeadmin:master Aug 21, 2024
21 checks passed
@tagliala tagliala deleted the feature/862-improve-return-codes branch August 21, 2024 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create & Update should return status code != 200 for failed requests
2 participants