Skip to content

Commit 1852f8d

Browse files
mduesterhoeftruibritopt
authored andcommitted
Serialize lists to empty list instead of null (#18)
1 parent 9cd0b43 commit 1852f8d

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

router-protobuf/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
testImplementation("org.assertj:assertj-core:3.11.1")
2525
testImplementation("io.mockk:mockk:1.8.13.kotlin13")
2626
testImplementation("org.slf4j:slf4j-simple:1.7.26")
27+
testImplementation("com.jayway.jsonpath:json-path:2.4.0")
2728
}
2829

2930

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ object ProtoBufUtils {
3434
private fun removeWrapperObjects(json: ObjectNode): ObjectNode {
3535
val result = jacksonObjectMapper().createObjectNode()
3636
for (entry in json.fields()) {
37-
if (entry.value.isContainerNode) {
37+
if (entry.value.isContainerNode && entry.value.size() > 0) {
3838
if (entry.value.size() > 0) {
3939
result.set(entry.key,
4040
removeWrapperObjects(entry.value)
4141
)
42-
} else {
43-
result.set(entry.key, jacksonObjectMapper().nodeFactory.nullNode())
4442
}
4543
} else {
4644
result.set(entry.key, entry.value)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.moia.router.proto
2+
3+
import com.google.protobuf.StringValue
4+
import com.jayway.jsonpath.JsonPath
5+
import io.moia.router.proto.sample.SampleOuterClass.ComplexSample
6+
import io.moia.router.proto.sample.SampleOuterClass.ComplexSample.SampleEnum.ONE
7+
import org.assertj.core.api.BDDAssertions.then
8+
import org.junit.jupiter.api.Test
9+
10+
class ProtoBufUtilsTest {
11+
12+
@Test
13+
fun `should serialize empty list`() {
14+
val message = ComplexSample.newBuilder()
15+
.addAllSamples(emptyList())
16+
.build()
17+
18+
val json = ProtoBufUtils.toJsonWithoutWrappers(message)
19+
20+
then(JsonPath.read<List<Any>>(json, "samples")).isEmpty()
21+
}
22+
23+
@Test
24+
fun `should remove wrapper object`() {
25+
val message = ComplexSample.newBuilder()
26+
.setSomeString(StringValue.newBuilder().setValue("some").build())
27+
.build()
28+
29+
val json = ProtoBufUtils.toJsonWithoutWrappers(message)
30+
31+
then(JsonPath.read<String>(json, "someString")).isEqualTo("some")
32+
}
33+
34+
@Test
35+
fun `should serialize value when it is the default`() {
36+
val message = ComplexSample.newBuilder()
37+
.setEnumAttribute(ONE) // enum zero value
38+
.build()
39+
40+
val json = ProtoBufUtils.toJsonWithoutWrappers(message)
41+
42+
then(JsonPath.read<String>(json, "enumAttribute")).isEqualTo("ONE")
43+
}
44+
}

router-protobuf/src/test/proto/Sample.proto

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ syntax = "proto3";
22

33
package io.moia.router.proto.sample;
44

5+
import "google/protobuf/wrappers.proto";
6+
57
message Sample {
68
string hello = 1;
79
string request = 2;
@@ -16,4 +18,16 @@ message UnprocessableEntityError {
1618
string message = 1;
1719
string code = 2;
1820
string path = 3;
19-
}
21+
}
22+
23+
message ComplexSample {
24+
enum SampleEnum {
25+
ONE = 0;
26+
TWO = 1;
27+
}
28+
29+
SampleEnum enumAttribute = 1;
30+
repeated Sample samples = 2;
31+
google.protobuf.StringValue someString = 3;
32+
}
33+

0 commit comments

Comments
 (0)