Skip to content
Merged
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
105 changes: 105 additions & 0 deletions docs/customize/code/code-regions/java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: Custom code regions in Java
description: "Learn how to use custom code regions in Java SDKs."
---

# Custom code regions in Java

To enable custom code regions for Java SDKs, update the project's
`.speakeasy/gen.yaml` file like so:

```diff .speakeasy/gen.yaml
configVersion: 2.0.0
generation:
# ...
java:
# ...
+ enableCustomCodeRegions: true
```

## Full example

The Speakeasy examples repository has a [full example](https://github.com/speakeasy-api/examples/tree/main/customcode-sdkclasses-java) of a Java SDK
that uses custom code regions.


## Regions

Below are the available code regions in Java SDKs.

### SDK classes

Java SDK classes can have two code regions:

* `// #region imports` - allows the addition of imports to an SDK file needed for
custom methods and properties. This region must be located at the top of the
file alongside generated imports.
* `// #region sdk-class-body` - allows the addition of custom methods and
properties to an SDK class. This region must be located in the body of a Java
SDK class alongside generated methods and properties.

## Managing dependencies

When adding custom code that requires external packages, configure these dependencies in the `.speakeasy/gen.yaml` file to prevent them from being removed during SDK regeneration. Use the `additionalDependencies` configuration to specify package dependencies:

```yaml .speakeasy/gen.yaml
java:
additionalDependencies:
- implementation:org.commonmark:commonmark:0.21.0
- implementation:org.jsoup:jsoup:1.16.1
- testImplementation:org.junit.jupiter:junit-jupiter:5.9.2
- testImplementation:org.mockito:mockito-core:5.1.1
```

This ensures dependencies persist across SDK regenerations and are properly included in the generated `build.gradle`.

```java src/main/java/com/example/sdk/Todos.java
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/

package com.example.sdk;

import com.example.sdk.models.operations.*;
import com.example.sdk.models.shared.*;
import com.example.sdk.utils.HTTPClient;
import com.example.sdk.utils.HTTPRequest;
import com.example.sdk.utils.Utils;

// !focus(1:3)
// #region imports
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
// #endregion imports

public class Todos implements
MethodCallCreate,
MethodCallGetOne {

private final SDKConfiguration sdkConfiguration;

Todos(SDKConfiguration sdkConfiguration) {
this.sdkConfiguration = sdkConfiguration;
}

// !focus(1:11)
// #region sdk-class-body
public String renderTodo(String id) throws Exception {
Todo todo = getOne(id).todo()
.orElseThrow(() -> new Exception("Todo not found"));

Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();

String markdown = String.format("# %s\n\n%s", todo.title(), todo.description());

return renderer.render(parser.parse(markdown));
}
// #endregion sdk-class-body

public Todo getOne(String id) throws Exception {
// Generated method implementation
...
}
}
```
1 change: 1 addition & 0 deletions docs/customize/code/code-regions/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ for [creating code folds](https://code.visualstudio.com/docs/editor/codebasics#_

Custom code regions are currently supported in the following languages:

- [Java](/docs/customize/code/code-regions/java)
- [Python](/docs/customize/code/code-regions/python)
- [TypeScript](/docs/customize/code/code-regions/typescript)
Loading