Skip to content

Commit

Permalink
Improves handling of null members in lists of arguments to the servic…
Browse files Browse the repository at this point in the history
…eJourneys GraphQL method.

With this change the null members are simply ignored, just like when passing null members to the ID list.
  • Loading branch information
eibakke committed Jan 10, 2025
1 parent 6a540d1 commit 5ed5c3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.opentripplanner.apis.transmodel.model.EnumTypes.MULTI_MODAL_MODE;
import static org.opentripplanner.apis.transmodel.model.EnumTypes.TRANSPORT_MODE;
import static org.opentripplanner.apis.transmodel.model.scalars.DateTimeScalarFactory.createMillisecondsSinceEpochAsDateTimeStringScalar;
import static org.opentripplanner.apis.transmodel.support.GqlUtil.mapArgCollectionNullSafe;
import static org.opentripplanner.model.projectinfo.OtpProjectInfo.projectInfo;

import graphql.Scalars;
Expand Down Expand Up @@ -1310,11 +1311,11 @@ private GraphQLSchema create() {
);
var privateCodes = FilterValues.ofEmptyIsEverything(
"privateCodes",
environment.<List<String>>getArgument("privateCodes")
mapArgCollectionNullSafe(environment.<List<String>>getArgument("privateCodes"))
);
var activeServiceDates = FilterValues.ofEmptyIsEverything(
"activeDates",
environment.<List<LocalDate>>getArgument("activeDates")
mapArgCollectionNullSafe(environment.<List<LocalDate>>getArgument("activeDates"))
);

TripRequest tripRequest = TripRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import javax.annotation.Nullable;
import org.opentripplanner.apis.transmodel.TransmodelRequestContext;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.framework.graphql.GraphQLUtils;
Expand All @@ -18,7 +21,7 @@

/**
* Provide some of the commonly used "chain" of methods. Like all ids should be created the same
* wayThis
* way.
*/
public class GqlUtil {

Expand Down Expand Up @@ -96,4 +99,16 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
? GraphQLUtils.getLocale(environment, lang)
: GraphQLUtils.getLocale(environment);
}

/**
* Null-safe handling of a collection of arguments of type T.
* Returns an empty collection if the collection is null.
* If the collection contains null elements, they are ignored.
*/
public static <T> List<T> mapArgCollectionNullSafe(@Nullable Collection<T> args) {
if (args == null) {
return List.of();
}
return args.stream().filter(Objects::nonNull).toList();
}
}

0 comments on commit 5ed5c3f

Please sign in to comment.