microsoftgraph / microsoftgraph/msgraph-sdk-java

Boiler plate when iterating through collections

Open
#1,965 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:enhancement
Dominant language
Java
Stars
444
Forks
154
Avg merge
18h 28m
Merged PRs (30d)
4

Description

Hello, i have a small question.

https://github.com/microsoftgraph/msgraph-sdk-java/blob/dev/docs/upgrade-to-v6.md#pageiterator

  • With the new page iterator there is a boilerplate associated with each request and iterating through it. Since a new page iterator would need to be created for each request.
  • I was wondering if there is a generic way to create an iterator to run on collection requests and delta collection requests.

So I created this code, from looking at the generated code in the msft graph sdk:

  public static <T extends Parsable> void genericIterator(
      GraphServiceClient graphClient,
      BaseDeltaFunctionResponse res,
      Consumer<T> processEntity, 
      Consumer<String> processDeltaLink) {
    PageIterator<T, BaseDeltaFunctionResponse> pageIterator;
    try {
      pageIterator = new PageIterator.Builder<T, BaseDeltaFunctionResponse>()
              .client(graphClient)
              .collectionPage(res)
              .collectionPageFactory(BaseDeltaFunctionResponse::createFromDiscriminatorValue)
              .processPageItemCallback(entity -> {
                processEntity.accept(entity);
                return true; 
              }).build();
      pageIterator.iterate();
      processDeltaLink.accept(pageIterator.getDeltaLink());
    } catch (ApiException | ReflectiveOperationException e) {
      log.error("Error ", e);
    }
  }

  public static <T extends Parsable> void genericIterator(
      GraphServiceClient graphClient,
      BaseCollectionPaginationCountResponse res,
      Consumer<T> processGroupOwner) {
    PageIterator<T, BaseCollectionPaginationCountResponse> pageIterator;
    try {
      pageIterator = new PageIterator.Builder<T, BaseCollectionPaginationCountResponse>()
              .client(graphClient)
              .collectionPage(res)
              .collectionPageFactory(BaseCollectionPaginationCountResponse::createFromDiscriminatorValue)
              .processPageItemCallback(entity -> {
                processGroupOwner.accept(entity);
                return true; 
              }).build();
      pageIterator.iterate();
    } catch (ApiException | ReflectiveOperationException e) {
      log.error("Error ", e);
    }
  }

This second method works with general collectors, but not the first one for delta collections.

I was wondering if this is even recommending considering that the sdk is based on code generations and/or if there is a way to iterate over collections in a more concise manner than the current page iterator.

Error received from the generic delta collection iterator:

DeltaGetResponse res2 = graphServiceClient.groups().delta().get();
genericIterator(graphServiceClient, res2, (Group group) -> {
  log.info("Group: {} {}", group.getId(), group.getDisplayName());
}, (String deltaLink) -> {
  log.info("DeltaLink: {}", deltaLink);
});
java.lang.IllegalAccessException: NO_COLLECTION_PROPERTY_ERROR
        at com.microsoft.graph.core.tasks.PageIterator.extractEntityListFromParsable(PageIterator.java:300)
        at com.microsoft.graph.core.tasks.PageIterator.interpageIterate(PageIterator.java:250)
        at com.microsoft.graph.core.tasks.PageIterator.iterate(PageIterator.java:272)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PageIterator implementation at com.microsoft.graph.core.tasks.PageIterator.java, especially extractEntityListFromParsable and the interpage iteration path, then review the PageIterator section of docs/upgrade-to-v6.md. Reproduce the generic delta example from the issue and determine whether collection and delta collection requests can share a concise iterator; done means the supported behavior and required API change are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.