24
24
import org .springframework .ai .template .ValidationMode ;
25
25
import org .springframework .util .Assert ;
26
26
import org .stringtemplate .v4 .ST ;
27
+ import org .stringtemplate .v4 .compiler .Compiler ;
27
28
import org .stringtemplate .v4 .compiler .STLexer ;
28
29
29
30
import java .util .HashSet ;
@@ -48,17 +49,22 @@ public class StTemplateRenderer implements TemplateRenderer {
48
49
49
50
private static final ValidationMode DEFAULT_VALIDATION_MODE = ValidationMode .THROW ;
50
51
52
+ private static final boolean DEFAULT_SKIP_BUILT_IN_FUNCTIONS_VALIDATION = false ;
53
+
51
54
private final char startDelimiterToken ;
52
55
53
56
private final char endDelimiterToken ;
54
57
55
58
private final ValidationMode validationMode ;
56
59
57
- StTemplateRenderer (char startDelimiterToken , char endDelimiterToken , ValidationMode validationMode ) {
60
+ private final boolean skipBuiltInFunctionsValidation ;
61
+
62
+ StTemplateRenderer (char startDelimiterToken , char endDelimiterToken , ValidationMode validationMode , boolean skipBuiltInFunctionsValidation ) {
58
63
Assert .notNull (validationMode , "validationMode cannot be null" );
59
64
this .startDelimiterToken = startDelimiterToken ;
60
65
this .endDelimiterToken = endDelimiterToken ;
61
66
this .validationMode = validationMode ;
67
+ this .skipBuiltInFunctionsValidation = skipBuiltInFunctionsValidation ;
62
68
}
63
69
64
70
@ Override
@@ -113,15 +119,20 @@ private Set<String> getInputVariables(ST st) {
113
119
if (token .getType () == STLexer .LDELIM && i + 1 < tokens .size ()
114
120
&& tokens .get (i + 1 ).getType () == STLexer .ID ) {
115
121
if (i + 2 < tokens .size () && tokens .get (i + 2 ).getType () == STLexer .COLON ) {
116
- inputVariables .add (tokens .get (i + 1 ).getText ());
117
- isInsideList = true ;
122
+ String text = tokens .get (i + 1 ).getText ();
123
+ if (!Compiler .funcs .containsKey (text ) || !skipBuiltInFunctionsValidation ) {
124
+ inputVariables .add (text );
125
+ isInsideList = true ;
126
+ }
118
127
}
119
128
}
120
129
else if (token .getType () == STLexer .RDELIM ) {
121
130
isInsideList = false ;
122
131
}
123
132
else if (!isInsideList && token .getType () == STLexer .ID ) {
124
- inputVariables .add (token .getText ());
133
+ if (!Compiler .funcs .containsKey (token .getText ()) || !skipBuiltInFunctionsValidation ) {
134
+ inputVariables .add (token .getText ());
135
+ }
125
136
}
126
137
}
127
138
@@ -140,6 +151,8 @@ public static class Builder {
140
151
141
152
private ValidationMode validationMode = DEFAULT_VALIDATION_MODE ;
142
153
154
+ private boolean skipBuiltInFunctionsValidation = DEFAULT_SKIP_BUILT_IN_FUNCTIONS_VALIDATION ;
155
+
143
156
private Builder () {
144
157
}
145
158
@@ -158,10 +171,15 @@ public Builder validationMode(ValidationMode validationMode) {
158
171
return this ;
159
172
}
160
173
174
+ public Builder skipBuiltInFunctionsValidation () {
175
+ this .skipBuiltInFunctionsValidation = true ;
176
+ return this ;
177
+ }
178
+
161
179
public StTemplateRenderer build () {
162
- return new StTemplateRenderer (startDelimiterToken , endDelimiterToken , validationMode );
180
+ return new StTemplateRenderer (startDelimiterToken , endDelimiterToken , validationMode , skipBuiltInFunctionsValidation );
163
181
}
164
182
165
183
}
166
184
167
- }
185
+ }
0 commit comments