Skip to content

Commit 9dacbfb

Browse files
Add pagination information
1 parent 368609d commit 9dacbfb

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/integrations/BuiltinsIntegration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import software.amazon.smithy.docgen.core.interceptors.NoReplaceBindingInterceptor;
2121
import software.amazon.smithy.docgen.core.interceptors.NoReplaceOperationInterceptor;
2222
import software.amazon.smithy.docgen.core.interceptors.NullabilityInterceptor;
23+
import software.amazon.smithy.docgen.core.interceptors.PaginationInterceptor;
2324
import software.amazon.smithy.docgen.core.interceptors.PatternInterceptor;
2425
import software.amazon.smithy.docgen.core.interceptors.RangeInterceptor;
2526
import software.amazon.smithy.docgen.core.interceptors.RecommendedInterceptor;
@@ -70,6 +71,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
7071
// the ones at the end will be at the top of the rendered pages. Therefore, interceptors
7172
// that provide more critical information should appear at the bottom of this list.
7273
return List.of(
74+
new PaginationInterceptor(),
7375
new RequestCompressionInterceptor(),
7476
new NoReplaceBindingInterceptor(),
7577
new NoReplaceOperationInterceptor(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.docgen.core.interceptors;
7+
8+
import software.amazon.smithy.codegen.core.SymbolReference;
9+
import software.amazon.smithy.docgen.core.sections.ShapeDetailsSection;
10+
import software.amazon.smithy.docgen.core.writers.DocWriter;
11+
import software.amazon.smithy.docgen.core.writers.DocWriter.AdmonitionType;
12+
import software.amazon.smithy.model.knowledge.PaginatedIndex;
13+
import software.amazon.smithy.model.traits.PaginatedTrait;
14+
import software.amazon.smithy.utils.CodeInterceptor;
15+
16+
/**
17+
* This adds pagination information to operation docs.
18+
*/
19+
public final class PaginationInterceptor implements CodeInterceptor<ShapeDetailsSection, DocWriter> {
20+
@Override
21+
public Class<ShapeDetailsSection> sectionType() {
22+
return ShapeDetailsSection.class;
23+
}
24+
25+
@Override
26+
public boolean isIntercepted(ShapeDetailsSection section) {
27+
return section.shape().isOperationShape() && section.shape().hasTrait(PaginatedTrait.class);
28+
}
29+
30+
@Override
31+
public void write(DocWriter writer, String previousText, ShapeDetailsSection section) {
32+
var paginatedIndex = PaginatedIndex.of(section.context().model());
33+
var service = section.context().settings().service();
34+
var paginationInfo = paginatedIndex.getPaginationInfo(service, section.shape()).get();
35+
var symbolProvider = section.context().symbolProvider();
36+
writer.putContext("size", paginationInfo.getPageSizeMember().map(symbolProvider::toSymbol));
37+
writer.putContext("inputToken", SymbolReference.builder()
38+
.symbol(symbolProvider.toSymbol(paginationInfo.getInputTokenMember()))
39+
.alias("input token")
40+
.build());
41+
42+
var outputTokenPath = paginationInfo.getOutputTokenMemberPath();
43+
var outputToken = outputTokenPath.get(outputTokenPath.size() - 1);
44+
writer.putContext("outputToken", SymbolReference.builder()
45+
.symbol(symbolProvider.toSymbol(outputToken))
46+
.alias("output token")
47+
.build());
48+
49+
writer.openAdmonition(AdmonitionType.IMPORTANT);
50+
writer.write("""
51+
This operation returns partial results in pages${?size}, whose maximum size may be
52+
configured with ${size:R}${/size}. Each request may return an ${outputToken:R} that \
53+
may be used as an ${inputToken:R} in subsequent requests to fetch the next page of results. \
54+
If the operation does not return an ${outputToken:R}, that means that there are \
55+
no more results. If the operation returns a repeated ${outputToken:R}, there MAY be \
56+
more results later.""");
57+
writer.closeAdmonition();
58+
writer.writeWithNoFormatting(previousText);
59+
}
60+
}

smithy-docgen-test/model/main.smithy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,22 @@ operation DeleteArchivedDocumentation {}
344344

345345
/// Lists the avialable documentation resources.
346346
@readonly
347+
@paginated(inputToken: "paginationToken", outputToken: "paginationToken", items: "documentation", pageSize: "pageSize")
347348
operation ListDocumentation {
348349
input := {
349350
// Whether to list documentation that has been archived.
350351
showArchived: Boolean = false
352+
353+
paginationToken: String
354+
355+
pageSize: Integer
351356
}
352357

353358
output := {
354-
/// A list of all the documentation. No pagination here.
355359
@required
356360
documentation: DocumentationList
361+
362+
paginationToken: String
357363
}
358364
}
359365

0 commit comments

Comments
 (0)