21
21
*/
22
22
23
23
import com .fasterxml .jackson .databind .JsonNode ;
24
- import com .fasterxml .jackson .databind .ObjectMapper ;
25
24
import com .fasterxml .jackson .databind .node .ObjectNode ;
26
25
26
+ import org .springframework .ai .util .json .JsonParser ;
27
27
import org .springframework .util .Assert ;
28
28
29
29
/**
30
30
* Utility class for converting JSON Schema to OpenAPI schema format.
31
31
*/
32
32
public final class JsonSchemaConverter {
33
33
34
- private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
35
-
36
34
private JsonSchemaConverter () {
37
35
// Prevent instantiation
38
36
}
39
37
40
38
public static ObjectNode fromJson (String jsonString ) {
41
39
try {
42
- return (ObjectNode ) OBJECT_MAPPER .readTree (jsonString );
40
+ return (ObjectNode ) JsonParser . getObjectMapper () .readTree (jsonString );
43
41
}
44
42
catch (Exception e ) {
45
43
throw new RuntimeException ("Failed to parse JSON: " + jsonString , e );
@@ -57,7 +55,7 @@ public static ObjectNode convertToOpenApiSchema(ObjectNode jsonSchemaNode) {
57
55
58
56
try {
59
57
// Convert to OpenAPI schema using our custom conversion logic
60
- ObjectNode openApiSchema = convertSchema (jsonSchemaNode , OBJECT_MAPPER .getNodeFactory ());
58
+ ObjectNode openApiSchema = convertSchema (jsonSchemaNode , JsonParser . getObjectMapper () .getNodeFactory ());
61
59
62
60
// Add OpenAPI-specific metadata
63
61
if (!openApiSchema .has ("openapi" )) {
@@ -105,8 +103,8 @@ private static void handleJsonSchemaSpecifics(ObjectNode source, ObjectNode targ
105
103
ObjectNode properties = target .putObject ("properties" );
106
104
source .get ("properties" ).fields ().forEachRemaining (entry -> {
107
105
if (entry .getValue () instanceof ObjectNode ) {
108
- properties .set (entry .getKey (),
109
- convertSchema (( ObjectNode ) entry . getValue (), OBJECT_MAPPER .getNodeFactory ()));
106
+ properties .set (entry .getKey (), convertSchema (( ObjectNode ) entry . getValue (),
107
+ JsonParser . getObjectMapper () .getNodeFactory ()));
110
108
}
111
109
});
112
110
}
@@ -124,15 +122,15 @@ private static void handleJsonSchemaSpecifics(ObjectNode source, ObjectNode targ
124
122
}
125
123
else if (additionalProps .isObject ()) {
126
124
target .set ("additionalProperties" ,
127
- convertSchema ((ObjectNode ) additionalProps , OBJECT_MAPPER .getNodeFactory ()));
125
+ convertSchema ((ObjectNode ) additionalProps , JsonParser . getObjectMapper () .getNodeFactory ()));
128
126
}
129
127
}
130
128
131
129
// Handle arrays
132
130
if (source .has ("items" )) {
133
131
JsonNode items = source .get ("items" );
134
132
if (items .isObject ()) {
135
- target .set ("items" , convertSchema ((ObjectNode ) items , OBJECT_MAPPER .getNodeFactory ()));
133
+ target .set ("items" , convertSchema ((ObjectNode ) items , JsonParser . getObjectMapper () .getNodeFactory ()));
136
134
}
137
135
}
138
136
0 commit comments