daniel-frak / dummy4j

An extensible dummy/fake data generator library for Java
https://daniel-frak.github.io/dummy4j/
Apache License 2.0
11 stars 2 forks source link

MissingLocaleException when using Dummy4j in a Spring Boot jar #38

Open little-pinecone opened 3 years ago

little-pinecone commented 3 years ago

Example application: efficient-mvp-example

After building application with the following command:

mvn clean package && java -jar -Dspring.profiles.active=dev,init target/efficient-mvp-example-0.0.1-SNAPSHOT.jar 

and making the http://localhost:8080/api/init?seed=1&how-many-books=100 request I'm getting MissingLocaleException.

The logs show: Loading definitions from: [BOOT-INF/classes/dummy4j/en/book.yml] instead of Loading definitions from: [dummy4j/en/book.yml, …]

Adding another condition specifically for Spring Boot projects in YamlFileLoader::isInAllowedPath seems to fix the problem, but there must be a better way that doesn't require hardcoding:

    private boolean isInAllowedPath(String resource, List<String> paths) {
        boolean isInAllowedPath = false;
        for (String path : paths) {
            if (resource.startsWith(path) || resource.startsWith("BOOT-INF/classes/" + path)) {
                isInAllowedPath = true;
                break;
            }
        }

        return isInAllowedPath;
    }

Useful links: Resource pattern filter for complete resource path #148

daniel-frak commented 3 years ago

Good find, there are some issues in versions 0.7.2 and earlier when it comes to using Dummy4j in jars.

I have fixed this in version 0.7.3, at least mostly. There should now be no issues when using Dummy4j in Spring Boot. Unfortunately, I had to hardcode the "BOOT-INF/classes/" prefix, as I could not figure out a better solution for the time being.

While this issue is fixed, I will leave this issue open for now, to act as a reminder that the resource loading code needs some refactoring.

As for the refactoring, an idea to pursue could be to use the ResourceStreamProvider::get method to get a list of all files in the client's classpath (and somehow try to get their prefix), but that's for another day.