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

Support 'Accept-Patch' header in MVC and WebFlux #26759

Closed
igorakkerman opened this issue Apr 4, 2021 · 1 comment
Closed

Support 'Accept-Patch' header in MVC and WebFlux #26759

igorakkerman opened this issue Apr 4, 2021 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@igorakkerman
Copy link

igorakkerman commented Apr 4, 2021

An API providing a PATCH endpoint should ensure two things:

1 - When the PATCH endpoint is sent a body with an unsupported media type, the response should include an Accept-Patch header, advertising the correct media type. This is specified in RFC5789, Section 2.2:

Unsupported patch document: Can be specified using a 415
(Unsupported Media Type) response when the client sends a patch
document format that the server does not support for the resource
identified by the Request-URI. Such a response SHOULD include an
Accept-Patch response header as described in Section 3.1 to notify
the client what patch document media types are supported.

2 - The OPTIONS endpoint should advertise the accepted request media types through its Accept-Patch header. This is stated in RFC5789, Section 3:

Accept-Patch SHOULD appear in the OPTIONS response for any resource
that supports the use of the PATCH method. The presence of the
Accept-Patch header in response to any method is an implicit
indication that PATCH is allowed on the resource identified by the
Request-URI. The presence of a specific patch document format in
this header indicates that that specific format is allowed on the
resource identified by the Request-URI.

It would be great if this would happen by default. Currently, Spring MVC doesn't add the header for either PATCH or OPTIONS, so I had to add this myself:

const val ACCEPT_PATCH_HEADER = "Accept-Patch"
const val APPLICATION_MERGE_PATCH_JSON_VALUE = "application/merge-patch+json"

// if used with a wrong media type, provide the Accept-Patch header with the error response
// as suggested by RFC 5789 "PATCH Method for HTTP" https://tools.ietf.org/html/rfc5789#section-2.2
@PatchMapping("/{id}")
@ResponseStatus(UNSUPPORTED_MEDIA_TYPE)
fun patchBadMediaType(response: HttpServletResponse) {
    response.addHeader(ACCEPT_PATCH_HEADER, APPLICATION_MERGE_PATCH_JSON_VALUE)
}

// advertise the Accept-Patch header as additional information
// as suggested by RFC 5789 "PATCH Method for HTTP" https://tools.ietf.org/html/rfc5789#section-3
@RequestMapping("/{id}", method = [RequestMethod.OPTIONS])
fun optionsForId(response: HttpServletResponse) {
    response.setHeader(ALLOW, "GET, HEAD, POST, PUT, OPTIONS, PATCH")
    response.addHeader(ACCEPT_PATCH_HEADER, APPLICATION_MERGE_PATCH_JSON_VALUE)
}

Note that I had to set the Allow header values manually, which would only be added correctly if I hadn't implemented the method myself. I might create an own issue for this.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 4, 2021
@igorakkerman igorakkerman changed the title MVC: 'Accept-Patch' header should advertise media type and OPTIONS MVC: 'Accept-Patch' header should advertise media type for PATCH and OPTIONS endpoints Apr 4, 2021
@igorakkerman igorakkerman changed the title MVC: 'Accept-Patch' header should advertise media type for PATCH and OPTIONS endpoints MVC: 'Accept-Patch' header should advertise PATCH media type for PATCH and OPTIONS endpoints Apr 4, 2021
@poutsma poutsma self-assigned this Apr 6, 2021
@poutsma poutsma added this to the 5.3.6 milestone Apr 6, 2021
@poutsma poutsma added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 8, 2021
@poutsma poutsma changed the title MVC: 'Accept-Patch' header should advertise PATCH media type for PATCH and OPTIONS endpoints Support 'Accept-Patch' header in MVC and WebFlux Apr 8, 2021
poutsma added a commit that referenced this issue Apr 8, 2021
This commit introduces support in both servlet and webflux for the
"Accept-Patch" header in OPTIONS requests, as defined in section 3.1 of
RFC 5789.

See gh-26759
@poutsma poutsma closed this as completed in a2d91a5 Apr 8, 2021
@poutsma poutsma added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 8, 2021
@igorakkerman
Copy link
Author

igorakkerman commented Apr 10, 2021

Thanks for implementing this so quickly. Works for me with the snapshot version, I can remove my own controller methods now.

Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
This commit introduces support in both servlet and webflux for the
"Accept-Patch" header in OPTIONS requests, as defined in section 3.1 of
RFC 5789.

See spring-projectsgh-26759
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
This commit introduces support in both servlet and webflux for the
"Accept-Patch" header, which is sent when the client sends unsupported
data in PATCH requests.
See  section 2.2 of RFC 5789.

Closes spring-projectsgh-26759
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
This commit introduces support in both servlet and webflux for the
"Accept-Patch" header in OPTIONS requests, as defined in section 3.1 of
RFC 5789.

See spring-projectsgh-26759
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
This commit introduces support in both servlet and webflux for the
"Accept-Patch" header, which is sent when the client sends unsupported
data in PATCH requests.
See  section 2.2 of RFC 5789.

Closes spring-projectsgh-26759
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants