Skip to content

Commit cb2bf5a

Browse files
committed
Be more tolerant with Map/Iterable type sample data generating
1 parent 5edd394 commit cb2bf5a

2 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/main/java/act/apidoc/Endpoint.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import act.app.DevModeClassLoader;
2525
import act.app.Source;
2626
import act.app.data.StringValueResolverManager;
27-
import act.db.DbBind;
2827
import act.handler.RequestHandler;
2928
import act.handler.builtin.controller.RequestHandlerProxy;
3029
import act.handler.builtin.controller.impl.ReflectedHandlerInvoker;
@@ -40,6 +39,7 @@
4039
import org.osgl.mvc.result.Result;
4140
import org.osgl.storage.ISObject;
4241
import org.osgl.util.C;
42+
import org.osgl.util.Generics;
4343
import org.osgl.util.N;
4444
import org.osgl.util.StringValueResolver;
4545
import org.rythmengine.utils.S;
@@ -339,10 +339,10 @@ private ParamInfo paramInfo(Type type, Annotation[] annos, DependencyInjector in
339339
if (ParamValueLoaderService.providedButNotDbBind(spec, injector)) {
340340
return null;
341341
}
342-
if (spec.hasAnnotation(DbBind.class)) {
342+
if (ParamValueLoaderService.hasDbBind(spec.allAnnotations())) {
343343
return new ParamInfo(name, BeanSpec.of(String.class, Act.injector()), name + " id");
344344
}
345-
String description = spec.toString();
345+
String description = spec.rawType().getSimpleName();
346346
Description descAnno = spec.getAnnotation(Description.class);
347347
if (null != descAnno) {
348348
description = descAnno.value();
@@ -462,19 +462,36 @@ private static Object generateSampleData(BeanSpec spec, Set<Class<?>> typeChain)
462462
return null;
463463
} else if (Map.class.isAssignableFrom(type)) {
464464
Map map = $.cast(Act.getInstance(type));
465-
Type keyType = spec.typeParams().get(0);
466-
Type valType = spec.typeParams().get(1);
467-
map.put(
468-
generateSampleData(BeanSpec.of(keyType, null, Act.injector()), typeChain),
469-
generateSampleData(BeanSpec.of(valType, null, Act.injector()), typeChain));
470-
map.put(
471-
generateSampleData(BeanSpec.of(keyType, null, Act.injector()), typeChain),
472-
generateSampleData(BeanSpec.of(valType, null, Act.injector()), typeChain));
465+
List<Type> typeParams = spec.typeParams();
466+
if (typeParams.isEmpty()) {
467+
typeParams = Generics.typeParamImplementations(type, Map.class);
468+
}
469+
if (typeParams.size() < 2) {
470+
map.put(S.random(), S.random());
471+
map.put(S.random(), S.random());
472+
} else {
473+
Type keyType = typeParams.get(0);
474+
Type valType = typeParams.get(1);
475+
map.put(
476+
generateSampleData(BeanSpec.of(keyType, null, Act.injector()), typeChain),
477+
generateSampleData(BeanSpec.of(valType, null, Act.injector()), typeChain));
478+
map.put(
479+
generateSampleData(BeanSpec.of(keyType, null, Act.injector()), typeChain),
480+
generateSampleData(BeanSpec.of(valType, null, Act.injector()), typeChain));
481+
}
473482
} else if (Iterable.class.isAssignableFrom(type)) {
474483
Collection col = $.cast(Act.getInstance(type));
475-
Type componentType = spec.typeParams().get(0);
476-
col.add(generateSampleData(BeanSpec.of(componentType, null, Act.injector()), typeChain));
477-
col.add(generateSampleData(BeanSpec.of(componentType, null, Act.injector()), typeChain));
484+
List<Type> typeParams = spec.typeParams();
485+
if (typeParams.isEmpty()) {
486+
typeParams = Generics.typeParamImplementations(type, Map.class);
487+
}
488+
if (typeParams.isEmpty()) {
489+
col.add(S.random());
490+
} else {
491+
Type componentType = typeParams.get(0);
492+
col.add(generateSampleData(BeanSpec.of(componentType, null, Act.injector()), typeChain));
493+
col.add(generateSampleData(BeanSpec.of(componentType, null, Act.injector()), typeChain));
494+
}
478495
return col;
479496
}
480497

src/main/java/act/inject/param/ParamValueLoaderService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,12 +785,17 @@ public static boolean providedButNotDbBind(BeanSpec beanSpec, DependencyInjector
785785
return false;
786786
}
787787
Annotation[] aa = beanSpec.allAnnotations();
788-
for (Annotation a : aa) {
788+
return !hasDbBind(aa);
789+
}
790+
791+
// DbBind is special: it's class loader is AppClassLoader
792+
public static boolean hasDbBind(Annotation[] annotations) {
793+
for (Annotation a : annotations) {
789794
if (a.annotationType().getName().equals(DbBind.class.getName())) {
790-
return false;
795+
return true;
791796
}
792797
}
793-
return true;
798+
return false;
794799
}
795800

796801
}

0 commit comments

Comments
 (0)