Skip to content

Commit cae2df3

Browse files
Always run IO structure reuse validator
This adds a validation step to the plugin that runs the IO structure reuse validator at the danger level, adding some additional context for how it impacts docs specifically. This also removes the invocation to always create dedicated input and output shapes.
1 parent 5e3b480 commit cae2df3

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

smithy-docgen-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ dependencies {
1616
implementation("software.amazon.smithy:smithy-model:$smithyVersion")
1717
implementation("software.amazon.smithy:smithy-utils:$smithyVersion")
1818
implementation("software.amazon.smithy:smithy-codegen-core:$smithyVersion")
19+
implementation("software.amazon.smithy:smithy-linters:$smithyVersion")
1920
}

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/DirectedDocGen.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package software.amazon.smithy.docgen.core;
77

8-
import java.util.logging.Logger;
98
import software.amazon.smithy.codegen.core.SymbolProvider;
109
import software.amazon.smithy.codegen.core.directed.CreateContextDirective;
1110
import software.amazon.smithy.codegen.core.directed.CreateSymbolProviderDirective;
@@ -27,8 +26,6 @@
2726
@SmithyUnstableApi
2827
final class DirectedDocGen implements DirectedCodegen<DocGenerationContext, DocSettings, DocIntegration> {
2928

30-
private static final Logger LOGGER = Logger.getLogger(DirectedDocGen.class.getName());
31-
3229
@Override
3330
public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective<DocSettings> directive) {
3431
return new DocSymbolProvider(directive.model(), directive.settings());

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/SmithyDocPlugin.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
package software.amazon.smithy.docgen.core;
77

88

9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Optional;
912
import java.util.logging.Logger;
1013
import software.amazon.smithy.build.PluginContext;
1114
import software.amazon.smithy.build.SmithyBuildPlugin;
1215
import software.amazon.smithy.codegen.core.directed.CodegenDirector;
16+
import software.amazon.smithy.docgen.core.validation.DocValidationEventDecorator;
1317
import software.amazon.smithy.docgen.core.writers.DocWriter;
18+
import software.amazon.smithy.linters.InputOutputStructureReuseValidator;
19+
import software.amazon.smithy.model.Model;
20+
import software.amazon.smithy.model.validation.ValidatedResult;
21+
import software.amazon.smithy.model.validation.ValidationEvent;
22+
import software.amazon.smithy.model.validation.ValidationEventDecorator;
23+
import software.amazon.smithy.model.validation.suppressions.ModelBasedEventDecorator;
1424
import software.amazon.smithy.utils.SmithyInternalApi;
1525

1626
/**
@@ -29,20 +39,44 @@ public String getName() {
2939
@Override
3040
public void execute(PluginContext pluginContext) {
3141
LOGGER.fine("Beginning documentation generation.");
32-
DocSettings docSettings = DocSettings.from(pluginContext.getSettings());
42+
DocSettings settings = DocSettings.from(pluginContext.getSettings());
3343

3444
CodegenDirector<DocWriter, DocIntegration, DocGenerationContext, DocSettings> runner
3545
= new CodegenDirector<>();
3646

3747
runner.directedCodegen(new DirectedDocGen());
3848
runner.integrationClass(DocIntegration.class);
3949
runner.fileManifest(pluginContext.getFileManifest());
40-
runner.model(pluginContext.getModel());
41-
runner.settings(docSettings);
42-
runner.service(docSettings.service());
50+
runner.model(getValidatedModel(pluginContext.getModel()).unwrap());
51+
runner.settings(settings);
52+
runner.service(settings.service());
4353
runner.performDefaultCodegenTransforms();
44-
runner.createDedicatedInputsAndOutputs();
4554
runner.run();
4655
LOGGER.fine("Finished documentation generation.");
4756
}
57+
58+
private ValidatedResult<Model> getValidatedModel(Model model) {
59+
// This decorator will add context for why these are particularly important for docs.
60+
ValidationEventDecorator eventDecorator = new DocValidationEventDecorator();
61+
62+
// This will discover and apply suppressions from the model.
63+
Optional<ValidationEventDecorator> modelDecorator = new ModelBasedEventDecorator()
64+
.createDecorator(model).getResult();
65+
if (modelDecorator.isPresent()) {
66+
eventDecorator = ValidationEventDecorator.compose(List.of(modelDecorator.get(), eventDecorator));
67+
}
68+
69+
var events = new ArrayList<ValidationEvent>();
70+
for (var event : validate(model)) {
71+
if (eventDecorator.canDecorate(event)) {
72+
event = eventDecorator.decorate(event);
73+
}
74+
events.add(event);
75+
}
76+
return new ValidatedResult<>(model, events);
77+
}
78+
79+
private List<ValidationEvent> validate(Model model) {
80+
return new InputOutputStructureReuseValidator().validate(model);
81+
}
4882
}

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/sections/ShapeMembersSection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
@SmithyUnstableApi
2727
public record ShapeMembersSection(
28-
2928
DocGenerationContext context,
3029
Shape shape,
3130
Collection<MemberShape> members,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.validation;
7+
8+
import software.amazon.smithy.model.validation.Severity;
9+
import software.amazon.smithy.model.validation.ValidationEvent;
10+
import software.amazon.smithy.model.validation.ValidationEventDecorator;
11+
import software.amazon.smithy.utils.SmithyInternalApi;
12+
13+
/**
14+
* Adds context to validation events describing how they impact docs.
15+
*/
16+
@SmithyInternalApi
17+
public class DocValidationEventDecorator implements ValidationEventDecorator {
18+
private static final String REUSE_DOCUMENTATION_CONTEXT = """
19+
Additionally, reusing your input and output structures can make your \
20+
documentation confusing for customers, because they'll see those \
21+
structures both as the inputs or outputs of your operation and as \
22+
standalone structures. This can be particularly confusing if not all of \
23+
your operation inputs and outputs do this.""";
24+
25+
@Override
26+
public boolean canDecorate(ValidationEvent event) {
27+
return event.containsId("InputOutputStructureReuse");
28+
}
29+
30+
@Override
31+
public ValidationEvent decorate(ValidationEvent event) {
32+
return event.toBuilder()
33+
.message(event.getMessage() + " " + REUSE_DOCUMENTATION_CONTEXT)
34+
.severity(Severity.DANGER)
35+
.build();
36+
}
37+
}

0 commit comments

Comments
 (0)