Skip to content

Commit

Permalink
Fix Paging Samples That Produce NullPointerExceptions (#15997)
Browse files Browse the repository at this point in the history
* Fix paging samples that produce NullPointerExceptions

* Fix linting issue
  • Loading branch information
alzimmermsft authored Oct 7, 2020
1 parent 43e5f92 commit 7606ace
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void classDocSnippet() {

/**
* Code snippets for creating an instance of {@link PagedFluxBase}
*
* @return An instance of {@link PagedFluxBase}
*/
private PagedFluxBase<Integer, PagedResponse<Integer>> createAnInstance() {
Expand All @@ -82,8 +83,9 @@ private PagedFluxBase<Integer, PagedResponse<Integer>> createAnInstance() {
// A supplier that fetches the first page of data from source/service
Supplier<Mono<PagedResponse<Integer>>> firstPageRetrieverFunction = () -> getFirstPage();

PagedFluxBase<Integer, PagedResponse<Integer>> pagedFluxBaseInstance = new PagedFluxBase<>(firstPageRetrieverFunction,
nextPageRetriever);
PagedFluxBase<Integer, PagedResponse<Integer>> pagedFluxBaseInstance =
new PagedFluxBase<>(firstPageRetrieverFunction,
nextPageRetriever);
// END: com.azure.core.http.rest.pagedfluxbase.singlepage.instantiation
return pagedFluxBase;
}
Expand Down Expand Up @@ -128,22 +130,22 @@ public void byTSnippet() {

// BEGIN: com.azure.core.http.rest.pagedfluxbase.subscribe
pagedFluxBase.subscribe(new BaseSubscriber<Integer>() {
@Override
protected void hookOnSubscribe(Subscription subscription) {
System.out.println("Subscribed to paged flux processing items");
super.hookOnSubscribe(subscription);
}

@Override
protected void hookOnNext(Integer value) {
System.out.println("Processing item with value: " + value);
}

@Override
protected void hookOnComplete() {
System.out.println("Processing complete.");
}
});
@Override
protected void hookOnSubscribe(Subscription subscription) {
System.out.println("Subscribed to paged flux processing items");
super.hookOnSubscribe(subscription);
}

@Override
protected void hookOnNext(Integer value) {
System.out.println("Processing item with value: " + value);
}

@Override
protected void hookOnComplete() {
System.out.println("Processing complete.");
}
});
// END: com.azure.core.http.rest.pagedfluxbase.subscribe
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

package com.azure.core.http.rest;

import reactor.core.publisher.Mono;

import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Supplier;
import reactor.core.publisher.Mono;

/**
* Code snippets for {@link PagedIterableBase}
Expand Down Expand Up @@ -94,7 +95,7 @@ public void iterableByPageWhileSnippet() {
*
* @return An instance of {@link PagedFlux}
*/
public CustomPagedFlux<String> createCustomInstance() {
CustomPagedFlux<String> createCustomInstance() {

// A supplier that fetches the first page of data from source/service
Supplier<Mono<PagedResponse<String>>> firstPageRetriever = () -> null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

package com.azure.core.http.rest;

import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpRequest;
import reactor.core.publisher.Mono;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Code snippets for {@link PagedIterable}
Expand Down Expand Up @@ -91,28 +98,51 @@ public PagedFlux<Integer> createAnInstance() {
Function<String, Mono<PagedResponse<Integer>>> nextPageRetriever =
continuationToken -> getNextPage(continuationToken);

PagedFlux<Integer> pagedFlux = new PagedFlux<>(firstPageRetriever,
nextPageRetriever);
return pagedFlux;
return new PagedFlux<>(firstPageRetriever, nextPageRetriever);
}


/**
* Implementation not provided
* Retrieves the next page from a paged API.
*
* @param continuationToken Token to fetch the next page
* @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer}
*/
private Mono<PagedResponse<Integer>> getNextPage(String continuationToken) {
return null;
return getPage(continuationToken);
}

/**
* Implementation not provided
* Retrieves the initial page from a paged API.
*
* @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer}
*/
private Mono<PagedResponse<Integer>> getFirstPage() {
return null;
return getPage(null);
}

/**
* Retrieves a page from a paged API.
*
* @param continuationToken Token to fetch the next page, if {@code null} the first page is retrieved.
* @return A {@link Mono} of {@link PagedResponse} containing items of type {@code Integer}
*/
private Mono<PagedResponse<Integer>> getPage(String continuationToken) {
// Given this isn't calling an actual API we will arbitrarily generate a continuation token or end paging.
boolean lastPage = Math.random() > 0.5;

// If it is the last page there should be no additional continuation tokens returned.
String nextContinuationToken = lastPage ? null : UUID.randomUUID().toString();

// Arbitrarily begin the next page of integers.
int elementCount = (int) Math.ceil(Math.random() * 15);
List<Integer> elements = IntStream.range(elementCount, elementCount + elementCount)
.map(val -> (int) (Math.random() * val))
.boxed()
.collect(Collectors.toList());

// This is a rough approximation of a service response.
return Mono.just(new PagedResponseBase<Void, Integer>(new HttpRequest(HttpMethod.GET, "https://requestUrl.com"),
200, new HttpHeaders(), elements, nextContinuationToken, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.azure.core.util.IterableStream;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -40,44 +43,69 @@ boolean isDone() {
}

class FileContinuationToken {
private final int nextLinkId;

FileContinuationToken(int nextLinkId) {
this.nextLinkId = nextLinkId;
}

public int getNextLinkId() {
return 0;
return nextLinkId;
}
}

class File {
private final String guid;

File(String guid) {
this.guid = guid;
}

public String getGuid() {
return guid;
}
}

class FilePage implements ContinuablePage<FileContinuationToken, File> {
private final IterableStream<File> elements;
private final FileContinuationToken fileContinuationToken;

FilePage(List<File> elements, FileContinuationToken fileContinuationToken) {
this.elements = IterableStream.of(elements);
this.fileContinuationToken = fileContinuationToken;
}

@Override
public IterableStream<File> getElements() {
return null;
return elements;
}

@Override
public FileContinuationToken getContinuationToken() {
return null;
return fileContinuationToken;
}
}

class FileShareServiceClient {
Flux<FilePage> getFilePages(FileContinuationToken token) {
return null;
List<File> files = Collections.singletonList(new File(UUID.randomUUID().toString()));
if (token.getNextLinkId() < 10) {
return Flux.just(new FilePage(files, null));
} else {
return Flux.just(new FilePage(files,
new FileContinuationToken((int) Math.floor(Math.random() * 20))));
}
}
}
FileShareServiceClient client = null; // Initialize client

Supplier<PageRetriever<FileContinuationToken, FilePage>> pageRetrieverProvider
= new Supplier<PageRetriever<FileContinuationToken, FilePage>>() {
@Override
public PageRetriever<FileContinuationToken, FilePage> get() {
return (continuationToken, pageSize) -> client.getFilePages(continuationToken);
}
};
FileShareServiceClient client = new FileShareServiceClient();

Supplier<PageRetriever<FileContinuationToken, FilePage>> pageRetrieverProvider = () ->
(continuationToken, pageSize) -> client.getFilePages(continuationToken);

class FilePagedFlux extends ContinuablePagedFluxCore<FileContinuationToken, File, FilePage> {
FilePagedFlux(Supplier<PageRetriever<FileContinuationToken, FilePage>>
pageRetrieverProvider) {
pageRetrieverProvider) {
super(pageRetrieverProvider);
}
}
Expand Down

0 comments on commit 7606ace

Please sign in to comment.