Skip to content

Conversation

@S-Saranya1
Copy link
Contributor

Add code examples near the individual method documentation in Java SDK API Refs guide

Motivation and Context

Enhance the Java SDK API Refs documentation to include code examples.

Modifications

  • Added createLinkToCodeExample method to DocumentationUtils.java to generate code example links from example-meta.json

  • Updated OperationDocProvider.java which is in codegen and responsible for generating Javadoc documentation for individual operation methods to include "Code Example" links in method @see sections, appearing after AWS API Documentation links

Testing

  • Ran the inject_examples.py script locally
  • Generated javadoc documentation for test services
  • Opened the package-summary.html files in browser and verified code examples sections are properly displayed
  • Confirmed examples are organized by categories and links work correctly

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@S-Saranya1 S-Saranya1 requested a review from a team as a code owner December 15, 2025 22:58
* @param operationName the name of the operation to find an example for
* @return a '@see also' HTML link to the code example, or empty string if no example found
*/
public static String createLinkToCodeExample(Metadata metadata, String operationName) {
Copy link
Contributor

@zoewangg zoewangg Dec 16, 2025

Choose a reason for hiding this comment

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

Can we add some codegen tests? we can create a mock example.json file

Copy link
Contributor

Choose a reason for hiding this comment

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

Any updates?

return String.format("<a href=\"%s\" target=\"_top\">Code Example</a>", url);
}

return "";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just use null to indicate there's no example, so here, we'd return Optional.empty()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, we're not using null here. we're returning an empty string ("") right, so based on the check we have in OperationDocProvider.java, it wont show example when its empty string.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I was suggesting using null to denote lack of example instead of empty string

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
30.9% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

docBuilder.see(crosslink);
}

String exampleMetaPath = "software/amazon/awssdk/codegen/example-meta.json";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we create a constant?

javadoc.append("<p>The following code examples show how to use this service with the AWS SDK for Java v2:</p>")
.append("\n");

Map<String, String> categoryMapping = new java.util.LinkedHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we initialize this map in ctor?

return String.format("<a href=\"%s\" target=\"_top\">Code Example</a>", url);
}

return "";
Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I was suggesting using null to denote lack of example instead of empty string

Comment on lines 27 to 30
/**
* Emits the package-info.java for the base service package. Includes the service
* level documentation.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we update javadoc?

Comment on lines +72 to +81
StringBuilder result = new StringBuilder();
String[] lines = codeExamplesJavadoc.split("\n");
for (String line : lines) {
if (!line.trim().isEmpty()) {
result.append(line);
if (!line.equals(lines[lines.length - 1])) {
result.append(System.lineSeparator()).append(" * ");
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not straighforward what this block is trying to do. Can we create a separate method with documentation?



private String generateCodeExamplesJavadoc(List<DocumentationUtils.ExampleData> examples) {
Map<String, List<DocumentationUtils.ExampleData>> categorizedExamples = new java.util.LinkedHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's replace qualified name with import. new java.util.LinkedHashMap<>(); -> new LinkedHashMap<>())

Can be shortened to the following:

        Map<String, List<DocumentationUtils.ExampleData>> categorizedExamples = 
            examples.stream().collect(Collectors.groupingBy(DocumentationUtils.ExampleData::getCategory, 
                                                            LinkedHashMap::new, 
                                                            Collectors.toList()));

Comment on lines +105 to +122
for (Map.Entry<String, String> entry : categoryMapping.entrySet()) {
String category = entry.getKey();
String displayName = entry.getValue();
List<DocumentationUtils.ExampleData> categoryExamples = categorizedExamples.get(category);
if (categoryExamples != null && !categoryExamples.isEmpty()) {
appendCategorySection(javadoc, displayName, categoryExamples);
}
}

for (Map.Entry<String, List<DocumentationUtils.ExampleData>> entry : categorizedExamples.entrySet()) {
String category = entry.getKey();
if (!categoryMapping.containsKey(category)) {
List<DocumentationUtils.ExampleData> categoryExamples = entry.getValue();
if (!categoryExamples.isEmpty()) {
appendCategorySection(javadoc, category, categoryExamples);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not very straighforward what those two loop do. Can we create separat methods with descriptive method names (ideally with javadocs).

Comment on lines +70 to +71
private static Map<String, JsonNode> serviceNodeCache;
private static Map<String, String> normalizedServiceKeyMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't seem thread safe. Suggesting creating a class instead of using static util methods for the new functionalities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants