2020import dev .restate .serde .jackson .JacksonSerdeFactory ;
2121import java .io .IOException ;
2222import java .util .stream .StreamSupport ;
23+ import org .apache .logging .log4j .LogManager ;
24+ import org .apache .logging .log4j .Logger ;
2325import org .jspecify .annotations .NonNull ;
26+ import org .jspecify .annotations .Nullable ;
2427
2528/**
2629 * @deprecated This will be removed in the next release, please check the individual methods for
@@ -31,52 +34,68 @@ public final class JacksonSerdes {
3134
3235 private JacksonSerdes () {}
3336
37+ private static final Logger LOG = LogManager .getLogger (JacksonSerdes .class );
38+
3439 private static final ObjectMapper defaultMapper ;
35- private static final SchemaGenerator schemaGenerator ;
40+
41+ private static final @ Nullable SchemaGenerator schemaGenerator ;
3642
3743 static {
3844 defaultMapper = new ObjectMapper ();
3945 // Find modules through SPI (e.g. jackson-datatype-jsr310)
4046 defaultMapper .findAndRegisterModules ();
4147
42- JacksonModule module =
43- new JacksonModule (
44- JacksonOption .RESPECT_JSONPROPERTY_REQUIRED , JacksonOption .INLINE_TRANSFORMED_SUBTYPES );
45- SchemaGeneratorConfigBuilder configBuilder =
46- new SchemaGeneratorConfigBuilder (
47- defaultMapper , SchemaVersion .DRAFT_2020_12 , OptionPreset .PLAIN_JSON )
48- .with (module );
49-
50- // Make sure we use `title` for types
51- configBuilder
52- .forTypesInGeneral ()
53- .withTypeAttributeOverride (
54- (schema , scope , context ) -> {
55- if (schema .isObject ()
56- && !schema .hasNonNull (
57- SchemaKeyword .TAG_TITLE .forVersion (
58- context .getGeneratorConfig ().getSchemaVersion ()))) {
59- JsonNode typeKeyword =
60- schema .get (
61- SchemaKeyword .TAG_TYPE .forVersion (
62- context .getGeneratorConfig ().getSchemaVersion ()));
63- boolean isObjectSchema =
64- typeKeyword != null
65- && ((typeKeyword .isTextual () && "object" .equals (typeKeyword .textValue ()))
66- || (typeKeyword .isArray ()
67- && StreamSupport .stream (typeKeyword .spliterator (), false )
68- .anyMatch (
69- el -> el .isTextual () && "object" .equals (el .textValue ()))));
70- if (isObjectSchema ) {
71- schema .put (
72- SchemaKeyword .TAG_TITLE .forVersion (
73- context .getGeneratorConfig ().getSchemaVersion ()),
74- scope .getSimpleTypeDescription ());
75- }
76- }
77- });
48+ schemaGenerator = buildSchemaGenerator (defaultMapper );
49+ }
7850
79- schemaGenerator = new SchemaGenerator (configBuilder .build ());
51+ private static @ Nullable SchemaGenerator buildSchemaGenerator (ObjectMapper mapper ) {
52+ try {
53+ JacksonModule module =
54+ new JacksonModule (
55+ JacksonOption .RESPECT_JSONPROPERTY_REQUIRED ,
56+ JacksonOption .INLINE_TRANSFORMED_SUBTYPES );
57+ SchemaGeneratorConfigBuilder configBuilder =
58+ new SchemaGeneratorConfigBuilder (
59+ mapper , SchemaVersion .DRAFT_2020_12 , OptionPreset .PLAIN_JSON )
60+ .with (module );
61+
62+ // Make sure we use `title` for types
63+ configBuilder
64+ .forTypesInGeneral ()
65+ .withTypeAttributeOverride (
66+ (schema , scope , context ) -> {
67+ if (schema .isObject ()
68+ && !schema .hasNonNull (
69+ SchemaKeyword .TAG_TITLE .forVersion (
70+ context .getGeneratorConfig ().getSchemaVersion ()))) {
71+ JsonNode typeKeyword =
72+ schema .get (
73+ SchemaKeyword .TAG_TYPE .forVersion (
74+ context .getGeneratorConfig ().getSchemaVersion ()));
75+ boolean isObjectSchema =
76+ typeKeyword != null
77+ && ((typeKeyword .isTextual () && "object" .equals (typeKeyword .textValue ()))
78+ || (typeKeyword .isArray ()
79+ && StreamSupport .stream (typeKeyword .spliterator (), false )
80+ .anyMatch (
81+ el ->
82+ el .isTextual () && "object" .equals (el .textValue ()))));
83+ if (isObjectSchema ) {
84+ schema .put (
85+ SchemaKeyword .TAG_TITLE .forVersion (
86+ context .getGeneratorConfig ().getSchemaVersion ()),
87+ scope .getSimpleTypeDescription ());
88+ }
89+ }
90+ });
91+
92+ return new SchemaGenerator (configBuilder .build ());
93+ } catch (LinkageError t ) {
94+ LOG .warn (
95+ "Cannot initialize the Jackson JSON Schema generator due to an incompatible version of com.github.victools:jsonschema-generator." ,
96+ t );
97+ return null ;
98+ }
8099 }
81100
82101 /**
@@ -99,7 +118,12 @@ public static <T> Serde<T> of(Class<T> clazz) {
99118 public static <T > Serde <T > of (ObjectMapper mapper , Class <T > clazz ) {
100119 return new Serde <>() {
101120 @ Override
102- public Schema jsonSchema () {
121+ public @ Nullable Schema jsonSchema () {
122+ // schemaGenerator is null when an incompatible victools version is on the classpath,
123+ // see #buildSchemaGenerator.
124+ if (schemaGenerator == null ) {
125+ return null ;
126+ }
103127 return new JsonSchema (schemaGenerator .generateSchema (clazz ));
104128 }
105129
0 commit comments