From be5576b0efe3ad19c53ddca2d650adb1a8d2d0b0 Mon Sep 17 00:00:00 2001 From: Pekka Iso-Ahola Date: Sat, 31 Oct 2020 18:48:12 +0200 Subject: [PATCH 1/3] Modified DocumentMapper to use ServiceUrlProvider for generating document self links. This allows for relative urls (by emptying crnk.domain-name) and also improves configurability in situations where the system runs behind a proxy. --- .../core/engine/internal/document/mapper/DocumentMapper.java | 5 ++++- .../engine/internal/document/mapper/DocumentMapperTest.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) 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..808139cfd 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()); + String selfLinkUrl = UrlUtils.concat(queryAdapter.getResourceRegistry().getServiceUrlProvider().getUrl(), requestContext.getPath()); + JsonApiUrlBuilder.UrlParameterBuilder urlBuilder = new JsonApiUrlBuilder.UrlParameterBuilder(selfLinkUrl); 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..f99cd9163 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 @@ -78,7 +78,7 @@ public void testSerializeRootSelfLink() { QueryAdapter adapter = createAdapter(Task.class); Mockito.when(container.getRequestContextBase().getRequestUri()).thenReturn(URI.create("http://localhost/api/foo")); Document document = mapper.toDocument(toResponse(task), adapter, mappingConfig).get(); - Assert.assertEquals("http://localhost/api/foo", getLinkText(document.getLinks().get("self"))); + Assert.assertEquals("http://127.0.0.1/api/foo", getLinkText(document.getLinks().get("self"))); } public static class TaskLinks implements LinksInformation { From 8ce2804c2d2ac8085a7c583e1fa5e9595267715d Mon Sep 17 00:00:00 2001 From: Pekka Iso-Ahola Date: Mon, 2 Nov 2020 12:02:06 +0200 Subject: [PATCH 2/3] Fixed DocumentMapper unit tests. --- .../document/mapper/DocumentMapperTest.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 f99cd9163..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://127.0.0.1/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 { From 17567997c9264f79851ce8a2fd2cfe622a83db41 Mon Sep 17 00:00:00 2001 From: Pekka Iso-Ahola Date: Mon, 2 Nov 2020 12:35:54 +0200 Subject: [PATCH 3/3] Replaced usage of ServiceUrlProvider#getBaseUrl with queryContext#getBaseUrl as both respect the domain-name configuration. --- .../core/engine/internal/document/mapper/DocumentMapper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 808139cfd..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 @@ -145,8 +145,8 @@ private LinksInformation enrichSelfLink(LinksInformation linksInformation, Query linksInformation = selfLinksInformation; } - String selfLinkUrl = UrlUtils.concat(queryAdapter.getResourceRegistry().getServiceUrlProvider().getUrl(), requestContext.getPath()); - JsonApiUrlBuilder.UrlParameterBuilder urlBuilder = new JsonApiUrlBuilder.UrlParameterBuilder(selfLinkUrl); + JsonApiUrlBuilder.UrlParameterBuilder urlBuilder = new JsonApiUrlBuilder.UrlParameterBuilder(UrlUtils.concat(queryAdapter.getQueryContext().getBaseUrl(), + requestContext.getPath())); urlBuilder.addQueryParameters(requestContext.getRequestParameters()); selfLinksInformation.setSelf(urlBuilder.toString()); }