microsoftgraph / microsoftgraph/msgraph-sdk-java

Boiler plate when iterating through collections

未關閉
#1,965 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type:enhancement
主要語言
Java
星號
444
分支
154
平均合併
18 小時 28 分鐘
30 天內合併 PR
4

描述

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)

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 com.microsoft.graph.core.tasks.PageIterator.java 中的 PageIterator 實作開始,特別查看 extractEntityListFromParsable 和頁間迭代路徑,然後檢視 docs/upgrade-to-v6.md 中的 PageIterator 區段。重現 issue 中的泛用 delta 範例,並判斷 collection 要求和 delta collection 要求是否能共用一個簡潔的 iterator;完成的標準是已釐清受支援的行為和必要的 API 變更。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
api
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。