Skip to content

Commit

Permalink
fix: fix for Git request parameter problem (#770)
Browse files Browse the repository at this point in the history
* update

* fix: fix for Git request parameter problem
  • Loading branch information
goodjava authored Jan 3, 2024
1 parent ad1cc15 commit eb07104
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion jcommon/docean/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Based on Java20
* Fully utilized coroutines and ScopeValue
* JVM parameters that need to be added:
--enable-preview--add-modulesjdk.incubator.concurrent-ea--add-opensjava.base/java.lang=ALL-UNNAMED--add-opensjava.base/jdk.internal.misc=ALL-UNNAMED-Dio.netty.tryReflectionSetAccessible=true
--enable-preview --add-modulesjdk.incubator.concurrent -ea --add-opensjava.base/java.lang=ALL-UNNAMED--add-opensjava.base/jdk.internal.misc=ALL-UNNAMED-Dio.netty.tryReflectionSetAccessible=true
* A lightweight microservices development framework. It can be embedded into the Spring framework.
* Features: Compliant with Java standards, lightweight, no unnecessary libraries, low memory footprint, fast service
requests, high maintainability, and supports plugin extensions.
Expand Down
8 changes: 8 additions & 0 deletions jcommon/docean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
17 changes: 10 additions & 7 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ public void callService(MvcContext context, MvcRequest request, MvcResponse resp

public void callMethod(MvcContext context, MvcRequest request, MvcResponse response, MvcResult<Object> result, HttpRequestMethod method) {
Safe.run(() -> {
JsonElement args = getArgs(method, request.getMethod().toLowerCase(Locale.ROOT), request);
context.setParams(args);
Object[] params = methodInvoker.getMethodParams(method.getMethod(), args);
setMvcContext(context, params);
JsonElement args = getArgs(method, request.getMethod().toLowerCase(Locale.ROOT), request, context);
Object[] params = new Object[]{null};
if (method.getMethod().getParameterTypes().length == 1 && method.getMethod().getParameterTypes()[0].equals(MvcContext.class)) {
params[0] = context;
} else {
params = methodInvoker.getMethodParams(method.getMethod(), args);
}
Object data = this.mvcConfig.isUseCglib() ? methodInvoker.invokeFastMethod(method.getObj(), method.getMethod(), params) :
methodInvoker.invokeMethod(method.getObj(), method.getMethod(), params);

Expand Down Expand Up @@ -229,11 +232,11 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
* @param httpMethod
* @return
*/
private JsonElement getArgs(HttpRequestMethod method, String httpMethod, MvcRequest req) {
private JsonElement getArgs(HttpRequestMethod method, String httpMethod, MvcRequest req, MvcContext context) {
if (httpMethod.equalsIgnoreCase("get")) {
return Get.getParams(method, req.getUri());
return Get.getParams(method, req.getUri(), context);
} else if (httpMethod.equalsIgnoreCase("post")) {
return Post.getParams(method, req.getBody());
return Post.getParams(method, req.getBody(), context);
} else {
throw new DoceanException("don't support:" + httpMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.xiaomi.youpin.docean.mvc;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.xiaomi.youpin.docean.anno.RequestParam;
import com.xiaomi.youpin.docean.mvc.httpmethod.HttpMethodUtils;
import io.netty.handler.codec.http.QueryStringDecoder;
Expand All @@ -31,22 +33,21 @@
*/
public abstract class Get {

public static JsonArray getParams(HttpRequestMethod method, String uri) {
public static JsonElement getParams(HttpRequestMethod method, String uri, MvcContext context) {
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Map<String, String> params = decoder.parameters().entrySet().stream().collect(Collectors.toMap(it -> it.getKey(), it -> it.getValue().get(0)));
JsonObject res = new JsonObject();
params.entrySet().forEach(it -> res.addProperty(it.getKey(), it.getValue()));
context.setParams(res);

JsonArray array = new JsonArray();
HttpMethodUtils.addMvcContext(method, array);
if (null == params) {
return array;
}
Annotation[][] anns = method.getMethod().getParameterAnnotations();
Arrays.stream(anns).forEach(it -> {
if (it.length > 0) {
RequestParam param = getRequestParam(it);
String name = param.value();
if (!params.containsKey(name)) {
array.add("");
// throw new DoceanException("Missing parameter:" + name);
} else {
array.add(params.get(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public abstract class Post {

private static Gson gson = new Gson();

public static JsonArray getParams(HttpRequestMethod method, byte[] data) {
public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcContext context) {
JsonElement arguments = (null == data || data.length == 0) ? null : gson.fromJson(new String(data), JsonElement.class);
JsonArray array = new JsonArray();
HttpMethodUtils.addMvcContext(method, array);

if (null == arguments) {
context.setParams(array);
return array;
}

Expand All @@ -49,6 +50,8 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data) {
array.add(arguments.getAsJsonPrimitive());
}

context.setParams(array);

return array;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
public class HttpMethodUtils {

public static void addMvcContext(HttpRequestMethod method, JsonArray array) {
if (paramIsMvcContext(method)) {
array.add(new JsonObject());
}
}

public static boolean paramIsMvcContext(HttpRequestMethod method) {
Class<?>[] types = method.getMethod().getParameterTypes();
if (types.length > 0 && types[0] == MvcContext.class) {
array.add(new JsonObject());
return true;
}
return false;
}


Expand Down

0 comments on commit eb07104

Please sign in to comment.