Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @externalDocumentation rendering #2263

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void append(TraitCodegenWriter writer, JavaDocSection section) {
// Add a space to make it easier to read
writer.writeDocStringContents("");
for (Map.Entry<String, String> entry : trait.getUrls().entrySet()) {
writer.writeDocStringContents("@see <a href=$S>$L</a>", entry.getKey(), entry.getValue());
writer.writeDocStringContents("@see <a href=$S>$L</a>", entry.getValue(), entry.getKey());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
Expand All @@ -31,24 +30,7 @@ public class TraitCodegenPluginTest {

@Test
public void generatesExpectedTraitFiles() {
MockManifest manifest = new MockManifest();
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.assemble()
.unwrap();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.settings(ObjectNode.builder()
.withMember("package", "com.example.traits")
.withMember("namespace", "test.smithy.traitcodegen")
.withMember("header", ArrayNode.fromStrings("Header line One"))
.build()
)
.model(model)
.build();

SmithyBuildPlugin plugin = new TraitCodegenPlugin();
plugin.execute(context);
MockManifest manifest = runTraitCodegen(Collections.singletonList("Header line One"), Collections.emptyList());

assertFalse(manifest.getFiles().isEmpty());
assertEquals(EXPECTED_NUMBER_OF_FILES, manifest.getFiles().size());
Expand All @@ -63,50 +45,15 @@ public void generatesExpectedTraitFiles() {

@Test
public void filtersTags() {
MockManifest manifest = new MockManifest();
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.assemble()
.unwrap();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.settings(ObjectNode.builder()
.withMember("package", "com.example.traits")
.withMember("namespace", "test.smithy.traitcodegen")
.withMember("header", ArrayNode.fromStrings("Header line One"))
.withMember("excludeTags", ArrayNode.fromStrings("filterOut"))
.build()
)
.model(model)
.build();

SmithyBuildPlugin plugin = new TraitCodegenPlugin();
plugin.execute(context);
MockManifest manifest = runTraitCodegen(Collections.singletonList("Header line One"), Collections.singletonList("filterOut"));

assertFalse(manifest.getFiles().isEmpty());
assertEquals(EXPECTED_NUMBER_OF_FILES - 1, manifest.getFiles().size());
}

@Test
public void addsHeaderLines() {
MockManifest manifest = new MockManifest();
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.assemble()
.unwrap();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.settings(ObjectNode.builder()
.withMember("package", "com.example.traits")
.withMember("namespace", "test.smithy.traitcodegen")
.withMember("header", ArrayNode.fromStrings("Header line one", "Header line two"))
.build()
)
.model(model)
.build();

SmithyBuildPlugin plugin = new TraitCodegenPlugin();
plugin.execute(context);
MockManifest manifest = runTraitCodegen(Arrays.asList("Header line one", "Header line two"), Collections.emptyList());

assertFalse(manifest.getFiles().isEmpty());
assertEquals(EXPECTED_NUMBER_OF_FILES, manifest.getFiles().size());
Expand All @@ -121,24 +68,7 @@ public void addsHeaderLines() {

@Test
public void doesNotFormatContentInsideHtmlTags() {
MockManifest manifest = new MockManifest();
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.assemble()
.unwrap();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.settings(ObjectNode.builder()
.withMember("package", "com.example.traits")
.withMember("namespace", "test.smithy.traitcodegen")
.withMember("header", ArrayNode.fromStrings("Header line one", "Header line two"))
.build()
)
.model(model)
.build();

SmithyBuildPlugin plugin = new TraitCodegenPlugin();
plugin.execute(context);
MockManifest manifest = runTraitCodegen(Collections.singletonList("Header line One"), Collections.emptyList());

assertFalse(manifest.getFiles().isEmpty());
assertEquals(EXPECTED_NUMBER_OF_FILES, manifest.getFiles().size());
Expand All @@ -163,4 +93,43 @@ public void doesNotFormatContentInsideHtmlTags() {
+ " }\n";
assertTrue(fileStringOptional.get().contains(expected));
}

@Test
public void rendersExternalDocumentation() {
MockManifest manifest = runTraitCodegen(Collections.emptyList(), Collections.emptyList());

assertFalse(manifest.getFiles().isEmpty());
assertEquals(EXPECTED_NUMBER_OF_FILES, manifest.getFiles().size());
Optional<String> fileStringOptional = manifest.getFileString(
Paths.get("com/example/traits/structures/StructureTrait.java").toString());
assertTrue(fileStringOptional.isPresent());
String expectedStructureExternalDocs = "<a href=\"https://external.docs/structureTrait\">External docs</a>";
assertTrue(fileStringOptional.get().contains(expectedStructureExternalDocs));
// String expectedFieldExternalDocs = "<a href=\"https://external.docs/structureTrait#fieldA\">External docs</a>";
// assertTrue(fileStringOptional.get().contains(expectedFieldExternalDocs));
Copy link
Contributor Author

@msosnicki msosnicki Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made these just for demonstration purposes, can revert as this is not related to this PR. Uncommenting these lines will make the test fail, even though there is

    @externalDocumentation(
        "External docs": "https://external.docs/structureTrait#fieldA"
    )
    fieldA: String

To make it work, it has to contain a documentation section:

    /// some other docs
    @externalDocumentation(
        "External docs": "https://external.docs/structureTrait#fieldA"
    )
    fieldA: String

I believe this is because how the interceptors are implemented, the ExternalDocsInterceptor has

    @Override
    public Class<JavaDocSection> sectionType() {
        return JavaDocSection.class;
    }

but this condition does not hold in the failing case.

I can raise an issue if you also feel that this is a problem

}

private MockManifest runTraitCodegen(List<String> headers, List<String> filters) {
MockManifest manifest = new MockManifest();
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.assemble()
.unwrap();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.settings(ObjectNode.builder()
.withMember("package", "com.example.traits")
.withMember("namespace", "test.smithy.traitcodegen")
.withMember("header", ArrayNode.fromStrings(headers))
.withMember("excludeTags", ArrayNode.fromStrings(filters))
.build()
)
.model(model)
.build();

SmithyBuildPlugin plugin = new TraitCodegenPlugin();
plugin.execute(context);
return manifest;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ $version: "2.0"

namespace test.smithy.traitcodegen.structures

/// structureTrait docs
@externalDocumentation(
"External docs": "https://external.docs/structureTrait"
)
@trait
structure structureTrait {

@externalDocumentation(
"External docs": "https://external.docs/structureTrait#fieldA"
)
@required
@pattern("^[^#+]+$")
fieldA: String
Expand Down
Loading