Skip to content

Commit 005121b

Browse files
Merge pull request #487 from vorburger:issue-473_ObjectMapper-inject
PiperOrigin-RevId: 877891308
2 parents c0f9f95 + 71b1070 commit 005121b

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

core/src/main/java/com/google/adk/tools/FunctionCallingUtils.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
public final class FunctionCallingUtils {
4646

4747
private static final Logger logger = LoggerFactory.getLogger(FunctionCallingUtils.class);
48-
private static final ObjectMapper objectMapper = JsonBaseModel.getMapper();
48+
private static final ObjectMapper defaultObjectMapper = JsonBaseModel.getMapper();
4949

5050
/** Holds the state during a single schema generation process to handle caching and recursion. */
5151
private static class SchemaGenerationContext {
@@ -162,7 +162,20 @@ private static Schema buildSchemaFromParameter(Parameter param) {
162162
* @throws IllegalArgumentException if a type is encountered that cannot be serialized by Jackson.
163163
*/
164164
public static Schema buildSchemaFromType(Type type) {
165-
return buildSchemaRecursive(objectMapper.constructType(type), new SchemaGenerationContext());
165+
return buildSchemaFromType(type, defaultObjectMapper);
166+
}
167+
168+
/**
169+
* Builds a Schema from a Java Type, creating a new context for the generation process.
170+
*
171+
* @param type The Java {@link Type} to convert into a Schema.
172+
* @param objectMapper The {@link ObjectMapper} to use for introspecting types.
173+
* @return The generated {@link Schema}.
174+
* @throws IllegalArgumentException if a type is encountered that cannot be serialized by Jackson.
175+
*/
176+
public static Schema buildSchemaFromType(Type type, ObjectMapper objectMapper) {
177+
return buildSchemaRecursive(
178+
objectMapper.constructType(type), new SchemaGenerationContext(), objectMapper);
166179
}
167180

168181
/**
@@ -173,7 +186,8 @@ public static Schema buildSchemaFromType(Type type) {
173186
* @return The generated {@link Schema}.
174187
* @throws IllegalArgumentException if a type is encountered that cannot be serialized by Jackson.
175188
*/
176-
private static Schema buildSchemaRecursive(JavaType javaType, SchemaGenerationContext context) {
189+
private static Schema buildSchemaRecursive(
190+
JavaType javaType, SchemaGenerationContext context, ObjectMapper objectMapper) {
177191
if (context.isProcessing(javaType)) {
178192
logger.warn("Type {} is recursive. Omitting from schema.", javaType.toCanonical());
179193
return Schema.builder()
@@ -194,7 +208,9 @@ private static Schema buildSchemaRecursive(JavaType javaType, SchemaGenerationCo
194208
Class<?> rawClass = javaType.getRawClass();
195209

196210
if (javaType.isCollectionLikeType() && List.class.isAssignableFrom(rawClass)) {
197-
builder.type("ARRAY").items(buildSchemaRecursive(javaType.getContentType(), context));
211+
builder
212+
.type("ARRAY")
213+
.items(buildSchemaRecursive(javaType.getContentType(), context, objectMapper));
198214
} else if (javaType.isMapLikeType()) {
199215
builder.type("OBJECT");
200216
} else if (String.class.equals(rawClass)) {
@@ -232,7 +248,8 @@ private static Schema buildSchemaRecursive(JavaType javaType, SchemaGenerationCo
232248
for (BeanPropertyDefinition property : beanDescription.findProperties()) {
233249
AnnotatedMember member = property.getPrimaryMember();
234250
if (member != null) {
235-
properties.put(property.getName(), buildSchemaRecursive(member.getType(), context));
251+
properties.put(
252+
property.getName(), buildSchemaRecursive(member.getType(), context, objectMapper));
236253
if (property.isRequired()) {
237254
required.add(property.getName());
238255
}

core/src/main/java/com/google/adk/tools/FunctionTool.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
public class FunctionTool extends BaseTool {
4545

4646
private static final Logger logger = LoggerFactory.getLogger(FunctionTool.class);
47-
private static final ObjectMapper objectMapper = JsonBaseModel.getMapper();
4847

4948
private final @Nullable Object instance;
5049
private final Method func;
5150
private final FunctionDeclaration funcDeclaration;
5251
private final boolean requireConfirmation;
52+
private final ObjectMapper objectMapper;
5353

5454
public static FunctionTool create(Object instance, Method func) {
5555
return create(instance, func, /* requireConfirmation= */ false);
@@ -166,11 +166,26 @@ private static boolean wasCompiledWithDefaultParameterNames(Method func) {
166166
}
167167

168168
protected FunctionTool(@Nullable Object instance, Method func, boolean isLongRunning) {
169-
this(instance, func, isLongRunning, /* requireConfirmation= */ false);
169+
this(
170+
instance, func, isLongRunning, /* requireConfirmation= */ false, JsonBaseModel.getMapper());
170171
}
171172

172173
protected FunctionTool(
173174
@Nullable Object instance, Method func, boolean isLongRunning, boolean requireConfirmation) {
175+
this(instance, func, isLongRunning, requireConfirmation, JsonBaseModel.getMapper());
176+
}
177+
178+
protected FunctionTool(
179+
@Nullable Object instance, Method func, boolean isLongRunning, ObjectMapper objectMapper) {
180+
this(instance, func, isLongRunning, /* requireConfirmation= */ false, objectMapper);
181+
}
182+
183+
protected FunctionTool(
184+
@Nullable Object instance,
185+
Method func,
186+
boolean isLongRunning,
187+
boolean requireConfirmation,
188+
ObjectMapper objectMapper) {
174189
super(
175190
func.isAnnotationPresent(Annotations.Schema.class)
176191
&& !func.getAnnotation(Annotations.Schema.class).name().isEmpty()
@@ -193,6 +208,7 @@ protected FunctionTool(
193208
FunctionCallingUtils.buildFunctionDeclaration(
194209
this.func, ImmutableList.of("toolContext", "inputStream"));
195210
this.requireConfirmation = requireConfirmation;
211+
this.objectMapper = objectMapper;
196212
}
197213

198214
@Override
@@ -365,7 +381,7 @@ private static Class<?> getTypeClass(Type type, String paramName) {
365381
}
366382
}
367383

368-
private static List<Object> createList(List<Object> values, Class<?> type) {
384+
private List<Object> createList(List<Object> values, Class<?> type) {
369385
List<Object> list = new ArrayList<>();
370386
// List of parameterized type is not supported.
371387
if (type == null) {
@@ -387,7 +403,7 @@ private static List<Object> createList(List<Object> values, Class<?> type) {
387403
return list;
388404
}
389405

390-
private static Object castValue(Object value, Class<?> type) {
406+
private Object castValue(Object value, Class<?> type) {
391407
if (type.equals(Integer.class) || type.equals(int.class)) {
392408
if (value instanceof Integer) {
393409
return value;

0 commit comments

Comments
 (0)