Skip to content

Commit 12184e9

Browse files
Add title for classes (#398)
1 parent 7954ab5 commit 12184e9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

examples/src/main/java/my/restate/sdk/examples/Counter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public long get(SharedObjectContext ctx) {
4848
return ctx.get(TOTAL).orElse(0L);
4949
}
5050

51+
/** Get the current counter value. */
52+
@Shared
53+
@Handler
54+
public Node getNode(SharedObjectContext ctx) {
55+
return null;
56+
}
57+
5158
/** Add a value, and get both the previous value and the new value. */
5259
@Handler
5360
public CounterUpdateResult getAndAdd(ObjectContext ctx, long request) {
@@ -64,6 +71,24 @@ public static void main(String[] args) {
6471
RestateHttpEndpointBuilder.builder().bind(new Counter()).buildAndListen();
6572
}
6673

74+
public static class Node {
75+
private final String data;
76+
private final Node inner;
77+
78+
public Node(String data, Node inner) {
79+
this.data = data;
80+
this.inner = inner;
81+
}
82+
83+
public String getData() {
84+
return data;
85+
}
86+
87+
public Node getInner() {
88+
return inner;
89+
}
90+
}
91+
6792
public static class CounterUpdateResult {
6893
private final long newValue;
6994
private final long oldValue;

sdk-serde-jackson/src/main/java/dev/restate/sdk/serde/jackson/JacksonSerdes.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
import com.fasterxml.jackson.core.JsonProcessingException;
1212
import com.fasterxml.jackson.core.type.TypeReference;
13+
import com.fasterxml.jackson.databind.JsonNode;
1314
import com.fasterxml.jackson.databind.ObjectMapper;
1415
import com.github.victools.jsonschema.generator.*;
1516
import com.github.victools.jsonschema.module.jackson.JacksonModule;
1617
import com.github.victools.jsonschema.module.jackson.JacksonOption;
1718
import dev.restate.sdk.common.RichSerde;
1819
import dev.restate.sdk.common.Serde;
1920
import java.io.IOException;
21+
import java.util.stream.StreamSupport;
2022
import org.jspecify.annotations.Nullable;
2123

2224
/**
@@ -59,6 +61,37 @@ private JacksonSerdes() {}
5961
new SchemaGeneratorConfigBuilder(
6062
defaultMapper, SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON)
6163
.with(module);
64+
65+
// Make sure we use `title` for types
66+
configBuilder
67+
.forTypesInGeneral()
68+
.withTypeAttributeOverride(
69+
(schema, scope, context) -> {
70+
if (schema.isObject()
71+
&& !schema.hasNonNull(
72+
SchemaKeyword.TAG_TITLE.forVersion(
73+
context.getGeneratorConfig().getSchemaVersion()))) {
74+
JsonNode typeKeyword =
75+
schema.get(
76+
SchemaKeyword.TAG_TYPE.forVersion(
77+
context.getGeneratorConfig().getSchemaVersion()));
78+
boolean isObjectSchema =
79+
typeKeyword != null
80+
&& ((typeKeyword.isTextual() && "object".equals(typeKeyword.textValue()))
81+
|| (typeKeyword.isArray()
82+
&& StreamSupport.stream(typeKeyword.spliterator(), false)
83+
.anyMatch(
84+
el -> el.isTextual() && "object".equals(el.textValue()))));
85+
if (isObjectSchema) {
86+
schema.put(
87+
SchemaKeyword.TAG_TITLE.forVersion(
88+
context.getGeneratorConfig().getSchemaVersion()),
89+
scope.getSimpleTypeDescription());
90+
}
91+
}
92+
});
93+
;
94+
6295
schemaGenerator = new SchemaGenerator(configBuilder.build());
6396
}
6497

0 commit comments

Comments
 (0)