Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified DocumentMapper to use ServiceUrlProvider for generating document self links #793

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what isnwring with the request uri? maybe a fix should be applied there if necessary. currently the code is inconsitent, having null check for resourceuri, but does not make use it.

requestContext.getPath()));
urlBuilder.addQueryParameters(requestContext.getRequestParameters());
selfLinksInformation.setSelf(urlBuilder.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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.<String, Set<String>>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 {
Expand Down