Skip to content

Commit a0bd438

Browse files
authored
Fix Custom Adapter Priority (#447)
* fix custom adapter priority it seems that custom adapters could be overshadowed in some cases * Update JsonbProcessor.java
1 parent d80214c commit a0bd438

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.example.other.custom;
2+
3+
import java.lang.reflect.Type;
4+
import java.util.List;
5+
6+
import io.avaje.json.JsonAdapter;
7+
import io.avaje.json.JsonReader;
8+
import io.avaje.json.JsonWriter;
9+
import io.avaje.jsonb.AdapterFactory;
10+
import io.avaje.jsonb.CustomAdapter;
11+
import io.avaje.jsonb.Json;
12+
import io.avaje.jsonb.Jsonb;
13+
import io.avaje.jsonb.Types;
14+
15+
@CustomAdapter
16+
public class CustomClashJsonAdapter<T>
17+
implements JsonAdapter<CustomClashJsonAdapter.NestedGeneric<T>> {
18+
19+
public static final AdapterFactory FACTORY =
20+
(type, jsonb) -> {
21+
if (Types.isGenericTypeOf(type, NestedGeneric.class)) {
22+
return new CustomClashJsonAdapter<>(jsonb, Types.typeArguments(type));
23+
}
24+
return null;
25+
};
26+
27+
public CustomClashJsonAdapter(Jsonb jsonb, Type[] types) {}
28+
29+
public record NestedGeneric<T>(T value) {}
30+
31+
@Json
32+
public record NestedGeneric2(List<NestedGeneric<String>> value) {}
33+
34+
@Override
35+
public void toJson(JsonWriter writer, NestedGeneric<T> value) {}
36+
37+
@Override
38+
public NestedGeneric<T> fromJson(JsonReader reader) {
39+
return null;
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.example.other.custom;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import org.junit.jupiter.api.AfterAll;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.avaje.jsonb.Jsonb;
11+
import io.avaje.jsonb.Types;
12+
13+
class CustomClashJsonAdapterTest {
14+
15+
@Test
16+
void test() {
17+
assertThat(Jsonb.instance().adapter(CustomClashJsonAdapter.NestedGeneric.class))
18+
.isInstanceOf(CustomClashJsonAdapter.class);
19+
20+
assertThat(
21+
Jsonb.instance()
22+
.adapter(Types.newParameterizedType(CustomClashJsonAdapter.class, String.class)))
23+
.isInstanceOf(CustomClashJsonAdapter.class);
24+
}
25+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import static io.avaje.jsonb.generator.APContext.logError;
55
import static io.avaje.jsonb.generator.APContext.logNote;
66
import static io.avaje.jsonb.generator.APContext.typeElement;
7-
import static io.avaje.jsonb.generator.Constants.*;
7+
import static io.avaje.jsonb.generator.Constants.JSON;
8+
import static io.avaje.jsonb.generator.Constants.JSON_IMPORT;
9+
import static io.avaje.jsonb.generator.Constants.JSON_IMPORT_LIST;
10+
import static io.avaje.jsonb.generator.Constants.JSON_MIXIN;
811
import static io.avaje.jsonb.generator.ProcessingContext.addImportedPrism;
912
import static io.avaje.jsonb.generator.ProcessingContext.createMetaInfWriterFor;
10-
import static io.avaje.jsonb.generator.ValuePrism.*;
1113
import static java.util.stream.Collectors.joining;
1214

1315
import java.io.IOException;
@@ -115,14 +117,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
115117
}
116118
APContext.setProjectModuleElement(annotations, round);
117119
readModule();
118-
119-
getElements(round, CustomAdapterPrism.PRISM_TYPE).ifPresent(this::registerCustomAdapters);
120120
getElements(round, ValuePrism.PRISM_TYPE).ifPresent(this::writeValueAdapters);
121121
getElements(round, JSON).ifPresent(this::writeAdapters);
122122
getElements(round, JSON_MIXIN).ifPresent(this::writeAdaptersForMixInTypes);
123123
getElements(round, JSON_IMPORT_LIST).ifPresent(this::writeAdaptersForImportedList);
124124
getElements(round, JSON_IMPORT).ifPresent(this::writeAdaptersForImported);
125125
getElements(round, "io.avaje.spi.ServiceProvider").ifPresent(this::registerSPI);
126+
getElements(round, CustomAdapterPrism.PRISM_TYPE).ifPresent(this::registerCustomAdapters);
126127

127128
metaData.fullName(false);
128129
cascadeTypes();

0 commit comments

Comments
 (0)