Commit 3c27dd06 authored by Jeremie Bresson's avatar Jeremie Bresson
Browse files

Merge remote-tracking branch 'origin/main' into 6.x

# Conflicts:
#	src/main/java/org/gitlab4j/api/models/Link.java
#	src/main/java/org/gitlab4j/api/models/PackageFile.java
parents 982f4b64 ad6d8d4f
......@@ -3,6 +3,7 @@ package org.gitlab4j.api.webhook;
import org.gitlab4j.api.utils.JacksonJson;
public class WikiPageEvent extends AbstractEvent {
private static final long serialVersionUID = 1L;
public static final String X_GITLAB_EVENT = "Wiki Page Hook";
public static final String OBJECT_KIND = "wiki_page";
......
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gitlab4j.api.utils.JacksonJson;
import org.junit.jupiter.api.Assertions;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -26,7 +37,7 @@ public class JsonUtils {
}
static JsonNode readTreeFromMap(Map<String, Object> map) throws JsonParseException, JsonMappingException, IOException {
String jsonString = jacksonJson.getObjectMapper().writeValueAsString(map);
String jsonString = jacksonJson.getObjectMapper().writeValueAsString(map);
return (jacksonJson.readTree(jsonString));
}
......@@ -40,48 +51,120 @@ public class JsonUtils {
}
static <T> T unmarshalResource(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (jacksonJson.unmarshal(returnType, reader));
}
static <T> List<T> unmarshalResourceList(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
static <T> List<T> unmarshalResourceList(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (JsonUtils.unmarshalList(returnType, reader));
}
static <T> Map<String, T> unmarshalResourceMap(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (jacksonJson.unmarshalMap(returnType, reader));
}
static <T> T unmarshal(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshal(returnType, reader));
}
static <T> T unmarshal(Class<T> returnType, JsonNode tree) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshal(returnType, tree));
}
static <T> T unmarshal(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshal(returnType, json));
}
static <T> List<T> unmarshalList(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshalList(returnType, reader));
}
static <T> List<T> unmarshalList(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshalList(returnType, json));
}
static <T> Map<String, T> unmarshalMap(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshalMap(returnType, reader));
}
static <T> Map<String, T> unmarshalMap(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
checkSerializable(returnType);
return (jacksonJson.unmarshalMap(returnType, json));
}
static <T> void checkSerializable(Class<T> cls) {
if(!isSerializable(cls, new HashSet<>())) {
fail("Class " + cls.getCanonicalName() + " or one of its member does not implement Serializable");
}
}
static <T> boolean isSerializable(Class<T> cls, Set<Class<?>> checkedTypes) {
if (checkedTypes.contains(cls)) {
return true;
}
checkedTypes.add(cls);
if (!Serializable.class.isAssignableFrom(cls)) {
return false;
}
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) {
Class<?> fieldClass = field.getType();
if (!isSimpleType(fieldClass) && !isSerializable(fieldClass, checkedTypes) && !isCollectionSerializable(field, checkedTypes)) {
return false;
}
}
}
return true;
}
private static boolean isSimpleType(Class<?> type) {
return type.isPrimitive() ||
type.equals(String.class) ||
type.equals(Integer.class) ||
type.equals(Long.class) ||
type.equals(Double.class) ||
type.equals(Float.class) ||
type.equals(Boolean.class) ||
type.equals(Character.class) ||
type.equals(Byte.class) ||
type.equals(Short.class);
}
private static boolean isCollectionSerializable(Field field, Set<Class<?>> checkedTypes) {
Class<?> fieldType = field.getType();
if (Collection.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType)) {
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
Type[] typeArguments = parameterizedType.getActualTypeArguments();
for (Type typeArg : typeArguments) {
if (typeArg instanceof Class) {
Class<?> typeClass = (Class<?>) typeArg;
if (!isSimpleType(typeClass) && !isSerializable(typeClass, checkedTypes)) {
return false;
}
}
}
}
}
return true;
}
static <T> boolean compareJson(T apiObject, String filename) throws IOException {
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (compareJson(apiObject, reader));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment