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

NoClassDefFoundError when sort query is applied #163

Open
lewimuchiri opened this issue Oct 24, 2024 · 2 comments
Open

NoClassDefFoundError when sort query is applied #163

lewimuchiri opened this issue Oct 24, 2024 · 2 comments

Comments

@lewimuchiri
Copy link

lewimuchiri commented Oct 24, 2024

I am getting the following error when I add a sort query:
java.lang.NoClassDefFoundError: Could not initialize class io.github.perplexhub.rsql.jsonb.JsonbSupport. If I remove the sort query, the filter works.

Environment:

Spring Boot: 3.3.4
Java: 21

Dependencies:

 dependencoes {
        implementation("io.github.perplexhub:rsql-jpa-spring-boot-starter:6.0.23")
 }

My Model:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "event")
public class Event {
    ...
    @Column(nullable = false, unique = true, length = 50)
    private String code;
    ...
}

The code:

    public Response<List<Event>> queryEvent(String filter, String sort, Integer first, Integer after) {
        Specification<Event> specification = toSpecification(filter, sort);

        Page<Event> eventPage = eventRepository.findAll(specification, toPageable(first, after));

        return Response.<List<Event>>builder()
                .data(eventPage .getContent())
                .pageInfo(PageInfo.builder()
                        .hasNextPage(eventPage .hasNext())
                        .hasPreviousPage(eventPage .hasPrevious())
                        .startCursor(eventPage .getNumberOfElements() > 0
                                ? eventPage .getContent().getFirst().getId()
                                : null)
                        .endCursor(eventPage .getNumberOfElements() > 0
                                ? eventPage .getContent().getLast().getId()
                                : null)
                        .totalRecords(eventPage.getTotalElements())
                        .build())
                .build();
    }


   public static <T> Specification<T> toSpecification(String rsqlQuery, String sortRsql) {
        Specification<T> specification;

        if (StringUtils.hasText(rsqlQuery)) {
            specification = RSQLJPASupport.toSpecification(rsqlQuery);

            if (StringUtils.hasText(sortRsql)) {
                specification = specification.and(RSQLJPASupport.toSort(sortRsql));
            }
        } else if (StringUtils.hasText(sortRsql)) {
            specification = RSQLJPASupport.toSort(sortRsql);
        } else {
            specification = null;
        }
        return specification;
    }

    public static Pageable toPageable(Integer first, Integer after) {
        // Default to page 0 for page and 50 for pageSize in case the inputs have problems
        return PageRequest.of(
                after == null ? 0 : after,
                first == null ? 50 : first
        );
    }

This is the way I am calling the query method: queryEvent("", "version,asc", null, null);

@ng-galien
Copy link
Contributor

Hi, is it possible to provide the complete stack trace, or even better, an example of the bug in a test?

@lewimuchiri
Copy link
Author

lewimuchiri commented Nov 8, 2024

Actually the exception is thrown even with the filter added.

Unit test will not reveal the error because I am having to mock the eventRepository.findAll() method, which is where the exception is being thrown. When I do an integration test however, I get the error. Minimal reproducible example is the code I shared earlier.

I suspect it has something to do with Java version or Spring Boot version but I am not sure. Interestingly it doesn't give any stack-trace at all except this exception (with the filter added):
java.lang.NoClassDefFoundError: Could not initialize class io.github.perplexhub.rsql.RSQLOperators'

The exception is being thrown in RSQLJPASupport.java, in a method called toSpecification(). In particular it is being thrown on this line:

Set<ComparisonOperator> supportedOperators = RSQLOperators.supportedOperators();

This exception is thrown when you add the sort query (without the filter):
java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class io.github.perplexhub.rsql.RSQLOperators.

This is thrown in the RSQLJPAPredicateConverter.java class within the findPropertyPathInternal() method. In particular, on this line:

else if (JsonbSupport.isJsonType(mappedProperty, classMetadata)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants