arturobernalg opened a new pull request, #1049:
URL: https://github.com/apache/maven-archetype/pull/1049

   Issue #274 reports that an archetype generates a corrupted directory when a 
module's directory name contains a fileSet's directory name as a substring, 
e.g. a module named `openapi` with a nested fileSet directory `api` produces 
`open/api` instead of `openapi/api`.
   
   `DefaultFilesetArchetypeGenerator.getOutputFile()` computes each generated 
file's path with `StringUtils.replaceOnce(template, directory, "")`, a plain 
substring search for the fileSet's `directory` inside the full `template` path. 
When the enclosing module directory (part of `moduleOffset`) happens to contain 
that same text -- `openapi` contains `api` -- `replaceOnce` matches the first 
occurrence, inside `openapi`, instead of the real `api` path segment that 
follows it later in the string. That silently corrupts the output path.
   
   The same root cause also crashes outright. The "create an empty directory 
even if there's no file to process" call (ARCHETYPE-57) passes `moduleOffset` 
itself as the `template` argument. When `moduleOffset` also contains 
`directory` as a substring, `replaceOnce` removes it from `moduleOffset`, 
leaving a string shorter than `moduleOffset.length()`, and the next call, 
`templateName.substring(moduleOffset.length())`, throws 
`StringIndexOutOfBoundsException` and fails the whole generation.
   
   `directory` is only meaningful once the `moduleOffset` prefix has already 
been accounted for, so the search for it must not start before that point. The 
fix searches for `directory` starting at `moduleOffset.length()` instead of 
from the beginning of the string, which finds the real path segment 
unambiguously regardless of what `moduleOffset` itself contains, and leaves the 
non-module case (empty `moduleOffset`) unchanged.
   
   Added a `demoapi` module with a nested `api` fileSet to the `fileset-1.0` 
test archetype used by `DefaultArchetypeGeneratorTest`, reproducing the exact 
substring collision from the issue. Confirmed it fails with the reported 
`StringIndexOutOfBoundsException` on unfixed code and passes with the fix. Full 
`archetype-common` test suite, checkstyle and spotless all still pass.
   
   - [x] I hereby declare this contribution to be licensed under the Apache 
License Version 2.0, January 2004
   - [x] I hereby declare that I have filed an Apache Individual Contributor 
License Agreement.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to