Skip to content

Commit

Permalink
fix: deserialize a JsonError as a GenericResource
Browse files Browse the repository at this point in the history
This pull request adds the JsonError test to verify JsonMapper can deserialize a JsonError as a GenericResource introduced in #10526 as a TCK test.
  • Loading branch information
sdelamo committed Mar 19, 2024
1 parent f070f7c commit 09258ae
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.server.tck.tests.hateoas;

import io.micronaut.http.hateoas.Resource;
import io.micronaut.http.tck.ServerUnderTest;
import io.micronaut.http.tck.ServerUnderTestProviderUtils;
import io.micronaut.json.JsonMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Collections;

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

@SuppressWarnings({
"java:S5960", // We're allowed assertions, as these are used in tests only
"checkstyle:MissingJavadocType",
"checkstyle:DesignForExtension"
})
public class JsonErrorSerdeTest {
private static final String JSON_ERROR = """
{"_links":{"self":[{"href":"/resolve","templated":false}]},"_embedded":{"errors":[{"message":"Internal Server Error: Something bad happened"}]},"message":"Internal Server Error"}""";
private static final String SPEC_NAME = "JsonErrorSerdeTest";

/**
* @throws IOException Exception thrown while getting the server under test.
*/
@Test
void canDeserializeAJsonErrorAsAGenericResource() throws IOException {
try (ServerUnderTest server = ServerUnderTestProviderUtils.getServerUnderTestProvider().getServer(SPEC_NAME, Collections.emptyMap())) {
JsonMapper jsonMapper = server.getApplicationContext().getBean(JsonMapper.class);
//when:
Resource resource = jsonMapper.readValue(JSON_ERROR, Resource.class);
//then:
assertTrue(resource.getEmbedded().getFirst("errors").isPresent());
assertTrue(resource.getLinks().getFirst("self").isPresent());
assertEquals("/resolve", resource.getLinks().getFirst("self").get().getHref());
assertFalse(resource.getLinks().getFirst("self").get().isTemplated());
}
}
}
17 changes: 10 additions & 7 deletions http/src/main/java/io/micronaut/http/hateoas/AbstractResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@
import io.micronaut.http.annotation.Produces;

import io.micronaut.core.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import java.util.*;

/**
* An abstract implementation of {@link Resource}.
Expand All @@ -42,7 +38,7 @@
*/
@Produces(MediaType.APPLICATION_HAL_JSON)
@Introspected
public abstract class AbstractResource<Impl extends AbstractResource> implements Resource {
public abstract class AbstractResource<Impl extends AbstractResource<Impl>> implements Resource {

private final Map<CharSequence, List<Link>> linkMap = new LinkedHashMap<>(1);
private final Map<CharSequence, List<Resource>> embeddedMap = new LinkedHashMap<>(1);
Expand Down Expand Up @@ -150,6 +146,13 @@ public final void setLinks(Map<String, Object> links) {
if (value instanceof Map) {
Map<String, Object> linkMap = (Map<String, Object>) value;
link(name, linkMap);
} else if (value instanceof Collection<?> collection) {
for (Object o : collection) {
if (o instanceof Map) {
Map<String, Object> linkMap = (Map<String, Object>) o;
link(name, linkMap);
}
}
}
}
}
Expand Down

0 comments on commit 09258ae

Please sign in to comment.