Skip to content

Commit a1e5226

Browse files
committed
Sync open source content 🐝 (from 7dc36ffbb1f4910128197ad168e8bdff812720e8)
1 parent 9d5e2f3 commit a1e5226

File tree

1 file changed

+110
-3
lines changed
  • docs/customize/code/code-regions

1 file changed

+110
-3
lines changed

docs/customize/code/code-regions/java.mdx

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,21 @@ Java SDK classes can have two code regions:
3333
- `// #region imports` - allows the addition of imports to an SDK file needed for
3434
custom methods and properties. This region must be located at the top of the
3535
file alongside generated imports.
36-
- `// #region sdk-class-body` - allows the addition of custom methods and
36+
- `// #region class-body` - allows the addition of custom methods and
3737
properties to an SDK class. This region must be located in the body of a Java
3838
SDK class alongside generated methods and properties.
3939

40+
### Model classes
41+
42+
Java model classes can also have custom code regions:
43+
44+
- `// #region imports` - allows the addition of imports to a model file needed for
45+
custom methods and properties. This region must be located at the top of the
46+
file alongside generated imports.
47+
- `// #region class-body` - allows the addition of custom methods and
48+
properties to a model class. This region must be located in the body of a Java
49+
model class alongside generated methods and properties.
50+
4051
## Managing dependencies
4152

4253
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:
@@ -82,7 +93,7 @@ public class Todos implements
8293
}
8394
8495
// !focus(1:11)
85-
// #region sdk-class-body
96+
// #region class-body
8697
public String renderTodo(String id) throws Exception {
8798
Todo todo = getOne(id).todo()
8899
.orElseThrow(() -> new Exception("Todo not found"));
@@ -94,11 +105,107 @@ public class Todos implements
94105
95106
return renderer.render(parser.parse(markdown));
96107
}
97-
// #endregion sdk-class-body
108+
// #endregion class-body
98109
99110
public Todo getOne(String id) throws Exception {
100111
// Generated method implementation
101112
...
102113
}
103114
}
104115
```
116+
117+
## Model class example
118+
119+
You can also add custom methods to model classes. Here's an example of adding a `render()` method to a `Todo` model class:
120+
121+
```java src/main/java/com/example/sdk/models/components/Todo.java
122+
/*
123+
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
124+
*/
125+
126+
package com.example.sdk.models.components;
127+
128+
import java.util.Objects;
129+
130+
// !focus(1:3)
131+
// #region imports
132+
import org.commonmark.parser.Parser;
133+
import org.commonmark.renderer.html.HtmlRenderer;
134+
// #endregion imports
135+
136+
import com.example.sdk.utils.Utils;
137+
import com.fasterxml.jackson.annotation.JsonCreator;
138+
import com.fasterxml.jackson.annotation.JsonIgnore;
139+
import com.fasterxml.jackson.annotation.JsonProperty;
140+
141+
public class Todo {
142+
143+
@JsonProperty("id")
144+
private String id;
145+
146+
@JsonProperty("title")
147+
private String title;
148+
149+
@JsonProperty("description")
150+
private String description;
151+
152+
@JsonProperty("completed")
153+
private boolean completed;
154+
155+
@JsonCreator
156+
public Todo(
157+
@JsonProperty("id") String id,
158+
@JsonProperty("title") String title,
159+
@JsonProperty("description") String description,
160+
@JsonProperty("completed") boolean completed) {
161+
Utils.checkNotNull(id, "id");
162+
Utils.checkNotNull(title, "title");
163+
Utils.checkNotNull(description, "description");
164+
Utils.checkNotNull(completed, "completed");
165+
this.id = id;
166+
this.title = title;
167+
this.description = description;
168+
this.completed = completed;
169+
}
170+
171+
@JsonIgnore
172+
public String id() {
173+
return id;
174+
}
175+
176+
@JsonIgnore
177+
public String title() {
178+
return title;
179+
}
180+
181+
@JsonIgnore
182+
public String description() {
183+
return description;
184+
}
185+
186+
@JsonIgnore
187+
public boolean completed() {
188+
return completed;
189+
}
190+
191+
// !focus(1:8)
192+
// #region class-body
193+
public String render() throws Exception {
194+
Parser parser = Parser.builder().build();
195+
HtmlRenderer renderer = HtmlRenderer.builder().build();
196+
String markdown = String.format("# %s\n\n%s", title(), description());
197+
198+
return renderer.render(parser.parse(markdown));
199+
}
200+
// #endregion class-body
201+
202+
// Generated methods continue...
203+
}
204+
```
205+
206+
This allows you to use the custom method like so:
207+
208+
```java
209+
Todo todo = ...;
210+
String rendered = todo.render();
211+
```

0 commit comments

Comments
 (0)