Skip to content

Commit 82ef34c

Browse files
committedFeb 24, 2025
fix(core): content type encoding should not be mandatory
1 parent e312ece commit 82ef34c

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed
 

‎core/src/main/java/io/kestra/core/http/HttpRequest.java

+28-28
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public abstract static class RequestBody {
148148

149149
public abstract String getContentType() throws IOException;
150150

151+
protected ContentType entityContentType() throws IOException {
152+
return this.getCharset() != null ? ContentType.create(this.getContentType(), this.getCharset()) : ContentType.create(this.getContentType());
153+
}
154+
151155
public static RequestBody from(HttpEntity entity) throws IOException {
152156
if (entity == null) {
153157
return null;
@@ -193,13 +197,12 @@ public static class InputStreamRequestBody extends RequestBody {
193197
@Builder.Default
194198
private String contentType = ContentType.APPLICATION_OCTET_STREAM.getMimeType();
195199

196-
@Builder.Default
197-
private Charset charset = StandardCharsets.UTF_8;
200+
private Charset charset;
198201

199202
private InputStream content;
200203

201-
public HttpEntity to() {
202-
return new InputStreamEntity(content, ContentType.create(contentType, charset));
204+
public HttpEntity to() throws IOException {
205+
return new InputStreamEntity(content, this.entityContentType());
203206
}
204207
}
205208

@@ -210,13 +213,12 @@ public static class StringRequestBody extends RequestBody {
210213
@Builder.Default
211214
private String contentType = ContentType.TEXT_PLAIN.getMimeType();
212215

213-
@Builder.Default
214-
private Charset charset = StandardCharsets.UTF_8;
216+
private Charset charset;
215217

216218
private String content;
217219

218-
public HttpEntity to() {
219-
return new StringEntity(this.content, ContentType.create(contentType, charset));
220+
public HttpEntity to() throws IOException {
221+
return new StringEntity(this.content, this.entityContentType());
220222
}
221223
}
222224

@@ -227,22 +229,20 @@ public static class ByteArrayRequestBody extends RequestBody {
227229
@Builder.Default
228230
private String contentType = ContentType.APPLICATION_OCTET_STREAM.getMimeType();
229231

230-
@Builder.Default
231-
private Charset charset = StandardCharsets.UTF_8;
232+
private Charset charset;
232233

233234
private byte[] content;
234235

235-
public HttpEntity to() {
236-
return new ByteArrayEntity(content, ContentType.create(contentType, charset));
236+
public HttpEntity to() throws IOException {
237+
return new ByteArrayEntity(content, this.entityContentType());
237238
}
238239
}
239240

240241
@Getter
241242
@AllArgsConstructor
242243
@SuperBuilder
243244
public static class JsonRequestBody extends RequestBody {
244-
@Builder.Default
245-
private Charset charset = StandardCharsets.UTF_8;
245+
private Charset charset;
246246

247247
private Object content;
248248

@@ -255,7 +255,7 @@ public HttpEntity to() throws IOException {
255255
try {
256256
return new StringEntity(
257257
JacksonMapper.ofJson().writeValueAsString(content),
258-
ContentType.APPLICATION_JSON.withCharset(this.charset)
258+
this.charset != null ? ContentType.APPLICATION_JSON.withCharset(this.charset) : ContentType.APPLICATION_JSON
259259
);
260260
} catch (JsonProcessingException e) {
261261
throw new IOException(e);
@@ -267,8 +267,7 @@ public HttpEntity to() throws IOException {
267267
@AllArgsConstructor
268268
@SuperBuilder
269269
public static class UrlEncodedRequestBody extends RequestBody {
270-
@Builder.Default
271-
private Charset charset = StandardCharsets.UTF_8;
270+
private Charset charset;
272271

273272
private Map<String, Object> content;
274273

@@ -278,22 +277,20 @@ public String getContentType() throws IOException {
278277
}
279278

280279
public HttpEntity to() throws IOException {
281-
return new UrlEncodedFormEntity(
282-
this.content .entrySet()
283-
.stream()
284-
.map(e -> new BasicNameValuePair(e.getKey(), e.getValue().toString()))
285-
.toList(),
286-
this.charset
287-
);
280+
List<BasicNameValuePair> list = this.content.entrySet()
281+
.stream()
282+
.map(e -> new BasicNameValuePair(e.getKey(), e.getValue().toString()))
283+
.toList();
284+
285+
return this.charset != null ? new UrlEncodedFormEntity(list, this.charset) : new UrlEncodedFormEntity(list);
288286
}
289287
}
290288

291289
@Getter
292290
@AllArgsConstructor
293291
@SuperBuilder
294292
public static class MultipartRequestBody extends RequestBody {
295-
@Builder.Default
296-
private Charset charset = StandardCharsets.UTF_8;
293+
private Charset charset;
297294

298295
private Map<String, Object> content;
299296

@@ -304,8 +301,11 @@ public String getContentType() throws IOException {
304301

305302
public HttpEntity to() throws IOException {
306303
MultipartEntityBuilder builder = MultipartEntityBuilder
307-
.create()
308-
.setCharset(this.charset);
304+
.create();
305+
306+
if (this.charset != null) {
307+
builder.setCharset(this.charset);
308+
}
309309

310310
content.forEach((key, value) -> {
311311
switch (value) {

‎core/src/main/java/io/kestra/plugin/core/http/AbstractHttp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected HttpRequest request(RunContext runContext) throws IllegalVariableEvalu
158158
request.body(HttpRequest.StringRequestBody.builder()
159159
.content(runContext.render(body).as(String.class).orElseThrow())
160160
.contentType(runContext.render(this.contentType).as(String.class).orElse(null))
161-
.charset(this.options != null ? runContext.render(this.options.getDefaultCharset()).as(Charset.class).orElse(null) : StandardCharsets.UTF_8)
161+
.charset(this.options != null && this.options.getDefaultCharset() != null ? runContext.render(this.options.getDefaultCharset()).as(Charset.class).orElse(null) : null)
162162
.build()
163163
);
164164
} else if (this.contentType != null) {

‎core/src/test/java/io/kestra/plugin/core/http/RequestTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.micronaut.http.annotation.*;
2121
import io.micronaut.http.multipart.StreamingFileUpload;
2222
import io.micronaut.runtime.server.EmbeddedServer;
23+
import jakarta.annotation.Nullable;
2324
import jakarta.inject.Inject;
2425
import org.apache.commons.io.IOUtils;
2526
import org.junit.jupiter.api.Test;
@@ -533,7 +534,10 @@ void specialContentType() throws Exception {
533534
.id(RequestTest.class.getSimpleName())
534535
.type(RequestTest.class.getName())
535536
.uri(Property.of(server.getURL().toString() + "/content-type"))
537+
.method(Property.of("POST"))
538+
.body(Property.of("{}"))
536539
.contentType(Property.of("application/vnd.campaignsexport.v1+json"))
540+
.options(HttpConfiguration.builder().logs(HttpConfiguration.LoggingType.values()).defaultCharset(null).build())
537541
.build();
538542

539543
RunContext runContext = TestsUtils.mockRunContext(this.runContextFactory, task, ImmutableMap.of());
@@ -552,10 +556,10 @@ HttpResponse<String> hello() {
552556
return HttpResponse.ok("{ \"hello\": \"world\" }");
553557
}
554558

555-
556-
@Get("content-type")
559+
@Post("content-type")
560+
@Consumes("application/vnd.campaignsexport.v1+json")
557561
@Produces(MediaType.TEXT_PLAIN)
558-
public io.micronaut.http.HttpResponse<String> contentType(io.micronaut.http.HttpRequest<?> request) {
562+
public io.micronaut.http.HttpResponse<String> contentType(io.micronaut.http.HttpRequest<?> request, @Nullable @Body Map<String, String> body) {
559563
return io.micronaut.http.HttpResponse.ok(request.getContentType().orElseThrow().toString());
560564
}
561565

0 commit comments

Comments
 (0)