[Feature][connector-http] Parameters support placeholder replacement#9184
Conversation
…_http_param # Conflicts: # docs/en/connector-v2/source/Http.md # seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/PageInfo.java # seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSource.java # seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java # seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/java/org/apache/seatunnel/e2e/connector/http/HttpIT.java # seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/mockserver-config.json
There was a problem hiding this comment.
Copilot reviewed 6 out of 13 changed files in this pull request and generated no comments.
Files not reviewed (7)
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_get_placeholder_replacement_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_get_placeholder_replacement_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_header_placeholder_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_header_placeholder_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_placeholder_replacement_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_placeholder_replacement_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/mockserver-config.json: Language not supported
Comments suppressed due to low confidence (2)
seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/java/org/apache/seatunnel/e2e/connector/http/HttpIT.java:366
- The variable 'execResult21' is declared twice in the same scope; consider renaming the second instance to avoid redeclaration.
Container.ExecResult execResult21 = container.executeJob("/http_placeholder_replacement_true.conf");
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java:187
- The processPageMap method is incorrectly applied on 'this.httpParameter.getHeaders()' instead of 'this.httpParameter.getParams()'; update the argument to use the correct map.
processPageMap( this.httpParameter.getHeaders(), pageField, pageValue, usePlaceholderReplacement);
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for placeholder replacement in the HTTP connector’s parameters, headers, and body while updating configuration and documentation accordingly.
- Introduces new tests to validate placeholder replacement functionality in various request components.
- Updates the HTTP source reader to incorporate placeholder replacement logic and new configuration options.
- Enhances documentation with examples on using placeholder replacement.
Reviewed Changes
Copilot reviewed 6 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/java/org/apache/seatunnel/e2e/connector/http/HttpIT.java | Added E2E tests for placeholder replacement on headers, parameters, and GET requests. |
| seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java | Modified updateRequestParam to support placeholder replacement; introduced processPageMap and processBodyPageMap. |
| seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSource.java | Updated paging configuration to include use of placeholder replacement. |
| seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/PageInfo.java | Added a boolean flag for placeholder replacement. |
| seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/HttpSourceOptions.java | Added a new configuration option for placeholder replacement. |
| docs/en/connector-v2/source/Http.md | Updated documentation with details and examples for placeholder replacement. |
Files not reviewed (7)
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_get_placeholder_replacement_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_get_placeholder_replacement_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_header_placeholder_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_header_placeholder_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_placeholder_replacement_false.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_placeholder_replacement_true.conf: Language not supported
- seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/mockserver-config.json: Language not supported
Comments suppressed due to low confidence (2)
seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/java/org/apache/seatunnel/e2e/connector/http/HttpIT.java:366
- The variable 'execResult21' is redeclared, which may cause a compilation error due to duplicate variable declaration. Consider renaming one of the variables.
Container.ExecResult execResult21 = container.executeJob("/http_placeholder_replacement_true.conf");
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java:187
- This line uses 'this.httpParameter.getHeaders()' while processing parameters. It seems intended to process the parameters map; replace it with 'this.httpParameter.getParams()'.
processPageMap(this.httpParameter.getHeaders(), pageField, pageValue, usePlaceholderReplacement);
|
@Hisoka-X PTAL |
| pageValue, | ||
| usePlaceholderReplacement); | ||
|
|
||
| // set page cursor as params |
There was a problem hiding this comment.
we should take care cursor base params too.
| if (value.equals("${" + pageField + "}")) { | ||
| // If the value is exactly the placeholder, use pageValue directly | ||
| updatedMap.put(key, pageValue.toString()); | ||
| } | ||
| } |
There was a problem hiding this comment.
we should take care value contains ${page} situation. E.g. page: 10${page}
| if (value instanceof String) { | ||
| String strValue = (String) value; | ||
| // Check if the value is exactly the placeholder for pageField | ||
| if (strValue.equals("${" + pageField + "}")) { | ||
| // If the value is exactly the placeholder, use pageValue directly | ||
| updatedBody.put(key, pageValue); | ||
| } | ||
| } |
There was a problem hiding this comment.
Even the value is another map we should replace the value too.
| @TestTemplate | ||
| public void testPlaceholderReplacement(TestContainer container) |
There was a problem hiding this comment.
Let's move all test case to unit test. You can refer https://github.com/apache/seatunnel/pull/9103/files#diff-4926ea699715eac0714ccf51f2093fa36b3d15f0a72239490b4ba77de5674bceR54
| */ | ||
| private String replacePlaceholder(String value, String pageField, Object pageValue) { | ||
| if (value == null || pageField == null || !value.contains("${" + pageField + "}")) { | ||
| return null; |
There was a problem hiding this comment.
| return null; | |
| return value; |
I think return value without change is better than null.
| String suffix = value.substring(placeholderIndex + placeholder.length()); | ||
| return prefix + pageValue + suffix; | ||
| } | ||
| return null; |
| Method updateRequestParamMethod = | ||
| HttpSourceReader.class.getDeclaredMethod( | ||
| "updateRequestParam", PageInfo.class, boolean.class); | ||
| updateRequestParamMethod.setAccessible(true); | ||
| updateRequestParamMethod.invoke(httpSourceReader, pageInfo, true); |
There was a problem hiding this comment.
Update updateRequestParam scope to default and add @VisibleForTesting annotation to avoid reflection.
| boolean usePlaceholderReplacement) { | ||
| if (usePlaceholderReplacement) { | ||
| String jsonBody = JsonUtils.toJsonString(bodyMap); | ||
| String placeholder = "\"${" + pageField + "}\""; |
There was a problem hiding this comment.
why we should special care "${key}" with quotes?
| entry -> entry.getValue().unwrapped(), | ||
| (v1, v2) -> v2)); | ||
| } catch (Exception e) { | ||
| log.warn("Failed to parse body string to map: {}", body, e); |
There was a problem hiding this comment.
we shoud throw exception directly.
| */ | ||
| private String replacePlaceholder(String value, String pageField, Object pageValue) { | ||
| if (value == null || pageField == null || !value.contains("${" + pageField + "}")) { | ||
| return null; |
| log.warn( | ||
| "Failed to parse body string for key-based replacement: {}", bodyString, e); |
| boolean usePlaceholderReplacement) { | ||
| if (usePlaceholderReplacement) { | ||
| String jsonBody = JsonUtils.toJsonString(bodyMap); | ||
| String placeholder = "\"${" + pageField + "}\""; |
| - As a number without quotes: `${page}` (in JSON body) | ||
| - In nested JSON structures: `{"pagination":{"page":${page}}}` | ||
|
|
||
| #### Example 1: Using page parameters in body and params |
There was a problem hiding this comment.
| #### Example 1: Using page parameters in body and params | |
| ##### Example 1: Using page parameters in body and params |
| } | ||
| ``` | ||
|
|
||
| #### Example 2: Using page parameters in headers |
| } | ||
| ``` | ||
|
|
||
| #### Example 3: Using key-based replacement (without placeholders) |
There was a problem hiding this comment.
| #### Example 3: Using key-based replacement (without placeholders) | |
| ##### Example 3: Using key-based replacement (without placeholders) |
| } | ||
| ``` | ||
|
|
||
| #### Example 4: Using prefixed page number in headers |
There was a problem hiding this comment.
| #### Example 4: Using prefixed page number in headers | |
| ##### Example 4: Using prefixed page number in headers |
| } | ||
| ``` | ||
|
|
||
| #### Example 5: Using unquoted page number in body |
There was a problem hiding this comment.
| #### Example 5: Using unquoted page number in body | |
| ##### Example 5: Using unquoted page number in body |
| } | ||
| ``` | ||
|
|
||
| #### Example 6: Using nested JSON structure with page parameter |
There was a problem hiding this comment.
| #### Example 6: Using nested JSON structure with page parameter | |
| ##### Example 6: Using nested JSON structure with page parameter |
Parameters support placeholder replacement.
close #9183
Purpose of this pull request
Does this PR introduce any user-facing change?
How was this patch tested?
Check list
New License Guide
release-note.