Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: Pattern converter error when Pattern used as key in Map to Nested Group #44805

Closed
rudiger3d opened this issue Nov 28, 2024 · 5 comments
Closed
Labels
area/config kind/enhancement New feature or request

Comments

@rudiger3d
Copy link

Describe the bug

If I have the following code:

@ConfigMapping(prefix = "provider.tryg")
public interface TrygConfiguration {
    Map<Pattern, String> infoMapping();
}

and application.properties

provider.tryg.info-mapping.Campervan\ \\d=CAMPERVAN

and change the infoMapping() Map to

@ConfigMapping(prefix = "provider.tryg")
public interface TrygConfiguration {
    Map<Pattern, NameLimit> infoMapping();

    interface NameLimit {
        String name();
        int limit();
    }
}
provider.tryg.info-mapping.Campervan\ \\d.name=CAMPERVAN
provider.tryg.info-mapping.Campervan\ \\d.limit=7000

Expected behavior

Then if I iterate the TrygConfiguration.infoMapping() entrySet(), i.e.

    Map.Entry<Pattern, ...>
    entry.getKey().matcher("Campervan 6").matches();

I'd expect entry.getKey().matcher("Campervan 6").matches() to return true in both cases.

Actual behavior

However in the second case, it returns false, because the internal pattern of the converted java.util.regex.Pattern is "Campervan d" and not "Campervan \\d".

I know it is somewhat weird to use Pattern as a key in a Map, as you may only retrieve it via iteration, but it made the corresponding configuration in the "application.properties" easier to write so that's why :)

How to Reproduce?

No response

Output of uname -a or ver

Darwin CO-MBP-C02YL4N9LVCF-DK 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64 x86_64

Output of java -version

openjdk version "21.0.5" 2024-10-15 LTS OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode)

Quarkus version or git rev

3.17.0

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@rudiger3d rudiger3d added the kind/bug Something isn't working label Nov 28, 2024
Copy link

quarkus-bot bot commented Nov 28, 2024

/cc @radcortez (config)

@radcortez
Copy link
Member

radcortez commented Nov 28, 2024

Our own key parser uses \ as an escape sequence, meaning that if you want to have a key with a Pattern with a proper escape sequence, you need to use Campervan\ \\\\d.

@radcortez radcortez added kind/enhancement New feature or request and removed kind/bug Something isn't working labels Nov 28, 2024
@rudiger3d
Copy link
Author

Our own key parser uses \ as an escape sequence, meaning that if you want to have a key with a Pattern with a proper escape sequence, you need to use `Campervan\ \d.

I'm not totally sure I follow: In both cases above I have used \ as an escape sequence for the key:

provider.tryg.info-mapping.Campervan\ \\d=CAMPERVAN

and

provider.tryg.info-mapping.Campervan\ \\d.name=CAMPERVAN.

The first case, the Pattern is parsed correctly, but second where the value of the map is a nested group, it does not.

I can provide a reproducer, but if this is only supported by accident then I guess it won't really matter...

@rudiger3d
Copy link
Author

rudiger3d commented Nov 29, 2024

Now I understand. An extra \ (java-style) works:

provider.tryg.info-mapping.Campervan\ \\\\d.name=CAMPERVAN.

Sorry for the noise. Btw, quarkus-config/smallrye config is great, and we use it extensively in our services.

@radcortez
Copy link
Member

Yes, I'm sorry. I noticed that my comment was misleading. I did type \\\\d, but I missed the final code block end element, and it got displayed as \\d only.

Thank you very much for your feedback. Feel free to provide ideas or improvements :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/config kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants