|
10 | 10 |
|
11 | 11 | import com.fasterxml.jackson.core.JsonProcessingException; |
12 | 12 | import com.fasterxml.jackson.core.type.TypeReference; |
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
13 | 14 | import com.fasterxml.jackson.databind.ObjectMapper; |
14 | 15 | import com.github.victools.jsonschema.generator.*; |
15 | 16 | import com.github.victools.jsonschema.module.jackson.JacksonModule; |
16 | 17 | import com.github.victools.jsonschema.module.jackson.JacksonOption; |
17 | 18 | import dev.restate.sdk.common.RichSerde; |
18 | 19 | import dev.restate.sdk.common.Serde; |
19 | 20 | import java.io.IOException; |
| 21 | +import java.util.stream.StreamSupport; |
20 | 22 | import org.jspecify.annotations.Nullable; |
21 | 23 |
|
22 | 24 | /** |
@@ -59,6 +61,37 @@ private JacksonSerdes() {} |
59 | 61 | new SchemaGeneratorConfigBuilder( |
60 | 62 | defaultMapper, SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON) |
61 | 63 | .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 | + |
62 | 95 | schemaGenerator = new SchemaGenerator(configBuilder.build()); |
63 | 96 | } |
64 | 97 |
|
|
0 commit comments