Skip to content

Commit bc8dabf

Browse files
authored
[Fix-17389][HttpTask] Fix json parsing exception in request body (#18085)
1 parent 9220a97 commit bc8dabf

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

  • dolphinscheduler-task-plugin/dolphinscheduler-task-http/src

dolphinscheduler-task-plugin/dolphinscheduler-task-http/src/main/java/org/apache/dolphinscheduler/plugin/task/http/HttpTask.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
import lombok.SneakyThrows;
4242
import lombok.extern.slf4j.Slf4j;
4343

44+
import com.fasterxml.jackson.core.type.TypeReference;
45+
import com.fasterxml.jackson.databind.JsonNode;
46+
4447
@Slf4j
4548
public class HttpTask extends AbstractTask {
4649

@@ -251,13 +254,18 @@ private Map<String, Object> getRequestParams() {
251254
private Map<String, Object> getRequestBody() {
252255
String convertedParams = ParameterUtils.convertParameterPlaceholders(httpParameters.getHttpRequestBody(),
253256
ParameterUtils.convert(taskExecutionContext.getPrepareParamsMap()));
254-
Map<String, String> requestBody = JSONUtils.toMap(convertedParams);
255-
if (requestBody == null) {
257+
JsonNode requestBodyJsonNode = JSONUtils.parseObject(convertedParams, JsonNode.class);
258+
if (requestBodyJsonNode == null) {
256259
return null;
257260
}
258261

259-
return requestBody.entrySet().stream()
260-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
262+
if (!requestBodyJsonNode.isObject()) {
263+
throw new IllegalArgumentException(String.format("Http request body should be a json object, but got: %s",
264+
convertedParams));
265+
}
266+
267+
return JSONUtils.parseObject(requestBodyJsonNode.toString(), new TypeReference<Map<String, Object>>() {
268+
});
261269
}
262270

263271
@Override

dolphinscheduler-task-plugin/dolphinscheduler-task-http/src/test/java/org/apache/dolphinscheduler/plugin/task/http/HttpTaskTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.concurrent.TimeUnit;
3738

3839
import okhttp3.mockwebserver.Dispatcher;
3940
import okhttp3.mockwebserver.MockResponse;
@@ -145,6 +146,25 @@ public void testHandleWithHttpBody() throws Exception {
145146
Assertions.assertEquals(EXIT_CODE_SUCCESS, httpTask.getExitStatusCode());
146147
}
147148

149+
@Test
150+
public void testHandleWithComplexHttpBody() throws Exception {
151+
String httpBody = "{\"name\":\"tom\",\"scores\":[100,98],\"metadata\":{\"grade\":\"A\"}}";
152+
String httpResponse = "{\"status\": \"success\"}";
153+
String url = withMockWebServer(DEFAULT_MOCK_PATH, HttpStatus.SC_OK, httpResponse);
154+
HttpTask httpTask = generateHttpTask(url, HttpRequestMethod.POST, httpBody,
155+
new ArrayList<>(), null, HttpCheckCondition.STATUS_CODE_DEFAULT, "");
156+
157+
httpTask.handle(null);
158+
159+
MockWebServer server = getLatestMockWebServer();
160+
RecordedRequest recordedRequest = server.takeRequest(1, TimeUnit.SECONDS);
161+
Assertions.assertNotNull(recordedRequest);
162+
String actualRequestBody = recordedRequest.getBody().readUtf8();
163+
Assertions.assertTrue(actualRequestBody.contains("\"scores\":[100,98]"));
164+
Assertions.assertTrue(actualRequestBody.contains("\"metadata\":{\"grade\":\"A\"}"));
165+
Assertions.assertEquals(EXIT_CODE_SUCCESS, httpTask.getExitStatusCode());
166+
}
167+
148168
@Test
149169
public void testHandleWithHttpParameterParams() throws Exception {
150170
List<HttpProperty> httpParams = new ArrayList<>();
@@ -241,11 +261,23 @@ private HttpTask generateHttpTask(String mockPath, HttpRequestMethod httpRequest
241261
String condition, int actualResponseCode,
242262
String actualResponseBody) throws IOException {
243263
String url = withMockWebServer(mockPath, actualResponseCode, actualResponseBody);
264+
return generateHttpTask(url, httpRequestMethod, httpBody, httpParams, prepareParamsMap,
265+
httpCheckConditionType, condition);
266+
}
267+
268+
private HttpTask generateHttpTask(String url, HttpRequestMethod httpRequestMethod, String httpBody,
269+
List<HttpProperty> httpParams,
270+
Map<String, String> prepareParamsMap, HttpCheckCondition httpCheckConditionType,
271+
String condition) throws JsonProcessingException {
244272
String paramData =
245273
generateHttpParameters(url, httpRequestMethod, httpBody, httpParams, httpCheckConditionType, condition);
246274
return generateHttpTaskFromParamData(paramData, prepareParamsMap);
247275
}
248276

277+
private MockWebServer getLatestMockWebServer() {
278+
return mockWebServers.get(mockWebServers.size() - 1);
279+
}
280+
249281
private HttpTask generateHttpTaskFromParamData(String paramData, Map<String, String> prepareParamsMap) {
250282
TaskExecutionContext taskExecutionContext = Mockito.mock(TaskExecutionContext.class);
251283
Mockito.when(taskExecutionContext.getTaskParams()).thenReturn(paramData);

0 commit comments

Comments
 (0)