File tree Expand file tree Collapse file tree 4 files changed +61
-4
lines changed
main/kotlin/io/moia/router/proto
kotlin/io/moia/router/proto Expand file tree Collapse file tree 4 files changed +61
-4
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ dependencies {
24
24
testImplementation(" org.assertj:assertj-core:3.11.1" )
25
25
testImplementation(" io.mockk:mockk:1.8.13.kotlin13" )
26
26
testImplementation(" org.slf4j:slf4j-simple:1.7.26" )
27
+ testImplementation(" com.jayway.jsonpath:json-path:2.4.0" )
27
28
}
28
29
29
30
Original file line number Diff line number Diff line change @@ -34,13 +34,11 @@ object ProtoBufUtils {
34
34
private fun removeWrapperObjects (json : ObjectNode ): ObjectNode {
35
35
val result = jacksonObjectMapper().createObjectNode()
36
36
for (entry in json.fields()) {
37
- if (entry.value.isContainerNode) {
37
+ if (entry.value.isContainerNode && entry.value.size() > 0 ) {
38
38
if (entry.value.size() > 0 ) {
39
39
result.set(entry.key,
40
40
removeWrapperObjects(entry.value)
41
41
)
42
- } else {
43
- result.set(entry.key, jacksonObjectMapper().nodeFactory.nullNode())
44
42
}
45
43
} else {
46
44
result.set(entry.key, entry.value)
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ syntax = "proto3";
2
2
3
3
package io.moia.router.proto.sample ;
4
4
5
+ import "google/protobuf/wrappers.proto" ;
6
+
5
7
message Sample {
6
8
string hello = 1 ;
7
9
string request = 2 ;
@@ -16,4 +18,16 @@ message UnprocessableEntityError {
16
18
string message = 1 ;
17
19
string code = 2 ;
18
20
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
+
You can’t perform that action at this time.
0 commit comments