Skip to content

Commit 48015b0

Browse files
Add base httpLabel trait support
1 parent 6be6b21 commit 48015b0

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
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
@@ -16,6 +16,7 @@
1616
import software.amazon.smithy.docgen.core.interceptors.ErrorFaultInterceptor;
1717
import software.amazon.smithy.docgen.core.interceptors.ExternalDocsInterceptor;
1818
import software.amazon.smithy.docgen.core.interceptors.HttpInterceptor;
19+
import software.amazon.smithy.docgen.core.interceptors.HttpLabelInterceptor;
1920
import software.amazon.smithy.docgen.core.interceptors.HttpQueryInterceptor;
2021
import software.amazon.smithy.docgen.core.interceptors.IdempotencyInterceptor;
2122
import software.amazon.smithy.docgen.core.interceptors.InternalInterceptor;
@@ -90,6 +91,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
9091
new XmlNameInterceptor(),
9192
new XmlFlattenedInterceptor(),
9293
new HttpQueryInterceptor(),
94+
new HttpLabelInterceptor(),
9395
new HttpInterceptor(),
9496
new PaginationInterceptor(),
9597
new RequestCompressionInterceptor(),

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/interceptors/HttpInterceptor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ protected ShapeId getTraitId() {
3030

3131
@Override
3232
void write(DocWriter writer, String previousText, ProtocolSection section, HttpTrait trait) {
33+
writer.putContext("hasLabels", !trait.getUri().getLabels().isEmpty());
3334
writer.write("""
3435
$B $`
3536
3637
$B $`
38+
${?hasLabels}
39+
40+
To resolve the path segment of the URI, replace any segments surrounded with
41+
brackets with the URI-escaped value of the corresponding member.
42+
${/hasLabels}
3743
3844
$L""", "HTTP Method:", trait.getMethod(), "URI:", trait.getUri(), previousText);
3945
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 java.util.Optional;
9+
import software.amazon.smithy.docgen.core.DocGenerationContext;
10+
import software.amazon.smithy.docgen.core.sections.ProtocolSection;
11+
import software.amazon.smithy.docgen.core.writers.DocWriter;
12+
import software.amazon.smithy.model.shapes.OperationShape;
13+
import software.amazon.smithy.model.shapes.Shape;
14+
import software.amazon.smithy.model.shapes.ShapeId;
15+
import software.amazon.smithy.model.traits.HttpLabelTrait;
16+
import software.amazon.smithy.model.traits.HttpTrait;
17+
import software.amazon.smithy.utils.SmithyInternalApi;
18+
19+
/**
20+
* Adds information about label bindings from the
21+
* <a href="https://smithy.io/2.0/spec/http-bindings.html#httplabel-trait">
22+
* httpLabel trait</a> if the protocol supports it.
23+
*/
24+
@SmithyInternalApi
25+
public final class HttpLabelInterceptor extends ProtocolTraitInterceptor<HttpLabelTrait> {
26+
@Override
27+
protected Class<HttpLabelTrait> getTraitClass() {
28+
return HttpLabelTrait.class;
29+
}
30+
31+
@Override
32+
protected ShapeId getTraitId() {
33+
return HttpLabelTrait.ID;
34+
}
35+
36+
@Override
37+
void write(DocWriter writer, String previousText, ProtocolSection section, HttpLabelTrait trait) {
38+
writer.putContext("greedy", getOperation(section.context(), section.shape())
39+
.map(operation -> operation.expectTrait(HttpTrait.class))
40+
.flatMap(httpTrait -> httpTrait.getUri().getGreedyLabel())
41+
.map(segment -> segment.getContent().equals(section.shape().getId().getName()))
42+
.orElse(false));
43+
var segment = "{" + section.shape().getId().getName() + "}";
44+
writer.write("""
45+
This is bound to the path of the URI. Its value should be URI-escaped and \
46+
and inserted in place of the $` segment.\
47+
${?greedy}
48+
When escaping this value, do not escape any backslashes ($`).
49+
${/greedy}
50+
51+
$L""", segment, "/", previousText);
52+
}
53+
54+
private Optional<OperationShape> getOperation(DocGenerationContext context, Shape shape) {
55+
var member = shape.asMemberShape().get();
56+
for (var operation : context.model().getOperationShapes()) {
57+
if (operation.getInputShape().equals(member.getContainer())) {
58+
return Optional.of(operation);
59+
}
60+
}
61+
return Optional.empty();
62+
}
63+
}

0 commit comments

Comments
 (0)