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

Fix Incorrect Pagination Links in Microservices When Forwarded Through Gateway #27432

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions generators/spring-boot/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
*/
import { SERVER_MAIN_RES_DIR, SERVER_MAIN_SRC_DIR, SERVER_TEST_RES_DIR, SERVER_TEST_SRC_DIR } from '../generator-constants.js';
import { addSectionsCondition, mergeSections } from '../base/support/index.js';
import {
javaMainPackageTemplatesBlock,
moveToJavaPackageSrcDir,
moveToJavaPackageTestDir,
moveToSrcMainResourcesDir,
} from '../java/support/index.js';
import { moveToJavaPackageSrcDir, moveToJavaPackageTestDir, moveToSrcMainResourcesDir } from '../java/support/index.js';

const imperativeConfigFiles = {
imperativeFiles: [
Expand Down Expand Up @@ -352,10 +347,6 @@ export const baseServerFiles = {
},
],
serverMicroservice: [
javaMainPackageTemplatesBlock({
condition: generator => generator.applicationTypeMicroservice,
templates: ['config/SpringDocConfiguration.java'],
}),
{
condition: generator => generator.applicationTypeMicroservice,
path: SERVER_MAIN_RES_DIR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _%>
return ResponseEntity.ok().body(entityList);
<%_ } else { _%>
Page<<%= instanceType %>> page = <%= entityInstance %>QueryService.findByCriteria(criteria, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())<% } %>, page);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromUri(request.getURI())<% } %>, page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
<%_ } _%>
<%_ } else { _%>
Expand All @@ -47,7 +47,7 @@ _%>
ResponseEntity.ok()
.headers(
PaginationUtil.generatePaginationHttpHeaders(
ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()),
UriComponentsBuilder.fromUri(request.getURI()),
new PageImpl<>(countWithEntities.getT2(), pageable, countWithEntities.getT1())
)
).body(countWithEntities.getT2())
Expand Down Expand Up @@ -104,7 +104,7 @@ _%>
ResponseEntity.ok()
.headers(
PaginationUtil.generatePaginationHttpHeaders(
ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()),
UriComponentsBuilder.fromUri(request.getURI()),
new PageImpl<>(countWithEntities.getT2(), pageable, countWithEntities.getT1())
)
).body(countWithEntities.getT2())
Expand All @@ -117,10 +117,10 @@ _%>
} else {
page = <%= entityInstance %><%= viaService ? 'Service' : 'Repository' %>.findAll(pageable)<%= reactiveEntityToDto %>;
}
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>UriComponentsBuilder.fromUri(request.getURI())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
<%_ } else { _%>
Page<<%= instanceType %>> page = <%= entityInstance %><%= viaService ? 'Service' : 'Repository' %>.findAll(pageable)<%= reactiveEntityToDto %>;
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>UriComponentsBuilder.fromUri(request.getURI())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
<%_ } _%>
return ResponseEntity.ok().headers(headers).body(page.getContent());
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (paginationNo) { %>
'Service.' + (searchEngineElasticsearch ? 'searchCount' : 'countAll') :
(searchEngineElasticsearch ? 'Search' : '') + 'Repository.count' %>()
.map(total -> new PageImpl<>(new ArrayList<>(), pageable, total))
.map(page -> PaginationUtil.generatePaginationHttpHeaders(ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()), page))
.map(page -> PaginationUtil.generatePaginationHttpHeaders(UriComponentsBuilder.fromUri(request.getURI()), page))
.map(headers -> ResponseEntity.ok().headers(headers).body(<%= entityInstance %><%= viaService ? 'Service' : (searchEngineElasticsearch ? 'Search' : '') + 'Repository' %>.search(query, pageable)<% if (!viaService && dtoMapstruct) { %>.map(<%= entityToDtoReference %>)<% } %>));
<%_ } else { _%>
try {
Expand All @@ -43,7 +43,7 @@ if (paginationNo) { %>
<%_ } else { _%>
Page<<%= persistClass %>> page = <%= entityInstance %><% if (searchEngineElasticsearch) { %>Search<% } %>Repository.search(query, pageable);
<%_ } _%>
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>UriComponentsBuilder.fromUri(request.getURI())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
return ResponseEntity.ok().headers(headers).body(<% if (!viaService && dtoMapstruct) { %><%= entityListToDtoListReference %>(<% } %>page.getContent()<% if (!viaService && dtoMapstruct) { %>)<% } %>);
} catch (RuntimeException e) {
<% if (searchEngineElasticsearch) { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.ForwardedHeaderUtils;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ } else {_%>
Expand Down Expand Up @@ -133,7 +133,7 @@ public class PublicUserResource {

return userService.countManagedUsers()
.map(total -> new PageImpl<>(new ArrayList<>(), pageable, total))
.map(page -> PaginationUtil.generatePaginationHttpHeaders(ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()), page))
.map(page -> PaginationUtil.generatePaginationHttpHeaders(UriComponentsBuilder.fromUri(request.getURI()), page))
.map(headers -> ResponseEntity.ok().headers(headers).body(userService.getAllPublicUsers(pageable)));
}
<%_ } else { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.ForwardedHeaderUtils;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ } else { _%>
Expand Down Expand Up @@ -288,7 +288,7 @@ public class UserResource {
.map(total -> new PageImpl<>(new ArrayList<>(), pageable, total))
.map(page ->
PaginationUtil.generatePaginationHttpHeaders(
ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()),
UriComponentsBuilder.fromUri(request.getURI()),
page
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
<%_ } _%>
<%_ if (reactive) { _%>
import org.springframework.web.util.ForwardedHeaderUtils;
import org.springframework.web.util.UriComponentsBuilder;
<%_ } else { _%>
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
<%_ } _%>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ spring:

server:
port: <%= serverPort %>
<%_ if (applicationTypeMonolith || applicationTypeGateway) { _%>
# make sure requests the proxy uri instead of the server one
forward-headers-strategy: native
<%_ } _%>

# ===================================================================
# JHipster specific properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ spring:
console-available: true

server:
<%_ if (applicationTypeMicroservice) { _%>
forward-headers-strategy: FRAMEWORK
<%_ } _%>
servlet:
session:
cookie:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,6 @@ exports[`generator - cassandra microservice-jwt-reactive(false)-maven-enableTran
"src/main/java/tech/jhipster/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -885,9 +882,6 @@ exports[`generator - cassandra microservice-jwt-reactive(true)-gradle-enableTran
"src/main/java/com/mycompany/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1232,9 +1226,6 @@ exports[`generator - cassandra microservice-oauth2-reactive(true)-gradle-enableT
"src/main/java/com/mycompany/config/SecurityConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,6 @@ exports[`generator - couchbase microservice-jwt-reactive(false)-maven-enableTran
"src/main/java/tech/jhipster/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -921,9 +918,6 @@ exports[`generator - couchbase microservice-jwt-reactive(true)-gradle-enableTran
"src/main/java/com/mycompany/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1268,9 +1262,6 @@ exports[`generator - couchbase microservice-oauth2-reactive(true)-gradle-enableT
"src/main/java/com/mycompany/config/SecurityConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,6 @@ exports[`generator - elasticsearch microservice-jwt-reactive(false)-maven-enable
"src/main/java/tech/jhipster/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -993,9 +990,6 @@ exports[`generator - elasticsearch microservice-jwt-reactive(true)-gradle-enable
"src/main/java/com/mycompany/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1376,9 +1370,6 @@ exports[`generator - elasticsearch microservice-oauth2-reactive(true)-gradle-ena
"src/main/java/com/mycompany/config/SecurityConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,6 @@ exports[`generator - mongodb microservice-jwt-reactive(false)-maven-enableTransl
"src/main/java/tech/jhipster/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -879,9 +876,6 @@ exports[`generator - mongodb microservice-jwt-reactive(true)-gradle-enableTransl
"src/main/java/com/mycompany/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1211,9 +1205,6 @@ exports[`generator - mongodb microservice-oauth2-reactive(true)-gradle-enableTra
"src/main/java/com/mycompany/config/SecurityConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,6 @@ exports[`generator - neo4j microservice-jwt-reactive(false)-maven-enableTranslat
"src/main/java/tech/jhipster/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/tech/jhipster/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -882,9 +879,6 @@ exports[`generator - neo4j microservice-jwt-reactive(true)-gradle-enableTranslat
"src/main/java/com/mycompany/config/SecurityJwtConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1214,9 +1208,6 @@ exports[`generator - neo4j microservice-oauth2-reactive(true)-gradle-enableTrans
"src/main/java/com/mycompany/config/SecurityConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/SpringDocConfiguration.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/config/WebConfigurer.java": {
"stateCleared": "modified",
},
Expand Down
Loading
Loading