Skip to content

The Unified and Autonomous Development Platform

Minor RESTful api help improvement

Robin Shenea04f96de446
diff --git a/server-core/src/main/java/io/onedev/server/model/Project.java b/server-core/src/main/java/io/onedev/server/model/Project.java
index e77507a65d..64b68ac35a 100644
--- a/server-core/src/main/java/io/onedev/server/model/Project.java
+++ b/server-core/src/main/java/io/onedev/server/model/Project.java
@@ -113,6 +113,7 @@ import io.onedev.server.model.support.issue.ProjectIssueSetting;
import io.onedev.server.model.support.pullrequest.ProjectPullRequestSetting;
import io.onedev.server.persistence.SessionManager;
import io.onedev.server.persistence.TransactionManager;
+import io.onedev.server.rest.annotation.Api;
import io.onedev.server.security.SecurityUtils;
import io.onedev.server.storage.AttachmentStorageManager;
import io.onedev.server.storage.StorageManager;
@@ -201,6 +202,8 @@ public class Project extends AbstractEntity implements NameAware {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(nullable=true)
+ @Api(description="Represents the project from which this project is forked. Remove this property if "
+ + "the project is not a fork when create/update the project")
private Project forkedFrom;
@Column(nullable=false, unique=true)
@@ -228,9 +231,11 @@ public class Project extends AbstractEntity implements NameAware {
private LinkedHashMap<String, ContributedProjectSetting> contributedSettings = new LinkedHashMap<>();
@Column(nullable=false)
+ @Api(readOnly=true)
private Date createDate = new Date();
@Column(nullable=false)
+ @Api(readOnly=true)
private Date updateDate = new Date();
@OneToMany(mappedBy="targetProject", cascade=CascadeType.REMOVE)
@@ -248,6 +253,8 @@ public class Project extends AbstractEntity implements NameAware {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(nullable=true)
+ @Api(description="This represents default role of the project. Remove this property if the project should not "
+ + "have a default role when create/update the project")
private Role defaultRole;
@OneToMany(mappedBy="project", cascade=CascadeType.REMOVE)
diff --git a/server-core/src/main/java/io/onedev/server/rest/annotation/Api.java b/server-core/src/main/java/io/onedev/server/rest/annotation/Api.java
index 7d43ad97d4..fd8a3f588c 100644
--- a/server-core/src/main/java/io/onedev/server/rest/annotation/Api.java
+++ b/server-core/src/main/java/io/onedev/server/rest/annotation/Api.java
@@ -17,6 +17,8 @@ public @interface Api {
String description() default "";
+ boolean readOnly() default false;
+
int order() default 100;
}
diff --git a/server-core/src/main/java/io/onedev/server/web/page/help/ApiHelpUtils.java b/server-core/src/main/java/io/onedev/server/web/page/help/ApiHelpUtils.java
index 93b2b26f6b..3d8edae37f 100644
--- a/server-core/src/main/java/io/onedev/server/web/page/help/ApiHelpUtils.java
+++ b/server-core/src/main/java/io/onedev/server/web/page/help/ApiHelpUtils.java
@@ -34,18 +34,19 @@ import io.onedev.server.rest.annotation.Api;
import io.onedev.server.util.BeanUtils;
import io.onedev.server.util.ReflectionUtils;
import io.onedev.server.web.page.layout.ContributedAdministrationSetting;
+import io.onedev.server.web.page.help.ValueInfo.Origin;
import io.onedev.server.web.page.layout.AdministrationSettingContribution;
import io.onedev.server.web.page.project.setting.ContributedProjectSetting;
import io.onedev.server.web.page.project.setting.ProjectSettingContribution;
public class ApiHelpUtils {
- public static Serializable getExampleValue(Type valueType) {
- return getExampleValue(valueType, Sets.newHashSet());
+ public static Serializable getExampleValue(Type valueType, ValueInfo.Origin origin) {
+ return getExampleValue(valueType, Sets.newHashSet(), origin);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- public static Serializable getExampleValue(Type valueType, Set<Class<?>> parsedTypes) {
+ public static Serializable getExampleValue(Type valueType, Set<Class<?>> parsedTypes, ValueInfo.Origin origin) {
Class<?> valueClass = ReflectionUtils.getClass(valueType);
Object value = new ExampleProvider(valueClass, valueClass.getAnnotation(Api.class)).getExample();
if (value == null) {
@@ -77,7 +78,7 @@ public class ApiHelpUtils {
throw new RuntimeException(e);
}
}
- collection.add(getExampleValue(collectionElementType, parsedTypes));
+ collection.add(getExampleValue(collectionElementType, parsedTypes, origin));
return (Serializable) collection;
} else if (Map.class.isAssignableFrom(valueClass)) {
Type mapKeyType = ReflectionUtils.getMapKeyType(valueType);
@@ -98,7 +99,8 @@ public class ApiHelpUtils {
}
}
- map.put(getExampleValue(mapKeyType, parsedTypes), getExampleValue(mapValueType, parsedTypes));
+ map.put(getExampleValue(mapKeyType, parsedTypes, origin),
+ getExampleValue(mapValueType, parsedTypes, origin));
return (Serializable) map;
} else {
@@ -112,7 +114,7 @@ public class ApiHelpUtils {
instantiationClass = valueClass;
}
value = new ObjenesisStd(true).getInstantiatorOf(instantiationClass).newInstance();
- for (Field field: getJsonFields(instantiationClass)) {
+ for (Field field: getJsonFields(instantiationClass, origin)) {
Object fieldValue = new ExampleProvider(valueClass, field.getAnnotation(Api.class)).getExample();
if (fieldValue == null) {
if (field.getAnnotation(ManyToOne.class) != null) {
@@ -120,11 +122,11 @@ public class ApiHelpUtils {
Field idField = AbstractEntity.class.getDeclaredField("id");
Object id = new ExampleProvider(idField.getType(), idField.getAnnotation(Api.class)).getExample();
if (id == null)
- id = getExampleValue(idField.getGenericType(), new HashSet<>(parsedTypes));
+ id = getExampleValue(idField.getGenericType(), new HashSet<>(parsedTypes), origin);
idField.setAccessible(true);
idField.set(fieldValue, id);
} else {
- fieldValue = getExampleValue(field.getGenericType(), new HashSet<>(parsedTypes));
+ fieldValue = getExampleValue(field.getGenericType(), new HashSet<>(parsedTypes), origin);
}
}
field.setAccessible(true);
@@ -171,14 +173,18 @@ public class ApiHelpUtils {
return implementations;
}
- public static List<Field> getJsonFields(Class<?> beanClass) {
+ public static List<Field> getJsonFields(Class<?> beanClass, ValueInfo.Origin origin) {
List<Field> fields = new ArrayList<>();
for (Field field: BeanUtils.findFields(beanClass)) {
if (field.getAnnotation(JsonIgnore.class) != null
|| field.getAnnotation(OneToMany.class) != null
|| field.getAnnotation(Transient.class) != null
- || Modifier.isTransient(field.getModifiers()))
+ || Modifier.isTransient(field.getModifiers())
+ || origin == Origin.REQUEST_BODY
+ && field.getAnnotation(Api.class) != null
+ && field.getAnnotation(Api.class).readOnly()) {
continue;
+ }
if (field.getAnnotation(JsonProperty.class) != null) {
fields.add(field);
} else {
diff --git a/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java b/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
index ae9dd16e56..da3c49d5d3 100644
--- a/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
+++ b/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
@@ -466,7 +466,7 @@ public class ExampleValuePanel extends Panel {
@Override
protected List<Field> load() {
- return ApiHelpUtils.getJsonFields(getValue().getClass());
+ return ApiHelpUtils.getJsonFields(getValue().getClass(), getValueOrigin());
}
};
@@ -516,7 +516,7 @@ public class ExampleValuePanel extends Panel {
@Override
public void onClick(AjaxRequestTarget target) {
- Serializable newValue = ApiHelpUtils.getExampleValue(clazz);
+ Serializable newValue = ApiHelpUtils.getExampleValue(clazz, getValueOrigin());
valueModel.setObject(newValue);
target.add(ExampleValuePanel.this);
send(getPage(), Broadcast.BREADTH, new ExampleValueChanged(target));
diff --git a/server-core/src/main/java/io/onedev/server/web/page/help/MethodDetailPage.java b/server-core/src/main/java/io/onedev/server/web/page/help/MethodDetailPage.java
index d33de84257..d43427a38f 100644
--- a/server-core/src/main/java/io/onedev/server/web/page/help/MethodDetailPage.java
+++ b/server-core/src/main/java/io/onedev/server/web/page/help/MethodDetailPage.java
@@ -116,7 +116,7 @@ public class MethodDetailPage extends ApiHelpPage {
if (param != null) {
Serializable exampleValue = new ExampleProvider(resourceClass, param.getAnnotation(Api.class)).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType());
+ exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType(), ValueInfo.Origin.REQUEST_BODY);
requestBodyClass = exampleValue.getClass();
IModel<ValueInfo> valueInfoModel = new LoadableDetachableModel<ValueInfo>() {
@@ -200,7 +200,7 @@ public class MethodDetailPage extends ApiHelpPage {
Serializable exampleValue = new ExampleProvider(getResourceMethod().getDeclaringClass(), api).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType());
+ exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType(), ValueInfo.Origin.PATH_PLACEHOLDER);
IModel<ValueInfo> valueInfoModel = new LoadableDetachableModel<ValueInfo>() {
@@ -251,7 +251,7 @@ public class MethodDetailPage extends ApiHelpPage {
Serializable exampleValue = new ExampleProvider(getResourceMethod().getDeclaringClass(), api).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType());
+ exampleValue = ApiHelpUtils.getExampleValue(param.getParameterizedType(), ValueInfo.Origin.QUERY_PARAM);
IModel<ValueInfo> valueInfoModel = new LoadableDetachableModel<ValueInfo>() {
@@ -280,7 +280,7 @@ public class MethodDetailPage extends ApiHelpPage {
Fragment fragment = new Fragment("successResponseBody", "hasResponseBodyFrag", MethodDetailPage.this);
Serializable exampleValue = new ExampleProvider(resourceClass, method.getAnnotation(Api.class)).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(method.getGenericReturnType());
+ exampleValue = ApiHelpUtils.getExampleValue(method.getGenericReturnType(), ValueInfo.Origin.RESPONSE_BODY);
IModel<ValueInfo> valueInfoModel = new LoadableDetachableModel<ValueInfo>() {
@@ -329,7 +329,7 @@ public class MethodDetailPage extends ApiHelpPage {
Api api = pathParam.getAnnotation(Api.class);
Serializable exampleValue = new ExampleProvider(getResourceMethod().getDeclaringClass(), api).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(pathParam.getParameterizedType());
+ exampleValue = ApiHelpUtils.getExampleValue(pathParam.getParameterizedType(), ValueInfo.Origin.PATH_PLACEHOLDER);
endPoint = endPoint.replace("{" + pathParam.getAnnotation(PathParam.class).value() + "}", String.valueOf(exampleValue));
}
}
@@ -343,7 +343,7 @@ public class MethodDetailPage extends ApiHelpPage {
Api api = queryParam.getAnnotation(Api.class);
Serializable exampleValue = new ExampleProvider(getResourceMethod().getDeclaringClass(), api).getExample();
if (exampleValue == null)
- exampleValue = ApiHelpUtils.getExampleValue(queryParam.getParameterizedType());
+ exampleValue = ApiHelpUtils.getExampleValue(queryParam.getParameterizedType(), ValueInfo.Origin.QUERY_PARAM);
queryParams.put(paramName, String.valueOf(exampleValue));
}