Skip to content

Commit c29f36b

Browse files
committed
When extends JpaRepository, using @parameter over the method results in duplicate of the same parameter. Fixes #2038
1 parent fcfc66e commit c29f36b

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/core/DataRestOperationService.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.commons.lang3.reflect.FieldUtils;
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
38+
import org.springdoc.core.GenericParameterService;
3839
import org.springdoc.core.MethodAttributes;
3940
import org.springdoc.core.OperationService;
4041
import org.springdoc.core.SpringDocAnnotationsUtils;
@@ -205,12 +206,17 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
205206
String pName = parameterMetadatum.getName();
206207
ResourceDescription description = parameterMetadatum.getDescription();
207208
if (description instanceof TypedResourceDescription) {
208-
Type type = getParameterType(pName,method,description);
209+
Type type = getParameterType(pName, method, description);
209210
Schema<?> schema = SpringDocAnnotationsUtils.extractSchema(openAPI.getComponents(), type, null, null);
210211
Parameter parameter = getParameterFromAnnotations(openAPI, methodAttributes, method, pName);
211-
if (parameter == null)
212+
if (parameter == null) {
212213
parameter = new Parameter().name(pName).in(ParameterIn.QUERY.toString()).schema(schema);
213-
operation.addParametersItem(parameter);
214+
operation.addParametersItem(parameter);
215+
}
216+
else if (CollectionUtils.isEmpty(operation.getParameters()))
217+
operation.addParametersItem(parameter);
218+
else
219+
GenericParameterService.mergeParameter(operation.getParameters(), parameter);
214220
}
215221
}
216222

@@ -238,7 +244,7 @@ private Type getParameterType(String pName, Method method, ResourceDescription d
238244
java.lang.reflect.Parameter[] parameters = method.getParameters();
239245
for (int i = 0; i < parameters.length; i++) {
240246
java.lang.reflect.Parameter parameter = parameters[i];
241-
if (pName.equals(parameter.getName()) || (parameter.getAnnotation(Param.class)!=null && pName.equals(parameter.getAnnotation(Param.class).value()))) {
247+
if (pName.equals(parameter.getName()) || (parameter.getAnnotation(Param.class) != null && pName.equals(parameter.getAnnotation(Param.class).value()))) {
242248
ResolvableType resolvableType = ResolvableType.forMethodParameter(method, i);
243249
type = resolvableType.getType();
244250
break;

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app37/ProductRepository.java

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import ch.qos.logback.core.rolling.helper.DateTokenConverter;
1111
import io.swagger.v3.oas.annotations.Parameter;
12+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
1213

1314
import org.springframework.data.jpa.repository.JpaRepository;
1415
import org.springframework.data.repository.query.Param;
@@ -23,6 +24,15 @@
2324
@RepositoryRestResource(path = "product")
2425
public interface ProductRepository extends JpaRepository<ProductEntity, Long> {
2526

27+
28+
List<ProductEntity> findByPrice( @Parameter(
29+
name = "price",
30+
description = "test desc",
31+
in = ParameterIn.QUERY,
32+
required = true
33+
)
34+
@Param("price")String price);
35+
2636
/**
2737
* 根据商品名称查询商品信息
2838
*

springdoc-openapi-data-rest/src/test/resources/results/app37.json

+35-1
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,46 @@
326326
}
327327
}
328328
},
329-
"/product/search/findTopByNameOrderByDateDesc": {
329+
"/product/search/findByPrice": {
330330
"get": {
331331
"tags": [
332332
"product-entity-search-controller"
333333
],
334334
"operationId": "executeSearch-productentity-get_5",
335+
"parameters": [
336+
{
337+
"name": "price",
338+
"in": "query",
339+
"description": "test desc",
340+
"required": true,
341+
"schema": {
342+
"type": "string"
343+
}
344+
}
345+
],
346+
"responses": {
347+
"200": {
348+
"description": "OK",
349+
"content": {
350+
"application/hal+json": {
351+
"schema": {
352+
"$ref": "#/components/schemas/CollectionModelEntityModelProductEntity"
353+
}
354+
}
355+
}
356+
},
357+
"404": {
358+
"description": "Not Found"
359+
}
360+
}
361+
}
362+
},
363+
"/product/search/findTopByNameOrderByDateDesc": {
364+
"get": {
365+
"tags": [
366+
"product-entity-search-controller"
367+
],
368+
"operationId": "executeSearch-productentity-get_6",
335369
"parameters": [
336370
{
337371
"name": "name2",

0 commit comments

Comments
 (0)