Skip to content

Weird behavior with Locale AZ #242

Closed

Description

@dadoonet

Hey team.

Thanks to RandomizedTesting framework, I found today this weird issue which only seems to happen when using az as the Locale.

Here is a test which reproduces this behavior:

package fr.pilato.elasticsearch.crawler.fs.settings;

import org.github.gestalt.config.Gestalt;
import org.github.gestalt.config.builder.GestaltBuilder;
import org.github.gestalt.config.source.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;

public class ConfigTest {

    private static final Locale savedLocale = Locale.getDefault();

    @After
    public void resetLocale() {
        Locale.setDefault(savedLocale);
    }

    @Test
    public void testLocaleAz() throws Exception {
        Locale.setDefault(new Locale.Builder().setLanguageTag("az").build());
        Gestalt gestalt = new GestaltBuilder()
                .addSource(MapConfigSourceBuilder.builder()
                .addCustomConfig("elasticsearch.index", "foo")
                .build())
                .build();
        gestalt.loadConfigs();
        assertThat(gestalt.getConfigOptional("elasticsearch", Elasticsearch.class)).isPresent();
    }

    @Test
    public void testLocaleFr() throws Exception {
        Locale.setDefault(new Locale.Builder().setLanguageTag("fr").build());
        Gestalt gestalt = new GestaltBuilder()
                .addSource(MapConfigSourceBuilder.builder()
                .addCustomConfig("elasticsearch.index", "foo")
                .build())
                .build();
        gestalt.loadConfigs();
        assertThat(gestalt.getConfigOptional("elasticsearch", Elasticsearch.class)).isPresent();
    }
}

The testLocaleFr passes but the testLocaleAz is failing.
I'm using the following libs:

  • Junit4
  • AssertJ

HTH

Activity

  1. self-assigned this
    on Apr 9, 2025
  2. credmond-git commented on Apr 9, 2025

    @credmond-git
    Collaborator

    Thanks for reporting this issue and including detailed unit tests.
    I added the test to Gestalt in a branch at https://github.com/gestalt-config/gestalt/blob/bug/242-locale/gestalt-bug/src/test/java/gestalt/ConfigTest.java
    That branch built and passed all tests.
    https://github.com/gestalt-config/gestalt/actions/runs/14362328750/job/40266736538

    Is there any additional steps needed to reproduce this?

  3. dadoonet commented on Apr 10, 2025

    @dadoonet
    ContributorAuthor

    Very interesting. I should have tried this in a blank empty project instead of within mine. I think there's a dependency issue here or something like this which is also loaded in my classpath causing this behavior.
    So I'm going to try to add in a blank project all the deps I have and remove one by one to see when the problem is disappearing.

    This might take me some days before I can do it though.

    I will share the full test repo when ready.

  4. dadoonet commented on Apr 11, 2025

    @dadoonet
    ContributorAuthor

    Interesting. When the Elasticsearch class is created within the ConfigTest class (inner class), then no issue.
    When it's a class from the outside, I can see the problem. But give me some time to reproduce that in an isolated project.

    Thanks

  5. dadoonet commented on Apr 11, 2025

    @dadoonet
    ContributorAuthor

    It was not about that. I managed to reproduce the issue with:

    package fr.pilato.gestalt.test;
    
    import org.github.gestalt.config.Gestalt;
    import org.github.gestalt.config.annotations.Config;
    import org.github.gestalt.config.builder.GestaltBuilder;
    import org.github.gestalt.config.source.*;
    import org.junit.After;
    import org.junit.Test;
    
    import java.util.Locale;
    import java.util.concurrent.TimeUnit;
    
    import static org.assertj.core.api.Assertions.assertThat;
    
    public class LocaleTest {
    
        private static final Locale savedLocale = Locale.getDefault();
    
        public static class TimeValue {
    
            private final long duration;
    
            private final TimeUnit timeUnit;
    
            public TimeValue(String time) {
                TimeValue timeValue = parseTimeValue(time);
                this.duration = timeValue.duration;
                this.timeUnit = timeValue.timeUnit;
            }
    
            public TimeValue(long duration, TimeUnit timeUnit) {
                this.duration = duration;
                this.timeUnit = timeUnit;
            }
    
            public static TimeValue parseTimeValue(String sValue) {
                if (sValue == null) {
                    return null;
                }
                try {
                    String lowerSValue = sValue.toLowerCase(Locale.ROOT).trim();
                    long duration = Long.parseLong(lowerSValue.substring(0, lowerSValue.length() - 1));
                    TimeUnit unit;
                    if (lowerSValue.endsWith("ms")) {
                        // Well, with ms, we need to substring 2 chars
                        duration = Long.parseLong(lowerSValue.substring(0, lowerSValue.length() - 2));
                        unit = TimeUnit.MILLISECONDS;
                    } else if (lowerSValue.endsWith("s")) {
                        unit = TimeUnit.SECONDS;
                    } else if (lowerSValue.endsWith("m")) {
                        unit = TimeUnit.MINUTES;
                    } else if (lowerSValue.endsWith("h")) {
                        unit = TimeUnit.HOURS;
                    } else if (lowerSValue.endsWith("d")) {
                        unit = TimeUnit.DAYS;
                    } else {
                        throw new IllegalArgumentException("Failed to parse timevalue [" + sValue + "]: unit is missing or unrecognized");
                    }
                    return new TimeValue(duration, unit);
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("Failed to parse timevalue [" + sValue + "].");
                }
            }
        }
    
        public static class Elasticsearch {
            @Config(defaultVal = "5s")
            private TimeValue flushInterval;
        }
    
        @After
        public void resetLocale() {
            Locale.setDefault(savedLocale);
        }
    
        @Test
        public void testLocaleAz() throws Exception {
            Locale.setDefault(new Locale.Builder().setLanguageTag("az").build());
            Gestalt gestalt = new GestaltBuilder()
                    .addSource(MapConfigSourceBuilder.builder()
                            .addCustomConfig("elasticsearch.index", "foo")
                            .build())
                    .build();
            gestalt.loadConfigs();
            assertThat(gestalt.getConfigOptional("elasticsearch", Elasticsearch.class)).isPresent();
        }
    
        @Test
        public void testLocaleFr() throws Exception {
            Locale.setDefault(new Locale.Builder().setLanguageTag("fr").build());
            Gestalt gestalt = new GestaltBuilder()
                    .addSource(MapConfigSourceBuilder.builder()
                            .addCustomConfig("elasticsearch.index", "foo")
                            .build())
                    .build();
            gestalt.loadConfigs();
            assertThat(gestalt.getConfigOptional("elasticsearch", Elasticsearch.class)).isPresent();
        }
    }

    HTH

  6. credmond-git commented on Apr 11, 2025

    @credmond-git
    Collaborator

    I found the root cause of the issue.
    Gestalt uses the following pattern to tokenize a path.
    pattern: ^((?<name>[\w .,+=\-;:"'`~!@#$%^&*()\<>]+)(?<array>\[(?<index>\d*)])?)$
    When presented with the word: 'flushinterval'
    the following doesn't find a match in the PathLexer: Matcher matcher = pathPattern.matcher(word);

    It seems to work in every other Locale, but Az?
    I need to look into why this is happening, and how to fix it.

  7. credmond-git commented on Apr 11, 2025

    @credmond-git
    Collaborator

    I also made a fix so we can support different languages Unicode characters for object field names. #244

  8. credmond-git commented on Apr 11, 2025

    @credmond-git
    Collaborator

    Please try release v0.35.7 to see if it fixes the issue.

  9. dadoonet commented on Apr 11, 2025

    @dadoonet
    ContributorAuthor

    That's great! It fixes the issue. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions