Comments (5)
The minimal code which reproduces the bug:
(Issue138.java
- it should be placed in /src/test/java/...
)
import net.kaczmarzyk.E2eTestBase;
import net.kaczmarzyk.spring.data.jpa.Customer;
import net.kaczmarzyk.spring.data.jpa.CustomerRepository;
import net.kaczmarzyk.spring.data.jpa.domain.In;
import net.kaczmarzyk.spring.data.jpa.web.annotation.JoinFetch;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class Issue138 extends E2eTestBase {
@RestController
public static class TestControllerIssue138 {
@Autowired
private CustomerRepository customerRepository;
@RequestMapping(value = "/issue138/join-fetch-interface/customers", params = { "orderIn" })
@ResponseBody
public Object findByOrderIn(@JoinFetch(paths = "orders", alias = "o")
@Spec(path = "o.itemName", params = "orderIn", spec = In.class) Specification<Customer> spec, Pageable pageable) {
return customerRepository.findAll(spec, pageable);
}
}
@Test
public void resolveSpecBasedOnJoinFetchAliasForPagedRequest() throws Exception {
mockMvc.perform(get("/issue138/join-fetch-interface/customers")
.param("orderIn", "Pizza")
.param("order", "Duff Beer")
.param("page", "0")
.param("size", "1")
.param("sort", "id")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0].firstName").value("Homer"))
.andExpect(jsonPath("$[1]").doesNotExist());
}
}
from specification-arg-resolver.
@yarikwest Thanks for reporting. After short analysis I understand the issue and prepared a working PoC of a fix. But it needs more testing and refactoring.
In a nuthshell:
- JoinFetch had an explicit skip of joinin in count queries -- this was done for 2 reasons: 1) fetch is very often used for just initiualizing lazy collections, not filtering so it would be superfluous for count query 2) hibernate seems to forbid join fetch in count query (i.e. when root entity is not included in the result list)
- I added a quick hackish fragment that in case of a count query converts join fetch to a join...
- ... and the test posted by @jradlica seems to be working fine -- see related PR
I think this is the way to go, but:
- More tests must be added (e.g. for multi-level joins)
- JoinFetch to Join conversion must be skipped if there is no actual filtering on the joined entity (in such a case it would be superfluous in a count query)
- JoinFetch allows multiple paths in a single annoatation, in the PoC I used just the first one
So it requires more work. You can compiling the version from the branch and see if it solves your issue in the meantime
from specification-arg-resolver.
(Marking as good first issue because sb could at least write more tests e.g. with nested joins (i.e. join fetching on a joined entity). The actual implementation requires more deep knowledge of the internals)
from specification-arg-resolver.
(uploaded as 2.9.1-SNAPSHOT to my private Maven repo for easier evaluation, see bottom of the README for the location details)
from specification-arg-resolver.
@yarikwest fix included in v2.10.0 (just released)
from specification-arg-resolver.
Related Issues (20)
- Required field HOT 2
- Specification arguments set HOT 1
- Java 17 incompatibility HOT 2
- Can you add SPeL in @Spec constVal with value from headers? HOT 2
- Issue with Join on Hierarchical Entities HOT 1
- Missing support for Spring Boot 3 and transition from javax to jakarta HOT 3
- Redundant proxy is created for multi-spec specifications when expected type is not a spec-interface HOT 3
- Path variables from `@RequestMapping` annotation above the class are not included in springdoc api documentation HOT 1
- Additional specifications HOT 1
- Support for auto-configuration
- Is it possible to reference the indices of the database? HOT 5
- Fail search with an error on unknown search parameters
- Add "description" attribute in the "@Spec" annotation HOT 2
- Json body params should not impose restrictions on requests HOT 3
- Interface extending Specification referenced from a method is not visible from class loader HOT 1
- Specification adds duplicate join when query has been built manually with existing join (Spring Boot 3.x)
- No params in Swagger UI generated by Springdoc-OpenAPI 1.6.13
- How to use with Dto and without entities in presentation layer (Controller)? HOT 2
- Query param with type Integer is not compatible when used with Blaze-Persistence
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specification-arg-resolver.