Speakeasy Logo
Skip to Content

Custom code regions in Java

To enable custom code regions for Java SDKs, update the project’s .speakeasy/gen.yaml file as follows:

Full example

The Speakeasy examples repository includes a full 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: The imports region allows you to add imports to an SDK file needed for custom methods and properties. It must be located at the top of the file alongside generated imports.
  • // #region class-body: The class-body region allows you to add custom methods and properties to an SDK class. It must be located in the body of a Java SDK class alongside generated methods and properties.

Model classes

Java model classes can also have custom code regions:

  • // #region imports: The imports region allows you to add imports to a model file needed for custom methods and properties. It must be located at the top of the file alongside generated imports.
  • // #region class-body: The class-body region allows you to add custom methods and properties to a model class. It must be located in the body of a Java model 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:

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

Model class example

You can also add custom methods to model classes. This example adds a render() method to a Todo model class:

This allows you to use the custom method as follows:

Last updated on