Skip to content

Commit fd61cea

Browse files
committed
Don't convert strings to numbers in schema validation
Signed-off-by: Daniel Widdis <widdis@gmail.com>
1 parent 28d8c3a commit fd61cea

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

plugin/src/main/java/org/opensearch/ml/utils/MLNodeUtils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,14 @@ public static String processRemoteInferenceInputDataSetParametersValue(String in
109109
if (value.isTextual()) {
110110
String textValue = value.asText();
111111
try {
112-
// Try to parse the string as JSON
113-
JsonNode parsedValue = mapper.readTree(textValue);
114-
// If successful, replace the string with the parsed JSON
115-
parametersNode.set(key, parsedValue);
112+
// Only try to parse as JSON if it looks like a JSON object or array
113+
if ((textValue.startsWith("{") && textValue.endsWith("}"))
114+
|| (textValue.startsWith("[") && textValue.endsWith("]"))) {
115+
// Try to parse the string as JSON
116+
JsonNode parsedValue = mapper.readTree(textValue);
117+
// If successful, replace the string with the parsed JSON
118+
parametersNode.set(key, parsedValue);
119+
}
116120
} catch (IOException e) {
117121
// If parsing fails, it's not a valid JSON string, so keep it as is
118122
parametersNode.set(key, value);

plugin/src/test/java/org/opensearch/ml/utils/MLNodeUtilsTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ public void testProcessRemoteInferenceInputDataSetParametersValueWithParametersP
161161
assertEquals(expectedJson, processedJson);
162162
}
163163

164+
@Test
165+
public void testProcessRemoteInferenceInputDataSetParametersValueWithParametersQuotedNumber() throws IOException {
166+
String json = "{\"key1\":\"foo\",\"key2\":123,\"key3\":true,\"parameters\":{\"key1\":\"123\",\"key2\":123,\"key3\":true}}";
167+
String processedJson = MLNodeUtils.processRemoteInferenceInputDataSetParametersValue(json);
168+
assertEquals(json, processedJson);
169+
}
170+
164171
@Test
165172
public void testProcessRemoteInferenceInputDataSetParametersValueWithParametersNoProcess() throws IOException {
166173
String json = "{\"key1\":\"foo\",\"key2\":123,\"key3\":true,\"parameters\":{\"key1\":\"foo\",\"key2\":123,\"key3\":true}}";

0 commit comments

Comments
 (0)