Skip to content

Commit b243d10

Browse files
authored
Improve handling of null values in argument lists to the serviceJourneys graphQL method in the Transmodel API (opentripplanner#6375)
* Improves handling of null members in lists of arguments to the serviceJourneys GraphQL method. With this change the null members are simply ignored, just like when passing null members to the ID list. * Improves naming of utility function.
1 parent 0312487 commit b243d10

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

application/src/main/java/org/opentripplanner/apis/transmodel/TransmodelGraphQLSchema.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.opentripplanner.apis.transmodel.model.EnumTypes.MULTI_MODAL_MODE;
1010
import static org.opentripplanner.apis.transmodel.model.EnumTypes.TRANSPORT_MODE;
1111
import static org.opentripplanner.apis.transmodel.model.scalars.DateTimeScalarFactory.createMillisecondsSinceEpochAsDateTimeStringScalar;
12+
import static org.opentripplanner.apis.transmodel.support.GqlUtil.toListNullSafe;
1213
import static org.opentripplanner.model.projectinfo.OtpProjectInfo.projectInfo;
1314

1415
import graphql.Scalars;
@@ -1310,11 +1311,11 @@ private GraphQLSchema create() {
13101311
);
13111312
var privateCodes = FilterValues.ofEmptyIsEverything(
13121313
"privateCodes",
1313-
environment.<List<String>>getArgument("privateCodes")
1314+
toListNullSafe(environment.<List<String>>getArgument("privateCodes"))
13141315
);
13151316
var activeServiceDates = FilterValues.ofEmptyIsEverything(
13161317
"activeDates",
1317-
environment.<List<LocalDate>>getArgument("activeDates")
1318+
toListNullSafe(environment.<List<LocalDate>>getArgument("activeDates"))
13181319
);
13191320

13201321
TripRequest tripRequest = TripRequest

application/src/main/java/org/opentripplanner/apis/transmodel/support/GqlUtil.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import graphql.schema.GraphQLInputObjectField;
77
import graphql.schema.GraphQLList;
88
import graphql.schema.GraphQLNonNull;
9+
import java.util.Collection;
910
import java.util.List;
1011
import java.util.Locale;
12+
import java.util.Objects;
13+
import javax.annotation.Nullable;
1114
import org.opentripplanner.apis.transmodel.TransmodelRequestContext;
1215
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
1316
import org.opentripplanner.framework.graphql.GraphQLUtils;
@@ -18,7 +21,7 @@
1821

1922
/**
2023
* Provide some of the commonly used "chain" of methods. Like all ids should be created the same
21-
* wayThis
24+
* way.
2225
*/
2326
public class GqlUtil {
2427

@@ -96,4 +99,15 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
9699
? GraphQLUtils.getLocale(environment, lang)
97100
: GraphQLUtils.getLocale(environment);
98101
}
102+
103+
/**
104+
* Null-safe handling of a collection of type T. Returns an empty list if the collection is null.
105+
* Null elements are filtered out.
106+
*/
107+
public static <T> List<T> toListNullSafe(@Nullable Collection<T> args) {
108+
if (args == null) {
109+
return List.of();
110+
}
111+
return args.stream().filter(Objects::nonNull).toList();
112+
}
99113
}

0 commit comments

Comments
 (0)