31
31
import static com .openblocks .sdk .exception .PluginCommonError .QUERY_ARGUMENT_ERROR ;
32
32
import static com .openblocks .sdk .exception .PluginCommonError .QUERY_EXECUTION_ERROR ;
33
33
import static com .openblocks .sdk .plugin .restapi .DataUtils .convertToMultiformFileValue ;
34
- import static com .openblocks .sdk .plugin .restapi .DataUtils .parseJsonBody ;
35
34
import static com .openblocks .sdk .plugin .restapi .auth .RestApiAuthType .DIGEST_AUTH ;
36
35
import static com .openblocks .sdk .plugin .restapi .auth .RestApiAuthType .OAUTH2_INHERIT_FROM_LOGIN ;
37
36
import static com .openblocks .sdk .util .ExceptionUtils .propagateError ;
38
37
import static com .openblocks .sdk .util .JsonUtils .readTree ;
39
38
import static com .openblocks .sdk .util .JsonUtils .toJsonThrows ;
40
- import static com .openblocks .sdk .util .MustacheHelper .renderMustacheJsonString ;
39
+ import static com .openblocks .sdk .util .MustacheHelper .renderMustacheJson ;
41
40
import static com .openblocks .sdk .util .MustacheHelper .renderMustacheString ;
42
41
import static com .openblocks .sdk .util .StreamUtils .collectList ;
43
42
import static org .apache .commons .collections4 .MapUtils .emptyIfNull ;
63
62
64
63
import org .apache .commons .collections4 .CollectionUtils ;
65
64
import org .apache .commons .lang3 .StringUtils ;
65
+ import org .apache .commons .lang3 .tuple .Pair ;
66
66
import org .bson .internal .Base64 ;
67
67
import org .pf4j .Extension ;
68
68
import org .springframework .http .HttpCookie ;
85
85
import com .openblocks .plugin .restapi .constants .ResponseDataType ;
86
86
import com .openblocks .plugin .restapi .helpers .AuthHelper ;
87
87
import com .openblocks .plugin .restapi .helpers .BufferingFilter ;
88
+ import com .openblocks .plugin .restapi .model .QueryBody ;
88
89
import com .openblocks .plugin .restapi .model .RestApiQueryConfig ;
89
90
import com .openblocks .plugin .restapi .model .RestApiQueryExecutionContext ;
90
91
import com .openblocks .sdk .exception .PluginException ;
100
101
import com .openblocks .sdk .plugin .restapi .auth .BasicAuthConfig ;
101
102
import com .openblocks .sdk .plugin .restapi .auth .RestApiAuthType ;
102
103
import com .openblocks .sdk .query .QueryVisitorContext ;
103
- import com .openblocks .sdk .util .JsonUtils ;
104
104
import com .openblocks .sdk .webclient .WebClients ;
105
105
106
106
import lombok .Builder ;
@@ -159,18 +159,23 @@ public RestApiQueryExecutionContext buildQueryExecutionContext(RestApiDatasource
159
159
160
160
List <Property > updatedQueryBodyParams = renderMustacheValueForQueryBody (queryBodyParams , requestParams , contentType );
161
161
162
- String updatedQueryBody ;
163
- if (isJsonContentType (contentType )) {
164
- updatedQueryBody = renderMustacheJsonString (queryBody , requestParams );
162
+ // string | jsonNode
163
+ QueryBody updatedQueryBody ;
164
+ Pair <Boolean , Boolean > jsonContentType = isJsonContentType (contentType );
165
+ boolean isJsonContent = jsonContentType .getLeft ();
166
+ Boolean isSpecialJsonContent = jsonContentType .getRight ();
167
+ if (isJsonContent ) {
168
+ updatedQueryBody = new QueryBody (renderMustacheJson (queryBody , requestParams ), true , isSpecialJsonContent );
165
169
} else {
166
- updatedQueryBody = renderMustacheString (queryBody , requestParams );
170
+ updatedQueryBody = new QueryBody ( renderMustacheString (queryBody , requestParams ), false , false );
167
171
}
168
172
169
173
Map <String , String > urlParams = buildUrlParams (datasourceUrlParams , updatedQueryParams );
170
174
List <Property > bodyParams = mergeBody (datasourceBodyFormData , updatedQueryBodyParams );
171
175
172
176
URI uri = RestApiUriBuilder .buildUri (urlDomain , updatedQueryPath , requestParams , urlParams );
173
177
178
+ QueryBody mergedQueryBody = mergeBody (updatedQueryBody , datasourceBodyFormData );
174
179
return RestApiQueryExecutionContext .builder ()
175
180
.httpMethod (httpMethod )
176
181
.uri (uri )
@@ -179,7 +184,7 @@ public RestApiQueryExecutionContext buildQueryExecutionContext(RestApiDatasource
179
184
.urlParams (urlParams )
180
185
.bodyParams (bodyParams )
181
186
.encodeParams (encodeParams )
182
- .queryBody (mergeBody ( updatedQueryBody , datasourceBodyFormData , contentType ) )
187
+ .queryBody (mergedQueryBody )
183
188
.forwardCookies (forwardCookies )
184
189
.forwardAllCookies (forwardAllCookies )
185
190
.requestCookies (queryVisitorContext .getCookies ())
@@ -206,21 +211,19 @@ private List<Property> renderMustacheValueForQueryBody(List<Property> queryBodyP
206
211
}
207
212
208
213
209
- private String mergeBody (String queryBody , List <Property > datasourceBody , String contentType ) {
210
- if (CollectionUtils .isEmpty (datasourceBody )) {
214
+ private QueryBody mergeBody (QueryBody queryBody , List <Property > datasourceBody ) {
215
+ if (! queryBody . isJsonContent () || CollectionUtils .isEmpty (datasourceBody )) {
211
216
return queryBody ;
212
217
}
213
- if (! isJsonContentType ( contentType )) {
214
- return queryBody ;
215
- }
216
- Map < String , Object > map = JsonUtils . fromJsonMap ( queryBody );
217
- if ( map == null ) {
218
+ JsonNode jsonNode = queryBody . getJsonValue ();
219
+ if ( jsonNode instanceof ObjectNode objectNode ) {
220
+ for ( Property property : datasourceBody ) {
221
+ objectNode . put ( property . getKey (), property . getValue () );
222
+ }
218
223
return queryBody ;
219
224
}
220
- for (Property property : datasourceBody ) {
221
- map .putIfAbsent (property .getKey (), property .getValue ());
222
- }
223
- return JsonUtils .toJson (map );
225
+
226
+ return queryBody ;
224
227
}
225
228
226
229
@ Override
@@ -469,7 +472,7 @@ private Map<String, String> buildHeaders(List<Property> datasourceHeaders, List<
469
472
private BodyInserter <?, ? super ClientHttpRequest > buildBodyInserter (HttpMethod httpMethod ,
470
473
boolean isEncodeParams ,
471
474
String requestContentType ,
472
- String queryBody ,
475
+ QueryBody queryBody ,
473
476
List <Property > bodyFormData ) {
474
477
475
478
if (HttpMethod .GET .equals (httpMethod )) {
@@ -480,15 +483,19 @@ private Map<String, String> buildHeaders(List<Property> datasourceHeaders, List<
480
483
return BodyInserters .fromValue (new byte [0 ]);
481
484
}
482
485
483
- if (isJsonContentType (requestContentType )) {
484
- return BodyInserters .fromValue (parseJsonBody (queryBody ));
486
+ if (queryBody .isSpecialJson ()) {
487
+ return BodyInserters .fromValue (queryBody .getJsonValue ().toString ());
488
+ }
489
+
490
+ if (queryBody .isJsonContent ()) {
491
+ return BodyInserters .fromValue (queryBody .getJsonValue ());
485
492
}
486
493
487
494
if (MediaType .APPLICATION_FORM_URLENCODED_VALUE .equals (requestContentType )
488
495
|| MediaType .MULTIPART_FORM_DATA_VALUE .equals (requestContentType )) {
489
496
return dataUtils .buildBodyInserter (bodyFormData , requestContentType , isEncodeParams );
490
497
}
491
- return BodyInserters .fromValue (queryBody );
498
+ return BodyInserters .fromValue (queryBody . value () );
492
499
}
493
500
494
501
private boolean isNoneContentType (String requestContentType ) {
0 commit comments