Skip to content

Commit 12365b7

Browse files
committed
Fix, when case of multiple non-default public constructors use the largest constructor
1 parent 4ac3b33 commit 12365b7

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package org.example.customer;
22

33
import io.avaje.jsonb.Json;
4+
5+
import java.util.Collections;
46
import java.util.List;
57

68
@Json
7-
public record SomeAddressWrapper (Long id, Address address, List<String> tags) {
9+
public record SomeAddressWrapper(Long id, Address address, List<String> tags) {
10+
11+
/**
12+
* Additional constructor.
13+
*/
14+
public SomeAddressWrapper(long id, Address address) {
15+
this(id, address, Collections.emptyList());
16+
}
817
}

blackbox-test/src/test/java/org/example/customer/SomeAddressWrapperTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class SomeAddressWrapperTest {
1818
void test_when_null() {
1919

2020
var type = jsonb.type(SomeAddressWrapper.class);
21-
String asJson = type.toJson(new SomeAddressWrapper(43L, null, Collections.emptyList()));
22-
assertThat(asJson).isEqualTo("{\"id\":43}");
21+
String asJson = type.toJson(new SomeAddressWrapper(43L, null, List.of("a", "b")));
22+
assertThat(asJson).isEqualTo("{\"id\":43,\"tags\":[\"a\",\"b\"]}");
2323

24-
var myList = List.of( new SomeAddressWrapper(43L, null, Collections.emptyList()), new SomeAddressWrapper(44L, null, Collections.emptyList()));
24+
var myList = List.of( new SomeAddressWrapper(43L, null), new SomeAddressWrapper(44L, null));
2525

2626
String asJsonList = type.list().toJson(myList);
2727
assertThat(asJsonList).isEqualTo("[{\"id\":43},{\"id\":44}]");
@@ -59,7 +59,7 @@ void includeEmpty_viaJsonB() {
5959
Jsonb jsonb = Jsonb.newBuilder().serializeEmpty(true).build();
6060

6161
var type = jsonb.type(SomeAddressWrapper.class);
62-
String asJson = type.toJson(new SomeAddressWrapper(43L, null, Collections.emptyList()));
62+
String asJson = type.toJson(new SomeAddressWrapper(43L, null));
6363
assertThat(asJson).isEqualTo("{\"id\":43,\"tags\":[]}");
6464
}
6565

@@ -70,7 +70,7 @@ void includeNullAndEmpty_viaJsonB() {
7070
Jsonb jsonb = Jsonb.newBuilder().serializeNulls(true).serializeEmpty(true).build();
7171

7272
var type = jsonb.type(SomeAddressWrapper.class);
73-
String asJson = type.toJson(new SomeAddressWrapper(43L, null, Collections.emptyList()));
73+
String asJson = type.toJson(new SomeAddressWrapper(43L, null));
7474
assertThat(asJson).isEqualTo("{\"id\":43,\"address\":null,\"tags\":[]}");
7575
}
7676
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,20 @@ private MethodReader determineConstructor() {
264264
// fallback to the single public constructor
265265
return allPublic.get(0);
266266
}
267-
return null;
267+
// find the largest constructor
268+
int argCount = 0;
269+
MethodReader largestConstructor = null;
270+
for (MethodReader ctor : publicConstructors) {
271+
if (ctor.isPublic()) {
272+
int paramCount = ctor.getParams().size();
273+
if (paramCount > argCount) {
274+
largestConstructor = ctor;
275+
argCount = paramCount;
276+
}
277+
}
278+
}
279+
280+
return largestConstructor;
268281
}
269282

270283
void process() {

0 commit comments

Comments
 (0)