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
*/
private static String mapServiceNameToExampleKey(String serviceName) {
if (serviceName == null) {
return "";
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.

Question: in what scenario would service name be null?

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, the service name won't be null anytime, we don't need this check, removed it.

return urlMap;
}

ObjectMapper objectMapper = new ObjectMapper();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: create a constant since creating object mapper is expensive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

* @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

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.

Comment on lines 259 to 276
if (servicesNode != null) {
for (JsonNode serviceNode : servicesNode) {
JsonNode examplesNode = serviceNode.get("examples");
if (examplesNode != null && examplesNode.isArray()) {
for (JsonNode example : examplesNode) {
JsonNode idNode = example.get("id");
JsonNode urlNode = example.get("url");

if (idNode != null && urlNode != null) {
String id = idNode.asText();
String url = urlNode.asText();
if (!id.isEmpty() && !url.isEmpty()) {
urlMap.put(id, url);
}
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit hard to read, let's break up this a bit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, Updated it!

Comment on lines 226 to 228
return serviceName
.replaceAll("([a-z0-9])([A-Z])", "$1-$2")
.toLowerCase(Locale.ROOT);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to double check, if this the naming pattern example team has been using?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Realized it was missing a few edge cases, so I refactored the code here a bit.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarQube Cloud

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