Skip to content

Commit 2cbc8da

Browse files
ennoellerennoeller
andauthored
Retrofit response body mapper fix (#44)
Co-authored-by: ennoeller <enno@bitweb.ee>
1 parent 18cc9c2 commit 2cbc8da

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
}
2020

2121
group 'ee.bitweb'
22-
version '4.0.1'
22+
version '4.0.2'
2323
java {
2424
sourceCompatibility = '17'
2525
}

src/main/java/ee/bitweb/core/retrofit/logging/mappers/RetrofitResponseBodyMapper.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ protected String sanitizeBody(String body) {
6666
private String parseBody(Response response) throws IOException {
6767
var responseBody = response.body();
6868
var contentType = responseBody.contentType();
69-
var contentLength = responseBody.contentLength();
7069
var charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
7170

7271
var source = responseBody.source();
@@ -84,18 +83,16 @@ private String parseBody(Response response) throws IOException {
8483
return "(binary %s-byte body omitted)".formatted(buffer.size());
8584
}
8685

87-
if (contentLength != 0L) {
88-
var bodyString = buffer.clone().readString(charset);
89-
if (contentLength > maxLoggableResponseSize) {
90-
return "%s ... Content size: %s characters".formatted(
91-
bodyString.substring(0, maxLoggableResponseSize),
92-
contentLength
93-
);
94-
}
86+
var bodyString = buffer.clone().readString(charset);
87+
var bodyStringLength = bodyString != null ? bodyString.length() : 0;
9588

96-
return sanitizeBody(bodyString);
89+
if (bodyStringLength > maxLoggableResponseSize) {
90+
return "%s ... Content size: %s characters".formatted(
91+
bodyString.substring(0, maxLoggableResponseSize),
92+
bodyStringLength
93+
);
9794
} else {
98-
return "";
95+
return sanitizeBody(bodyString);
9996
}
10097
}
10198
}

0 commit comments

Comments
 (0)