Skip to content

Commit cf75640

Browse files
markpollacktzolov
authored andcommitted
Make FunctionCallingOptions collections immutable
Prevent external modification of internal state by returning unmodifiable collections and adding defensive copies in setters and builders. Co-authored-by: youngmoneee@users.noreply.github.com
1 parent c544d0c commit cf75640

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallingOptionsBuilder.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.ai.model.function;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.List;
@@ -111,14 +112,18 @@ public FunctionCallingOptionsBuilder withProxyToolCalls(Boolean proxyToolCalls)
111112

112113
public FunctionCallingOptionsBuilder withToolContext(Map<String, Object> context) {
113114
Assert.notNull(context, "Tool context must not be null");
114-
this.options.getToolContext().putAll(context);
115+
Map<String, Object> newContext = new HashMap<>(this.options.getToolContext());
116+
newContext.putAll(context);
117+
this.options.setToolContext(newContext);
115118
return this;
116119
}
117120

118121
public FunctionCallingOptionsBuilder withToolContext(String key, Object value) {
119122
Assert.notNull(key, "Key must not be null");
120123
Assert.notNull(value, "Value must not be null");
121-
this.options.getToolContext().put(key, value);
124+
Map<String, Object> newContext = new HashMap<>(this.options.getToolContext());
125+
newContext.put(key, value);
126+
this.options.setToolContext(newContext);
122127
return this;
123128
}
124129

@@ -158,22 +163,22 @@ public static FunctionCallingOptionsBuilder builder() {
158163

159164
@Override
160165
public List<FunctionCallback> getFunctionCallbacks() {
161-
return this.functionCallbacks;
166+
return Collections.unmodifiableList(this.functionCallbacks);
162167
}
163168

164169
public void setFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
165170
Assert.notNull(functionCallbacks, "FunctionCallbacks must not be null");
166-
this.functionCallbacks = functionCallbacks;
171+
this.functionCallbacks = new ArrayList<>(functionCallbacks);
167172
}
168173

169174
@Override
170175
public Set<String> getFunctions() {
171-
return this.functions;
176+
return Collections.unmodifiableSet(this.functions);
172177
}
173178

174179
public void setFunctions(Set<String> functions) {
175180
Assert.notNull(functions, "Functions must not be null");
176-
this.functions = functions;
181+
this.functions = new HashSet<>(functions);
177182
}
178183

179184
@Override
@@ -258,11 +263,12 @@ public void setProxyToolCalls(Boolean proxyToolCalls) {
258263
}
259264

260265
public Map<String, Object> getToolContext() {
261-
return context;
266+
return Collections.unmodifiableMap(this.context);
262267
}
263268

264269
public void setToolContext(Map<String, Object> context) {
265-
this.context = context;
270+
Assert.notNull(context, "Context must not be null");
271+
this.context = new HashMap<>(context);
266272
}
267273

268274
@Override
@@ -271,14 +277,14 @@ public ChatOptions copy() {
271277
.withFrequencyPenalty(this.frequencyPenalty)
272278
.withMaxTokens(this.maxTokens)
273279
.withPresencePenalty(this.presencePenalty)
274-
.withStopSequences(this.stopSequences)
280+
.withStopSequences(this.stopSequences != null ? new ArrayList<>(this.stopSequences) : null)
275281
.withTemperature(this.temperature)
276282
.withTopK(this.topK)
277283
.withTopP(this.topP)
278-
.withFunctions(this.functions)
279-
.withFunctionCallbacks(this.functionCallbacks)
284+
.withFunctions(new HashSet<>(this.functions))
285+
.withFunctionCallbacks(new ArrayList<>(this.functionCallbacks))
280286
.withProxyToolCalls(this.proxyToolCalls)
281-
.withToolContext(this.getToolContext())
287+
.withToolContext(new HashMap<>(this.getToolContext()))
282288
.build();
283289
}
284290

0 commit comments

Comments
 (0)