Skip to content

Commit ce6a575

Browse files
fix: support a different arg name for plural args (#39)
1 parent 2b1c58f commit ce6a575

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

hypertrace-core-graphql-deserialization/src/main/java/org/hypertrace/core/graphql/deserialization/ArgumentDeserializationConfig.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ public interface ArgumentDeserializationConfig {
88

99
String getArgumentKey();
1010

11+
default String getListArgumentKey() {
12+
return getArgumentKey();
13+
}
14+
1115
Class<?> getArgumentSchema();
1216

1317
default List<Module> jacksonModules() {
1418
return Collections.emptyList();
1519
}
1620

1721
static ArgumentDeserializationConfig forPrimitive(String argKey, Class<?> argSchema) {
18-
return new SimpleArgumentDeserializationConfig(argKey, argSchema);
22+
return forPrimitive(argKey, argKey, argSchema);
23+
}
24+
25+
static ArgumentDeserializationConfig forPrimitive(
26+
String argKey, String listArgKey, Class<?> argSchema) {
27+
return new SimpleArgumentDeserializationConfig(argKey, listArgKey, argSchema);
1928
}
2029
}

hypertrace-core-graphql-deserialization/src/main/java/org/hypertrace/core/graphql/deserialization/DefaultArgumentDeserializer.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Objects;
1111
import java.util.Optional;
1212
import java.util.Set;
13+
import java.util.function.Function;
1314
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
1516
import javax.annotation.Nullable;
@@ -23,7 +24,7 @@ class DefaultArgumentDeserializer implements ArgumentDeserializer {
2324
private static final Logger LOG = LoggerFactory.getLogger(DefaultArgumentDeserializer.class);
2425

2526
private final ObjectMapper objectMapper;
26-
private final Map<Class<?>, String> argKeyBySchema;
27+
private final Map<Class<?>, ArgumentDeserializationConfig> configBySchema;
2728

2829
@Inject
2930
DefaultArgumentDeserializer(Set<ArgumentDeserializationConfig> argumentDeserializationConfigs) {
@@ -35,39 +36,38 @@ class DefaultArgumentDeserializer implements ArgumentDeserializer {
3536
.flatMap(Collection::stream))
3637
.collect(Collectors.toUnmodifiableSet());
3738

38-
this.argKeyBySchema =
39+
this.configBySchema =
3940
argumentDeserializationConfigs.stream()
4041
.collect(
4142
Collectors.toUnmodifiableMap(
42-
ArgumentDeserializationConfig::getArgumentSchema,
43-
ArgumentDeserializationConfig::getArgumentKey));
43+
ArgumentDeserializationConfig::getArgumentSchema, Function.identity()));
4444

4545
this.objectMapper = JsonMapper.builder().addModules(jacksonModules).build();
4646
}
4747

4848
@Override
4949
public <T> Optional<T> deserializeObject(Map<String, Object> arguments, Class<T> argSchema) {
50-
return this.argumentKeyForSchema(argSchema)
50+
return this.argumentKey(argSchema)
5151
.flatMap(key -> this.deserializeValue(arguments.get(key), argSchema));
5252
}
5353

5454
@Override
5555
public <T> Optional<List<T>> deserializeObjectList(
5656
Map<String, Object> arguments, Class<T> argSchema) {
57-
return this.argumentKeyForSchema(argSchema)
57+
return this.listArgumentKey(argSchema)
5858
.flatMap(key -> this.deserializeValueList(arguments.get(key), argSchema));
5959
}
6060

6161
@Override
6262
public <T> Optional<T> deserializePrimitive(
6363
Map<String, Object> arguments, Class<? extends PrimitiveArgument<T>> argSchema) {
64-
return this.argumentKeyForSchema(argSchema).map(key -> this.uncheckedCast(arguments.get(key)));
64+
return this.argumentKey(argSchema).map(key -> this.uncheckedCast(arguments.get(key)));
6565
}
6666

6767
@Override
6868
public <T> Optional<List<T>> deserializePrimitiveList(
6969
Map<String, Object> arguments, Class<? extends PrimitiveArgument<T>> argSchema) {
70-
return this.argumentKeyForSchema(argSchema)
70+
return this.listArgumentKey(argSchema)
7171
.flatMap(key -> this.logIfNotList(arguments.get(key)));
7272
}
7373

@@ -81,8 +81,16 @@ private <T> Optional<T> deserializeValue(@Nullable Object rawValue, Class<T> arg
8181
}
8282
}
8383

84-
private Optional<String> argumentKeyForSchema(Class<?> argSchema) {
85-
return Optional.ofNullable(this.argKeyBySchema.get(argSchema))
84+
private Optional<String> argumentKey(Class<?> argSchema) {
85+
return configForSchema(argSchema).map(ArgumentDeserializationConfig::getArgumentKey);
86+
}
87+
88+
private Optional<String> listArgumentKey(Class<?> argSchema) {
89+
return configForSchema(argSchema).map(ArgumentDeserializationConfig::getListArgumentKey);
90+
}
91+
92+
private Optional<ArgumentDeserializationConfig> configForSchema(Class<?> argSchema) {
93+
return Optional.ofNullable(this.configBySchema.get(argSchema))
8694
.or(
8795
() -> {
8896
LOG.warn(

hypertrace-core-graphql-deserialization/src/main/java/org/hypertrace/core/graphql/deserialization/SimpleArgumentDeserializationConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
@Value
66
class SimpleArgumentDeserializationConfig implements ArgumentDeserializationConfig {
77
String argumentKey;
8+
String listArgumentKey;
89
Class<?> argumentSchema;
910
}

hypertrace-core-graphql-deserialization/src/test/java/org/hypertrace/core/graphql/deserialization/DefaultArgumentDeserializerTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
import java.util.Set;
1313
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.Test;
15-
import org.junit.jupiter.api.extension.ExtendWith;
16-
import org.mockito.junit.jupiter.MockitoExtension;
1715

18-
@ExtendWith(MockitoExtension.class)
1916
class DefaultArgumentDeserializerTest {
2017

2118
private DefaultArgumentDeserializer argumentDeserializer;
2219

2320
private interface TestPrimitiveArgument extends PrimitiveArgument<String> {
2421
String ARG_NAME = "primitiveArg";
22+
String LIST_ARG_NAME = "primitiveArgs";
2523
}
2624

2725
private interface TestObjectArgument {
@@ -46,17 +44,10 @@ void beforeEach() {
4644
this.argumentDeserializer =
4745
new DefaultArgumentDeserializer(
4846
Set.of(
49-
new ArgumentDeserializationConfig() {
50-
@Override
51-
public String getArgumentKey() {
52-
return TestPrimitiveArgument.ARG_NAME;
53-
}
54-
55-
@Override
56-
public Class<?> getArgumentSchema() {
57-
return TestPrimitiveArgument.class;
58-
}
59-
},
47+
ArgumentDeserializationConfig.forPrimitive(
48+
TestPrimitiveArgument.ARG_NAME,
49+
TestPrimitiveArgument.LIST_ARG_NAME,
50+
TestPrimitiveArgument.class),
6051
new ArgumentDeserializationConfig() {
6152
@Override
6253
public String getArgumentKey() {
@@ -166,22 +157,29 @@ void deserializePrimitive() {
166157
void emptyIfNoPrimitivePresent() {
167158
assertEquals(
168159
Optional.empty(),
169-
this.argumentDeserializer.deserializeObject(
160+
this.argumentDeserializer.deserializePrimitive(
170161
Collections.emptyMap(), TestPrimitiveArgument.class));
171162
}
172163

173164
@Test
174165
void deserializePrimitiveList() {
175166
Map<String, Object> argMap =
176-
Map.of(TestPrimitiveArgument.ARG_NAME, List.of("foo", "bar", "baz"));
167+
Map.of(TestPrimitiveArgument.LIST_ARG_NAME, List.of("foo", "bar", "baz"));
177168
assertEquals(
178169
Optional.of(List.of("foo", "bar", "baz")),
179170
this.argumentDeserializer.deserializePrimitiveList(argMap, TestPrimitiveArgument.class));
171+
172+
// Make sure only the list arg name works
173+
argMap =
174+
Map.of(TestPrimitiveArgument.ARG_NAME, List.of("foo", "bar", "baz"));
175+
assertEquals(
176+
Optional.empty(),
177+
this.argumentDeserializer.deserializePrimitiveList(argMap, TestPrimitiveArgument.class));
180178
}
181179

182180
@Test
183181
void emptyListForEmptyPrimitiveList() {
184-
Map<String, Object> argMap = Map.of(TestPrimitiveArgument.ARG_NAME, List.of());
182+
Map<String, Object> argMap = Map.of(TestPrimitiveArgument.LIST_ARG_NAME, List.of());
185183
assertEquals(
186184
Optional.of(List.of()),
187185
this.argumentDeserializer.deserializePrimitiveList(argMap, TestPrimitiveArgument.class));

0 commit comments

Comments
 (0)