Skip to content

Commit

Permalink
[OLINGO-1573] Add next link support on expanded navigation
Browse files Browse the repository at this point in the history
All the credit goes to [email protected] for writing this patch.
  • Loading branch information
Scraylex authored and mibo committed Oct 13, 2023
1 parent 0734e23 commit 1cb2907
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.olingo.commons.api.data.ComplexValue;
import org.apache.olingo.commons.api.data.ContextURL;
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.data.EntityIterator;
import org.apache.olingo.commons.api.data.Link;
import org.apache.olingo.commons.api.data.Linked;
Expand Down Expand Up @@ -654,12 +655,18 @@ protected void writeExpandedNavigationProperty(
json.writeStartArray();
json.writeEndArray();
} else {
final EntityCollection inlineEntitySet = navigationLink.getInlineEntitySet();
if (innerCount != null && innerCount.getValue()) {
writeInlineCount(property.getName(), navigationLink.getInlineEntitySet().getCount(), json);
writeInlineCount(property.getName(), inlineEntitySet.getCount(), json);
}
json.writeFieldName(property.getName());
writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand, toDepth,
writeEntitySet(metadata, property.getType(), inlineEntitySet, innerExpand, toDepth,
innerSelect, writeOnlyRef, ancestors, name, json);

final URI nextLink = inlineEntitySet.getNext();
if (nextLink != null) {
json.writeStringField(navigationLink.getTitle() + constants.getNextLink(), nextLink.toASCIIString());
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -92,6 +94,7 @@
import org.apache.olingo.server.tecsvc.data.DataProvider;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -1700,6 +1703,45 @@ public void expandStarTwoLevels() throws Exception {
resultString);
}

@Test
public void expandWithNextLink() throws Exception {

final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim");
final EdmEntityType entityType = edmEntitySet.getEntityType();
final EdmEntitySet innerEntitySet = entityContainer.getEntitySet("ESAllPrim");
final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
final EntityCollection innerEntities = data.readAll(innerEntitySet);
innerEntities.setNext(URI.create("/next"));
final ExpandItem expandItem = ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimMany");
final List<ExpandItem> expandItems = new ArrayList<>();
expandItems.add(expandItem);

final ExpandOption expandOption = ExpandSelectMock.mockExpandOption(expandItems);
final Link link = new Link();
link.setTitle("NavPropertyETAllPrimMany");
link.setInlineEntitySet(innerEntities);
entity.getNavigationLinks().add(link);


final ContextURL build = ContextURL.with().entitySet(edmEntitySet)
.selectList(helper.buildContextURLSelectList(entityType, expandOption, null))
.suffix(Suffix.ENTITY).build();
final String resultString = IOUtils.toString(serializerV401
.entity(metadata, entityType, entity,
EntitySerializerOptions.with()
.contextURL(build)
.expand(expandOption)
.build()).getContent(), Charset.defaultCharset());


final Matcher<String> stringMatcher = CoreMatchers.endsWith("}],"
+ "\"NavPropertyETAllPrimMany@nextLink\":\"/next\"}");

final boolean matchResult = stringMatcher.matches(resultString);
Assert.assertTrue(matchResult);

}

@Test
public void primitiveProperty() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
Expand Down

0 comments on commit 1cb2907

Please sign in to comment.