diff --git a/crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/DocumentMapper.java b/crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/DocumentMapper.java index 028db93b7..ebd3bccd8 100644 --- a/crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/DocumentMapper.java +++ b/crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/DocumentMapper.java @@ -1,5 +1,7 @@ package io.crnk.core.engine.internal.document.mapper; +import io.crnk.core.engine.internal.utils.UrlUtils; +import io.crnk.core.engine.url.ServiceUrlProvider; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -143,7 +145,8 @@ private LinksInformation enrichSelfLink(LinksInformation linksInformation, Query linksInformation = selfLinksInformation; } - JsonApiUrlBuilder.UrlParameterBuilder urlBuilder = new JsonApiUrlBuilder.UrlParameterBuilder(requestUri.toString()); + JsonApiUrlBuilder.UrlParameterBuilder urlBuilder = new JsonApiUrlBuilder.UrlParameterBuilder(UrlUtils.concat(queryAdapter.getQueryContext().getBaseUrl(), + requestContext.getPath())); urlBuilder.addQueryParameters(requestContext.getRequestParameters()); selfLinksInformation.setSelf(urlBuilder.toString()); } diff --git a/crnk-core/src/test/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperTest.java b/crnk-core/src/test/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperTest.java index 7412eec65..4b9ee5e47 100644 --- a/crnk-core/src/test/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperTest.java +++ b/crnk-core/src/test/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperTest.java @@ -1,14 +1,18 @@ package io.crnk.core.engine.internal.document.mapper; +import com.google.common.collect.ImmutableMap; +import io.crnk.core.CoreTestContainer; import java.net.URI; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -77,8 +81,30 @@ public void testSerializeRootSelfLink() { Task task = createTask(2, "sample task"); QueryAdapter adapter = createAdapter(Task.class); Mockito.when(container.getRequestContextBase().getRequestUri()).thenReturn(URI.create("http://localhost/api/foo")); + Mockito.when(container.getRequestContextBase().getPath()).thenReturn("/api/foo"); Document document = mapper.toDocument(toResponse(task), adapter, mappingConfig).get(); - Assert.assertEquals("http://localhost/api/foo", getLinkText(document.getLinks().get("self"))); + Assert.assertEquals(CoreTestContainer.BASE_URL + "/api/foo", getLinkText(document.getLinks().get("self"))); + } + + @Test + public void shouldSerializeRootSelfLinkWithQueryParams() { + Task task = createTask(2, "sample task"); + QueryAdapter adapter = createAdapter(Task.class); + String queryParams = "?someParam=someValue&someOtherParam=someOtherValue"; + + // setup mocks + Mockito.when(container.getRequestContextBase().getRequestUri()).thenReturn(URI.create("http://localhost/api/foo" + queryParams)); + Mockito.when(container.getRequestContextBase().getPath()).thenReturn("/api/foo"); + Mockito.when(container.getRequestContextBase().getRequestParameters()).thenReturn(ImmutableMap.>builder() + .put("someParam", new HashSet<>(Arrays.asList("someValue"))) + .put("someOtherParam", new HashSet<>(Arrays.asList("someOtherValue"))) + .build()); + + // run + Document document = mapper.toDocument(toResponse(task), adapter, mappingConfig).get(); + + // assert + Assert.assertEquals(CoreTestContainer.BASE_URL + "/api/foo" + queryParams, getLinkText(document.getLinks().get("self"))); } public static class TaskLinks implements LinksInformation {