From 711e6a8e0c9845b5414b10d6084f46112d50dce2 Mon Sep 17 00:00:00 2001 From: Joel Lappalainen Date: Wed, 15 Mar 2023 14:12:08 +0200 Subject: [PATCH 1/3] Use accept-language header if defined instead of default routing request locale --- .../ext/legacygraphqlapi/LegacyGraphQLUtils.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ext/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtils.java b/src/ext/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtils.java index 988e6a1f81d..8ccaebdc794 100644 --- a/src/ext/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtils.java +++ b/src/ext/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtils.java @@ -29,12 +29,19 @@ public static Locale getLocale(DataFetchingEnvironment environment, String local return Locale.forLanguageTag(localeString); } + // This can come from the accept-language header + var envLocale = environment.getLocale(); + Map localContext = environment.getLocalContext(); - if (localContext != null && localContext.get("locale") != null) { + if ( + (envLocale == null || envLocale.getLanguage().equals("*")) && + localContext != null && + localContext.get("locale") != null + ) { return (Locale) localContext.get("locale"); } - return environment.getLocale(); + return envLocale; } public static String getTranslation(I18NString input, DataFetchingEnvironment environment) { From a683fcd70ee5b8e1c03a6a62f1c4dc89c5605aba Mon Sep 17 00:00:00 2001 From: Joel Lappalainen Date: Wed, 15 Mar 2023 14:58:44 +0200 Subject: [PATCH 2/3] Add tests --- .../LegacyGraphQLUtilsTest.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/ext-test/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtilsTest.java diff --git a/src/ext-test/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtilsTest.java b/src/ext-test/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtilsTest.java new file mode 100644 index 00000000000..5547a006918 --- /dev/null +++ b/src/ext-test/java/org/opentripplanner/ext/legacygraphqlapi/LegacyGraphQLUtilsTest.java @@ -0,0 +1,124 @@ +package org.opentripplanner.ext.legacygraphqlapi; + +import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import graphql.ExecutionInput; +import graphql.execution.ExecutionContext; +import graphql.execution.ExecutionId; +import graphql.schema.DataFetchingEnvironmentImpl; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.opentripplanner._support.time.ZoneIds; +import org.opentripplanner.ext.fares.impl.DefaultFareService; +import org.opentripplanner.routing.api.request.RouteRequest; +import org.opentripplanner.routing.graph.Graph; +import org.opentripplanner.routing.graphfinder.GraphFinder; +import org.opentripplanner.service.vehiclepositions.internal.DefaultVehiclePositionService; +import org.opentripplanner.service.vehiclerental.internal.DefaultVehicleRentalService; +import org.opentripplanner.transit.service.DefaultTransitService; +import org.opentripplanner.transit.service.TransitModel; + +class LegacyGraphQLUtilsTest { + + static final LegacyGraphQLRequestContext context; + + static { + Graph graph = new Graph(); + var transitModel = new TransitModel(); + transitModel.initTimeZone(ZoneIds.BERLIN); + final DefaultTransitService transitService = new DefaultTransitService(transitModel); + context = + new LegacyGraphQLRequestContext( + new TestRoutingService(List.of()), + transitService, + new DefaultFareService(), + graph.getVehicleParkingService(), + new DefaultVehicleRentalService(), + new DefaultVehiclePositionService(), + GraphFinder.getInstance(graph, transitService::findRegularStop), + new RouteRequest() + ); + } + + @Test + void testGetLocaleWithDefinedLocaleArg() { + var executionContext = getGenericExecutionContext(); + + var env = DataFetchingEnvironmentImpl + .newDataFetchingEnvironment(executionContext) + .localContext(Map.of("locale", Locale.GERMAN)) + .locale(Locale.ENGLISH) + .build(); + + var frenchLocale = Locale.FRENCH; + + var locale = LegacyGraphQLUtils.getLocale(env, frenchLocale.toString()); + + assertEquals(frenchLocale, locale); + } + + @Test + void testGetLocaleWithEnvLocale() { + var executionContext = getGenericExecutionContext(); + + var frenchLocale = Locale.FRENCH; + var env = DataFetchingEnvironmentImpl + .newDataFetchingEnvironment(executionContext) + .localContext(Map.of("locale", Locale.GERMAN)) + .locale(frenchLocale) + .build(); + + var locale = LegacyGraphQLUtils.getLocale(env); + + assertEquals(frenchLocale, locale); + } + + @Test + void testGetLocaleWithLocalContextLocale() { + var executionContext = getGenericExecutionContext(); + + // Should use locale from local context if env locale is not defined + + var frenchLocale = Locale.FRENCH; + var envWithNoLocale = DataFetchingEnvironmentImpl + .newDataFetchingEnvironment(executionContext) + .localContext(Map.of("locale", Locale.FRENCH)) + .build(); + + var locale = LegacyGraphQLUtils.getLocale(envWithNoLocale); + + assertEquals(frenchLocale, locale); + + // Wildcard locale from env should not override locale from local context if it's defined + + var wildcardLocale = new Locale("*"); + + var envWithWildcardLocale = DataFetchingEnvironmentImpl + .newDataFetchingEnvironment(executionContext) + .locale(wildcardLocale) + .localContext(Map.of("locale", Locale.FRENCH)) + .build(); + + locale = LegacyGraphQLUtils.getLocale(envWithWildcardLocale); + + assertEquals(frenchLocale, locale); + } + + private ExecutionContext getGenericExecutionContext() { + ExecutionInput executionInput = ExecutionInput + .newExecutionInput() + .query("") + .operationName("plan") + .context(context) + .locale(Locale.ENGLISH) + .build(); + + return newExecutionContextBuilder() + .executionInput(executionInput) + .executionId(ExecutionId.from(this.getClass().getName())) + .build(); + } +} From a7dd518e9e0a26ec492137a4f9bf2211b2189d7f Mon Sep 17 00:00:00 2001 From: Joel Lappalainen Date: Wed, 15 Mar 2023 15:11:38 +0200 Subject: [PATCH 3/3] Update changelog --- docs/sandbox/LegacyGraphQLApi.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/sandbox/LegacyGraphQLApi.md b/docs/sandbox/LegacyGraphQLApi.md index ef46f057e58..7208a133d41 100644 --- a/docs/sandbox/LegacyGraphQLApi.md +++ b/docs/sandbox/LegacyGraphQLApi.md @@ -77,3 +77,4 @@ on this API and the [Legacy GraphQL Api](LegacyGraphQLApi.md). The new API will - Rename unpreferredRouteCost to unpreferredCost (October 2022, [#4543](https://github.com/opentripplanner/OpenTripPlanner/pull/4543)) - Make plan fetcher async (December 2022, [#4676](https://github.com/opentripplanner/OpenTripPlanner/pull/4676)) - Fix alerts query severity, effect and cause filters (February 2023, [#4909](https://github.com/opentripplanner/OpenTripPlanner/pull/4909)) +- Use accept-language header instead of the default route request locale in the plan query (March 2023, [#4971](https://github.com/opentripplanner/OpenTripPlanner/pull/4971))