You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/08/23 16:04:24 UTC

(grails-core) 01/01: Replace deprecated DataFetchingEnvironment#getContext() in getLocale()

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch fix/graphql-errors-response
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 3c7d99ee829ce07de059ed0af805f5dd641118c0
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Aug 23 00:04:17 2026 -0500

    Replace deprecated DataFetchingEnvironment#getContext() in getLocale()
    
    getContext() has been deprecated in graphql-java since 2021-07-05 in
    favor of getGraphQlContext(), but the latter only accepts Map entries
    and can't carry an arbitrary object such as a custom
    GraphQLContextBuilder result implementing LocaleAwareContext.
    GraphqlController already sets both .context(context) and
    .root(context) to the same value specifically so callers can move off
    the legacy context; switch getLocale() to read environment.root
    instead, which covers both the Map-of-locale and LocaleAwareContext
    cases without changing behavior.
    
    Updated the existing LocaleAwareContext spec to stub root instead of
    context, and added coverage for the root-Map-with-locale and
    no-locale-entry-falls-back-to-default branches, which weren't
    separately tested before.
    
    Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
---
 .../DefaultGraphQLErrorsResponseHandler.groovy     | 14 +++++++----
 .../DefaultGraphQLErrorsResponseHandlerSpec.groovy | 27 +++++++++++++++++++++-
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandler.groovy b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandler.groovy
index 50718356af..111dc1f6f9 100644
--- a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandler.groovy
+++ b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandler.groovy
@@ -66,17 +66,21 @@ class DefaultGraphQLErrorsResponseHandler implements GraphQLErrorsResponseHandle
     }
 
     protected Locale getLocale(DataFetchingEnvironment environment) {
-        if (environment.context instanceof Map) {
-            Map context = (Map)environment.context
+        // environment.context is deprecated in favor of environment.graphQlContext, but the latter
+        // can only carry Map entries - it cannot hold an arbitrary GraphQLContextBuilder result such
+        // as a LocaleAwareContext. GraphqlController sets both context and root to the same object
+        // for this reason, so environment.root is used here instead as the non-deprecated equivalent.
+        if (environment.root instanceof Map) {
+            Map context = (Map) environment.root
             if (context.containsKey('locale')) {
                 Object localContext = context.get('locale')
                 if (localContext instanceof Locale) {
-                    return (Locale)localContext
+                    return (Locale) localContext
                 }
             }
         }
-        if (environment.context instanceof LocaleAwareContext) {
-            return ((LocaleAwareContext) environment.context).locale
+        if (environment.root instanceof LocaleAwareContext) {
+            return ((LocaleAwareContext) environment.root).locale
         }
         Locale.default
     }
diff --git a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandlerSpec.groovy b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandlerSpec.groovy
index 764306639e..27989ef09d 100644
--- a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandlerSpec.groovy
+++ b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/response/errors/DefaultGraphQLErrorsResponseHandlerSpec.groovy
@@ -117,7 +117,7 @@ class DefaultGraphQLErrorsResponseHandlerSpec extends Specification implements G
         1 * messageSource.getMessage(fieldError, contextLocale) >> 'bonjour'
         DataFetchingEnvironment mockFieldEnv = new MockDataFetchingEnvironment(
                 source: fieldError,
-                context: new LocaleAwareContext() {
+                root: new LocaleAwareContext() {
                     @Override
                     Locale getLocale() {
                         contextLocale
@@ -128,6 +128,31 @@ class DefaultGraphQLErrorsResponseHandlerSpec extends Specification implements G
         handler.messageFetcher.get(mockFieldEnv) == 'bonjour'
     }
 
+    void "test getLocale uses the locale from a root Map"() {
+        given:
+        Locale mapLocale = Locale.CANADA_FRENCH
+        FieldError fieldError = Mock(FieldError)
+        1 * messageSource.getMessage(fieldError, mapLocale) >> 'bonjour'
+        DataFetchingEnvironment mockFieldEnv = new MockDataFetchingEnvironment(
+                source: fieldError,
+                root: [locale: mapLocale])
+
+        expect:
+        handler.messageFetcher.get(mockFieldEnv) == 'bonjour'
+    }
+
+    void "test getLocale falls back to the default locale when the root Map has no locale entry"() {
+        given:
+        FieldError fieldError = Mock(FieldError)
+        1 * messageSource.getMessage(fieldError, Locale.default) >> 'hello'
+        DataFetchingEnvironment mockFieldEnv = new MockDataFetchingEnvironment(
+                source: fieldError,
+                root: [:])
+
+        expect:
+        handler.messageFetcher.get(mockFieldEnv) == 'hello'
+    }
+
     class MockValidateable implements GormValidateable {
         String foo
     }