Skip to content

Commit b7721b6

Browse files
committed
parameter of type com.querydsl.core.types.Predicate ignored when unique. Fixes #1522
1 parent 3612213 commit b7721b6

File tree

7 files changed

+220
-8
lines changed

7 files changed

+220
-8
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/QuerydslPredicateOperationCustomizer.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.data.util.CastUtils;
5757
import org.springframework.data.util.ClassTypeInformation;
5858
import org.springframework.data.util.TypeInformation;
59+
import org.springframework.util.CollectionUtils;
5960
import org.springframework.web.method.HandlerMethod;
6061

6162
/**
@@ -86,10 +87,6 @@ public QuerydslPredicateOperationCustomizer(QuerydslBindingsFactory querydslBind
8687

8788
@Override
8889
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
89-
if (operation.getParameters() == null) {
90-
return operation;
91-
}
92-
9390
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
9491

9592
int parametersLength = methodParameters.length;
@@ -118,7 +115,7 @@ public Operation customize(Operation operation, HandlerMethod handlerMethod) {
118115

119116
// if only listed properties should be included, remove all other fields from fieldsToAdd
120117
if (getFieldValueOfBoolean(bindings, "excludeUnlistedProperties")) {
121-
fieldsToAdd.removeIf(s -> !whiteList.contains(s) && !aliases.contains(s) );
118+
fieldsToAdd.removeIf(s -> !whiteList.contains(s) && !aliases.contains(s));
122119
}
123120

124121
for (String fieldName : fieldsToAdd) {
@@ -129,7 +126,14 @@ public Operation customize(Operation operation, HandlerMethod handlerMethod) {
129126
}
130127
}
131128
}
132-
operation.getParameters().addAll(parametersToAddToOperation);
129+
130+
if(!CollectionUtils.isEmpty(parametersToAddToOperation)){
131+
if (operation.getParameters() == null)
132+
operation.setParameters(parametersToAddToOperation);
133+
else
134+
operation.getParameters().addAll(parametersToAddToOperation);
135+
}
136+
133137
return operation;
134138
}
135139

@@ -246,7 +250,8 @@ private Type getFieldType(String fieldName, Map<String, Object> pathSpecMap, Cla
246250
Field declaredField;
247251
if (path.isPresent()) {
248252
genericType = path.get().getType();
249-
} else {
253+
}
254+
else {
250255
declaredField = root.getDeclaredField(fieldName);
251256
genericType = declaredField.getGenericType();
252257
}
@@ -275,7 +280,7 @@ private io.swagger.v3.oas.models.parameters.Parameter buildParam(Type type, Stri
275280
}
276281

277282
if (parameter.getSchema() == null) {
278-
Schema<?> schema ;
283+
Schema<?> schema;
279284
PrimitiveType primitiveType = PrimitiveType.fromType(type);
280285
if (primitiveType != null) {
281286
schema = primitiveType.createProperty();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test.org.springdoc.api.app30;
2+
3+
import com.querydsl.core.types.Predicate;
4+
5+
import org.springframework.data.querydsl.binding.QuerydslPredicate;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
10+
@RestController
11+
public class HelloController {
12+
13+
@GetMapping("/")
14+
public User testQueryDslAndSpringDoc(@QuerydslPredicate(root = User.class, bindings = UserPredicate.class) Predicate predicate) {
15+
return null;
16+
}
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package test.org.springdoc.api.app30;
2+
3+
import com.querydsl.core.types.Path;
4+
import com.querydsl.core.types.PathMetadata;
5+
import com.querydsl.core.types.dsl.EntityPathBase;
6+
import com.querydsl.core.types.dsl.NumberPath;
7+
import com.querydsl.core.types.dsl.StringPath;
8+
9+
import javax.annotation.Generated;
10+
11+
import static com.querydsl.core.types.PathMetadataFactory.forVariable;
12+
13+
14+
/**
15+
* QUser is a Querydsl query type for User
16+
*/
17+
@Generated("com.querydsl.codegen.EntitySerializer")
18+
public class QUser extends EntityPathBase<User> {
19+
20+
private static final long serialVersionUID = 222331676L;
21+
22+
public static final QUser user = new QUser("user");
23+
24+
public final StringPath email = createString("email");
25+
26+
public final StringPath firstName = createString("firstName");
27+
28+
public final NumberPath<Long> id = createNumber("id", Long.class);
29+
30+
public final StringPath lastName = createString("lastName");
31+
32+
public QUser(String variable) {
33+
super(User.class, forVariable(variable));
34+
}
35+
36+
public QUser(Path<? extends User> path) {
37+
super(path.getType(), path.getMetadata());
38+
}
39+
40+
public QUser(PathMetadata metadata) {
41+
super(User.class, metadata);
42+
}
43+
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.api.app30;
25+
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
30+
public class SpringDocApp30Test extends AbstractSpringDocTest {
31+
32+
@SpringBootApplication
33+
static class SpringDocTestApp {}
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app30;
2+
3+
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
@Entity
13+
@Getter
14+
@Setter
15+
public class User {
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
private String firstName;
20+
private String lastName;
21+
private String email;
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test.org.springdoc.api.app30;
2+
3+
4+
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
5+
import org.springframework.data.querydsl.binding.QuerydslBindings;
6+
7+
public class UserPredicate implements QuerydslBinderCustomizer<QUser> {
8+
9+
10+
@Override
11+
public void customize(QuerydslBindings bindings, QUser user) {
12+
bindings.excludeUnlistedProperties(true);
13+
bindings.including(user.email);
14+
bindings.bind(user.firstName).as("name").withDefaultBinding();
15+
}
16+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "testQueryDslAndSpringDoc",
20+
"parameters": [
21+
{
22+
"name": "name",
23+
"in": "query",
24+
"schema": {
25+
"type": "string"
26+
}
27+
},
28+
{
29+
"name": "email",
30+
"in": "query",
31+
"schema": {
32+
"type": "string"
33+
}
34+
}
35+
],
36+
"responses": {
37+
"200": {
38+
"description": "OK",
39+
"content": {
40+
"application/hal+json": {
41+
"schema": {
42+
"$ref": "#/components/schemas/User"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"components": {
52+
"schemas": {
53+
"User": {
54+
"type": "object",
55+
"properties": {
56+
"id": {
57+
"type": "integer",
58+
"format": "int64"
59+
},
60+
"firstName": {
61+
"type": "string"
62+
},
63+
"lastName": {
64+
"type": "string"
65+
},
66+
"email": {
67+
"type": "string"
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)