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

StringIndexOutOfBoundsException in panache when using project and a linebreak before FROM #29838

Closed
Felk opened this issue Dec 13, 2022 · 4 comments · Fixed by #29851
Closed
Labels
area/panache kind/bug Something isn't working
Milestone

Comments

@Felk
Copy link
Contributor

Felk commented Dec 13, 2022

When executing a Panache query that (a) contains a projection and (b) has a line break before the FROM clause, a StringIndexOutOfBoundsException is thrown:

java.lang.StringIndexOutOfBoundsException: Range [7, -1) out of bounds for length 30
        [...]
        at java.base/java.lang.String.substring(String.java:2715)
        at io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl.project(CommonPanacheQueryImpl.java:98)
        at io.quarkus.hibernate.orm.panache.runtime.PanacheQueryImpl.project(PanacheQueryImpl.java:33)
        at org.acme.MyEntityRepo.listEverything(MyEntityRepo.java:19)
        [...]
        at org.acme.MyEntityRepoTest_Subclass.testSelect(Unknown Source)
        [...]

I attached a reproducer containing this panache repository:

@ApplicationScoped
@Transactional
public class MyEntityRepo implements PanacheRepository<MyEntity> {
    public List<SomeProjection> listEverything() {
        return find("""
                SELECT e.field
                FROM MyEntity e
                """)
                .project(SomeProjection.class)
                .list();
    }

    @Value
    @RegisterForReflection
    static class SomeProjection {
        String field;
    }
}

quarkus-reproducer-panache-from-linebreak.zip

The issue seems to be in CommonPanacheQueryImpl#L95:

        if (lowerCasedTrimmedQuery.startsWith("select ")) {
            int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
            String trimmedQuery = query.trim();
            // 7 is the length of "select "
            String selectClause = trimmedQuery.substring(7, endSelect).trim();

Where endSelect is -1 because the from is not necessarily surrounded by spaces in all cases, such as in the reproducer.

@Felk Felk added the kind/bug Something isn't working label Dec 13, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Dec 13, 2022

/cc @FroMage, @loicmathieu

@geoand
Copy link
Contributor

geoand commented Dec 14, 2022

@loicmathieu can you think of any reason why #29851 would not work?

I can't say I know much about Panache, but this simple fix seems to make this case work properly

@loicmathieu
Copy link
Contributor

@geoand yes, it should works

@geoand
Copy link
Contributor

geoand commented Dec 14, 2022

Okay, taking it out of draft then

geoand added a commit to geoand/quarkus that referenced this issue Dec 14, 2022
geoand added a commit to geoand/quarkus that referenced this issue Dec 14, 2022
geoand added a commit that referenced this issue Dec 14, 2022
Ensure that new line chars don't break Panache projection
@quarkus-bot quarkus-bot bot added this to the 2.16 - main milestone Dec 14, 2022
@gsmet gsmet modified the milestones: 2.16 - main, 2.15.1.Final Dec 20, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue Dec 20, 2022
@gsmet gsmet modified the milestones: 2.15.1.Final, 2.13.7.Final Jan 9, 2023
gsmet pushed a commit to gsmet/quarkus that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/panache kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants