Skip to content

Commit

Permalink
Improve handling of null values in argument lists to the serviceJourn…
Browse files Browse the repository at this point in the history
…eys graphQL method in the Transmodel API (#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.
  • Loading branch information
eibakke authored Jan 14, 2025
1 parent 0312487 commit b243d10
Show file tree
Hide file tree
Showing 2 changed files with 18 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.toListNullSafe;
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")
toListNullSafe(environment.<List<String>>getArgument("privateCodes"))
);
var activeServiceDates = FilterValues.ofEmptyIsEverything(
"activeDates",
environment.<List<LocalDate>>getArgument("activeDates")
toListNullSafe(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,15 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
? GraphQLUtils.getLocale(environment, lang)
: GraphQLUtils.getLocale(environment);
}

/**
* Null-safe handling of a collection of type T. Returns an empty list if the collection is null.
* Null elements are filtered out.
*/
public static <T> List<T> toListNullSafe(@Nullable Collection<T> args) {
if (args == null) {
return List.of();
}
return args.stream().filter(Objects::nonNull).toList();
}
}

0 comments on commit b243d10

Please sign in to comment.