Skip to content

Commit

Permalink
regression: support JSON Merge Patch (#10089)
Browse files Browse the repository at this point in the history
  • Loading branch information
caioguedes authored Nov 10, 2023
1 parent 9a59ee8 commit 1620389
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
MediaType.APPLICATION_JSON_GITHUB,
MediaType.APPLICATION_JSON_FEED,
MediaType.APPLICATION_JSON_PATCH,
MediaType.APPLICATION_JSON_MERGE_PATCH,
MediaType.APPLICATION_JSON_PROBLEM
})
@Consumes({
Expand All @@ -67,6 +68,7 @@
MediaType.APPLICATION_JSON_GITHUB,
MediaType.APPLICATION_JSON_FEED,
MediaType.APPLICATION_JSON_PATCH,
MediaType.APPLICATION_JSON_MERGE_PATCH,
MediaType.APPLICATION_JSON_PROBLEM
})
@BootstrapContextCompatible
Expand Down
13 changes: 13 additions & 0 deletions http/src/main/java/io/micronaut/http/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ public class MediaType implements CharSequence {
*/
public static final MediaType APPLICATION_JSON_PATCH_TYPE = new MediaType(MediaType.APPLICATION_JSON_PATCH);

/**
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7386">JSON Merge Patch</a>
* JSON Merge Patch: application/merge-patch+json
*/
public static final String APPLICATION_JSON_MERGE_PATCH = "application/merge-patch+json";

/**
* JSON Merge Patch: application/merge-patch+json
*/
public static final MediaType APPLICATION_JSON_MERGE_PATCH_TYPE = new MediaType(MediaType.APPLICATION_JSON_MERGE_PATCH);

/**
* JSON Feed: application/problem+json.
*/
Expand Down Expand Up @@ -584,6 +595,8 @@ public static MediaType of(String mediaType) {
return APPLICATION_JSON_GITHUB_TYPE;
case APPLICATION_JSON_PATCH:
return APPLICATION_JSON_PATCH_TYPE;
case APPLICATION_JSON_MERGE_PATCH:
return APPLICATION_JSON_MERGE_PATCH_TYPE;
case APPLICATION_JSON_PROBLEM:
return APPLICATION_JSON_PROBLEM_TYPE;
case APPLICATION_YAML:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MediaTypeSpec extends Specification {
MediaType.of('application/feed+json') == MediaType.APPLICATION_JSON_FEED_TYPE
MediaType.of('application/problem+json') == MediaType.APPLICATION_JSON_PROBLEM_TYPE
MediaType.of('application/json-patch+json') == MediaType.APPLICATION_JSON_PATCH_TYPE
MediaType.of('application/merge-patch+json') == MediaType.APPLICATION_JSON_MERGE_PATCH_TYPE
}

void "test media type"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
MediaType.APPLICATION_JSON_GITHUB,
MediaType.APPLICATION_JSON_FEED,
MediaType.APPLICATION_JSON_PROBLEM,
MediaType.APPLICATION_JSON_PATCH
MediaType.APPLICATION_JSON_PATCH,
MediaType.APPLICATION_JSON_MERGE_PATCH
})
@Consumes({
MediaType.APPLICATION_JSON,
Expand All @@ -63,7 +64,8 @@
MediaType.APPLICATION_JSON_GITHUB,
MediaType.APPLICATION_JSON_FEED,
MediaType.APPLICATION_JSON_PROBLEM,
MediaType.APPLICATION_JSON_PATCH
MediaType.APPLICATION_JSON_PATCH,
MediaType.APPLICATION_JSON_MERGE_PATCH
})
@BootstrapContextCompatible
public final class JsonMessageHandler<T> implements MessageBodyHandler<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class JsonMediaTypeCodec extends MapperMediaTypeCodec {
MediaType.APPLICATION_JSON_GITHUB_TYPE,
MediaType.APPLICATION_JSON_FEED_TYPE,
MediaType.APPLICATION_JSON_PATCH_TYPE,
MediaType.APPLICATION_JSON_MERGE_PATCH_TYPE,
MediaType.APPLICATION_JSON_PROBLEM_TYPE
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.micronaut.docs.jsonpatch;

import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

@Property(name = "spec.name", value = "JsonMergePatchTest")
@MicronautTest
public class JsonMergePatchTest {
@Test
void acceptMergePatchJsonContentType(@Client("/") HttpClient client) {
String expected, body = expected = "{\"f\":\"foo\",\"b\":\"bar\"}";
String response = assertDoesNotThrow(() -> client.toBlocking().retrieve(
HttpRequest.PATCH("/json-merge-patch", body)
.contentType("application/merge-patch+json")
.accept("application/merge-patch+json")
));
assertNotNull(response);
assertEquals(expected, response);
}

@Requires(property = "spec.name", value = "JsonMergePatchTest")
@Controller("/json-merge-patch")
static class JsonMergePatchController {
@Consumes(MediaType.APPLICATION_JSON_MERGE_PATCH)
@Produces(MediaType.APPLICATION_JSON_MERGE_PATCH)
@Patch
MergePatch mergePatch(@Body MergePatch body) {
return body;
}
}

@Introspected
record MergePatch(String f, String b) {
}
}

0 comments on commit 1620389

Please sign in to comment.