Skip to content

Commit 09258ae

Browse files
committed
fix: deserialize a JsonError as a GenericResource
This pull request adds the JsonError test to verify JsonMapper can deserialize a JsonError as a GenericResource introduced in #10526 as a TCK test.
1 parent f070f7c commit 09258ae

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017-2024 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.http.server.tck.tests.hateoas;
17+
18+
import io.micronaut.http.hateoas.Resource;
19+
import io.micronaut.http.tck.ServerUnderTest;
20+
import io.micronaut.http.tck.ServerUnderTestProviderUtils;
21+
import io.micronaut.json.JsonMapper;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.IOException;
25+
import java.util.Collections;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
@SuppressWarnings({
30+
"java:S5960", // We're allowed assertions, as these are used in tests only
31+
"checkstyle:MissingJavadocType",
32+
"checkstyle:DesignForExtension"
33+
})
34+
public class JsonErrorSerdeTest {
35+
private static final String JSON_ERROR = """
36+
{"_links":{"self":[{"href":"/resolve","templated":false}]},"_embedded":{"errors":[{"message":"Internal Server Error: Something bad happened"}]},"message":"Internal Server Error"}""";
37+
private static final String SPEC_NAME = "JsonErrorSerdeTest";
38+
39+
/**
40+
* @throws IOException Exception thrown while getting the server under test.
41+
*/
42+
@Test
43+
void canDeserializeAJsonErrorAsAGenericResource() throws IOException {
44+
try (ServerUnderTest server = ServerUnderTestProviderUtils.getServerUnderTestProvider().getServer(SPEC_NAME, Collections.emptyMap())) {
45+
JsonMapper jsonMapper = server.getApplicationContext().getBean(JsonMapper.class);
46+
//when:
47+
Resource resource = jsonMapper.readValue(JSON_ERROR, Resource.class);
48+
//then:
49+
assertTrue(resource.getEmbedded().getFirst("errors").isPresent());
50+
assertTrue(resource.getLinks().getFirst("self").isPresent());
51+
assertEquals("/resolve", resource.getLinks().getFirst("self").get().getHref());
52+
assertFalse(resource.getLinks().getFirst("self").get().isTemplated());
53+
}
54+
}
55+
}

http/src/main/java/io/micronaut/http/hateoas/AbstractResource.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@
2626
import io.micronaut.http.annotation.Produces;
2727

2828
import io.micronaut.core.annotation.Nullable;
29-
import java.util.ArrayList;
30-
import java.util.Arrays;
31-
import java.util.LinkedHashMap;
32-
import java.util.List;
33-
import java.util.Map;
34-
import java.util.Optional;
29+
30+
import java.util.*;
3531

3632
/**
3733
* An abstract implementation of {@link Resource}.
@@ -42,7 +38,7 @@
4238
*/
4339
@Produces(MediaType.APPLICATION_HAL_JSON)
4440
@Introspected
45-
public abstract class AbstractResource<Impl extends AbstractResource> implements Resource {
41+
public abstract class AbstractResource<Impl extends AbstractResource<Impl>> implements Resource {
4642

4743
private final Map<CharSequence, List<Link>> linkMap = new LinkedHashMap<>(1);
4844
private final Map<CharSequence, List<Resource>> embeddedMap = new LinkedHashMap<>(1);
@@ -150,6 +146,13 @@ public final void setLinks(Map<String, Object> links) {
150146
if (value instanceof Map) {
151147
Map<String, Object> linkMap = (Map<String, Object>) value;
152148
link(name, linkMap);
149+
} else if (value instanceof Collection<?> collection) {
150+
for (Object o : collection) {
151+
if (o instanceof Map) {
152+
Map<String, Object> linkMap = (Map<String, Object>) o;
153+
link(name, linkMap);
154+
}
155+
}
153156
}
154157
}
155158
}

0 commit comments

Comments
 (0)