Skip to content

Commit

Permalink
Fixing the Issue of Binding Collection Parameters in JAX-RS
Browse files Browse the repository at this point in the history
  • Loading branch information
shalousun committed Feb 14, 2024
1 parent 0b431e9 commit c72e6aa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/handler/JaxrsPathHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public JaxrsPathMapping handle(ProjectDocConfigBuilder projectBuilder,
for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getFullyQualifiedName();
// method level annotation will override class level annotation
if (annotationName.equals(JakartaJaxrsAnnotations.JAX_CONSUMES)
if (annotationName.equals(JakartaJaxrsAnnotations.JAX_CONSUMES_FULLY)
|| annotationName.equals(JAXRSAnnotations.JAX_CONSUMES_FULLY)) {
Object value = annotation.getNamedParameter("value");
if (Objects.nonNull(value)) {
Expand Down
38 changes: 24 additions & 14 deletions src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ default List<ApiMethodDoc> buildEntryPointMethod(
IHeaderHandler headerHandler) {
String clazName = cls.getCanonicalName();
boolean paramsDataToTree = projectBuilder.getApiConfig().isParamsDataToTree();
ClassLoader classLoader = projectBuilder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = projectBuilder.getApiConfig().getClassLoader();
String group = JavaClassUtil.getClassTagsValue(cls, DocTags.GROUP, Boolean.TRUE);
List<JavaAnnotation> classAnnotations = this.getClassAnnotations(cls, frameworkAnnotations);
String baseUrl = "";
Expand Down Expand Up @@ -499,7 +499,7 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
List<ApiReqParam> configApiReqParams, FrameworkAnnotations frameworkAnnotations) {
JavaMethod javaMethod = docJavaMethod.getJavaMethod();
boolean isStrict = builder.getApiConfig().isStrict();
ClassLoader classLoader = builder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = builder.getApiConfig().getClassLoader();
String className = javaMethod.getDeclaringClass().getCanonicalName();
Map<String, String> paramTagMap = docJavaMethod.getParamTagMap();
Map<String, String> paramsComments = docJavaMethod.getParamsComments();
Expand Down Expand Up @@ -539,7 +539,9 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
AtomicInteger pathSize = new AtomicInteger(1);
return ApiMethodReqParam.builder()
.setPathParams(new ArrayList<>(pathReqParamMap.values().stream()
.map(p -> ApiReqParam.convertToApiParam(p).setPathParam(true).setId(pathSize.getAndIncrement()))
.map(p -> ApiReqParam.convertToApiParam(p)
.setPathParam(true)
.setId(pathSize.getAndIncrement()))
.collect(Collectors.toList())))
.setQueryParams(paramList)
.setRequestParams(new ArrayList<>(0));
Expand Down Expand Up @@ -610,7 +612,7 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
// throw new RuntimeException("You have use @RequestBody Passing multiple variables for method "
// + javaMethod.getName() + " in " + className + ",@RequestBody annotation could only bind one variables.");
// }
mockValue = JsonBuildHelper.buildJson(genericFullyQualifiedName, typeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses, builder);
mockValue = JsonBuildHelper.buildJson(fullyQualifiedName, typeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses, builder);
requestBodyCounter++;
isRequestBody = true;
}
Expand All @@ -622,9 +624,13 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
}
// file upload
if (JavaClassValidateUtil.isFile(typeName)) {
ApiParam param = ApiParam.of().setField(paramName).setType(DocGlobalConstants.PARAM_TYPE_FILE)
.setId(paramList.size() + 1).setQueryParam(true)
.setRequired(required).setVersion(DocGlobalConstants.DEFAULT_VERSION)
ApiParam param = ApiParam.of()
.setField(paramName)
.setType(DocGlobalConstants.PARAM_TYPE_FILE)
.setId(paramList.size() + 1)
.setQueryParam(true)
.setRequired(required)
.setVersion(DocGlobalConstants.DEFAULT_VERSION)
.setDesc(comment.toString());
if (typeName.contains("[]") || typeName.endsWith(">")) {
comment.append("(array of file)");
Expand All @@ -651,14 +657,17 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
JavaClass gicJavaClass = builder.getJavaProjectBuilder().getClassByName(gicName);
if (gicJavaClass.isEnum()) {
Object value = JavaClassUtil.getEnumValue(gicJavaClass, Boolean.TRUE);
ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of enum]")
ApiParam param = ApiParam.of()
.setField(paramName)
.setDesc(comment + ",[array of enum]")
.setRequired(required)
.setPathParam(isPathVariable)
.setQueryParam(queryParam)
.setId(paramList.size() + 1)
.setEnumValues(JavaClassUtil.getEnumValues(gicJavaClass))
.setEnumInfo(JavaClassUtil.getEnumInfo(gicJavaClass, builder))
.setType("array").setValue(String.valueOf(value));
.setType("array")
.setValue(String.valueOf(value));
paramList.add(param);
if (requestBodyCounter > 0) {
Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName);
Expand All @@ -683,7 +692,8 @@ default ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
}
} else if (JavaClassValidateUtil.isFile(gicName)) {
// file upload
ApiParam param = ApiParam.of().setField(paramName)
ApiParam param = ApiParam.of()
.setField(paramName)
.setType(DocGlobalConstants.PARAM_TYPE_FILE)
.setId(paramList.size() + 1)
.setQueryParam(true)
Expand Down Expand Up @@ -788,7 +798,7 @@ default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
JavaMethod method = javaMethod.getJavaMethod();
Map<String, String> pathParamsMap = new LinkedHashMap<>();
Map<String, String> queryParamsMap = new LinkedHashMap<>();
ClassLoader classLoader = configBuilder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = configBuilder.getApiConfig().getClassLoader();
apiMethodDoc.getPathParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam())
.forEach(param -> pathParamsMap.put(param.getSourceField(), param.getValue()));
apiMethodDoc.getQueryParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam())
Expand Down Expand Up @@ -880,7 +890,7 @@ default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
}
boolean isArrayOrCollection = false;
if (JavaClassValidateUtil.isArray(genericFullyQualifiedName) || JavaClassValidateUtil.isCollection(fullyQualifiedName)) {
if (JavaClassValidateUtil.isArray(fullyQualifiedName) || JavaClassValidateUtil.isCollection(fullyQualifiedName)) {
simpleTypeName = globGicName[0];
isArrayOrCollection = true;
}
Expand All @@ -896,7 +906,7 @@ default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
}
requestExample.setJsonBody(mockValue).setJson(true);
} else {
String json = JsonBuildHelper.buildJson(genericFullyQualifiedName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses,
String json = JsonBuildHelper.buildJson(fullyQualifiedName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses,
configBuilder);
requestExample.setJsonBody(JsonUtil.toPrettyFormat(json)).setJson(true);
}
Expand Down Expand Up @@ -941,7 +951,7 @@ default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
FormData formData = new FormData();
formData.setKey(paramName);
formData.setType("file");
if (genericFullyQualifiedName.contains("[]") || genericFullyQualifiedName.endsWith(">")) {
if (fullyQualifiedName.contains("[]") || fullyQualifiedName.endsWith(">")) {
comment = comment + "(array of file)";
formData.setType(DocGlobalConstants.PARAM_TYPE_FILE);
formData.setHasItems(true);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/ly/doc/template/JaxrsDocBuildTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ private List<ApiMethodDoc> buildControllerMethod(final JavaClass cls, ApiConfig
String annotationName = annotation.getType().getFullyQualifiedName();
if (JakartaJaxrsAnnotations.JAX_PATH_FULLY.equals(annotationName)
|| JAXRSAnnotations.JAX_PATH_FULLY.equals(annotationName)) {
ClassLoader classLoader = projectBuilder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = projectBuilder.getApiConfig().getClassLoader();
baseUrl = StringUtil.removeQuotes(DocUtil.getRequestHeaderValue(classLoader, annotation));
}
// use first annotation's value
if (annotationName.equals(JakartaJaxrsAnnotations.JAX_CONSUMES)
if (annotationName.equals(JakartaJaxrsAnnotations.JAX_CONSUMES_FULLY)
|| annotationName.equals(JAXRSAnnotations.JAX_CONSUMES_FULLY)) {
Object value = annotation.getNamedParameter("value");
if (Objects.nonNull(value)) {
Expand Down Expand Up @@ -332,7 +332,7 @@ private ApiMethodReqParam requestParams(final DocJavaMethod docJavaMethod, Proje
.setRequestParams(new ArrayList<>(0));
}
boolean isStrict = builder.getApiConfig().isStrict();
ClassLoader classLoader = builder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = builder.getApiConfig().getClassLoader();
JavaMethod javaMethod = docJavaMethod.getJavaMethod();
String className = javaMethod.getDeclaringClass().getCanonicalName();
Map<String, String> paramTagMap = docJavaMethod.getParamTagMap();
Expand Down Expand Up @@ -606,7 +606,7 @@ private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
formData.setDescription(comment);
formData.setValue(mockValue);
formDataList.add(formData);
} else if (JavaClassValidateUtil.isPrimitive(typeName)) {
} else if (JavaClassValidateUtil.isPrimitive(fullyQualifiedName)) {
FormData formData = new FormData();
formData.setKey(paramName);
formData.setDescription(comment);
Expand Down Expand Up @@ -651,7 +651,7 @@ private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
if (JavaClassValidateUtil.isPrimitive(simpleTypeName)) {
requestExample.setJsonBody(mockValue).setJson(true);
} else {
String json = JsonBuildHelper.buildJson(typeName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses, configBuilder);
String json = JsonBuildHelper.buildJson(fullyQualifiedName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), groupClasses, configBuilder);
requestExample.setJsonBody(JsonUtil.toPrettyFormat(json)).setJson(true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/template/RpcDocBuildTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private List<ApiParam> requestParams(final JavaMethod javaMethod,
if (parameterList.size() < 1) {
return null;
}
ClassLoader classLoader = builder.getJavaProjectBuilder().getClass().getClassLoader();
ClassLoader classLoader = builder.getApiConfig().getClassLoader();
List<ApiParam> paramList = new ArrayList<>();
for (JavaParameter parameter : parameterList) {
boolean required = false;
Expand Down
Loading

0 comments on commit c72e6aa

Please sign in to comment.