Skip to content

Commit

Permalink
fix: null handling in config mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Dec 6, 2023
1 parent 578865c commit b84200e
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/main/java/io/supertokens/storage/mysql/utils/ConfigMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package io.supertokens.storage.mysql.utils;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonAlias;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -72,21 +72,9 @@ private static <T> Field findField(Class<T> clazz, String key) {
}

private static <T> void setValue(T object, Field field, JsonElement value) throws InvalidConfigException {
boolean foundAnnotation = false;
for (Annotation a : field.getAnnotations()) {
if (a.toString().contains("JsonProperty")) {
foundAnnotation = true;
break;
}
}

if (!foundAnnotation) {
return;
}

field.setAccessible(true);
Object convertedValue = convertJsonElementToTargetType(value, field.getType(), field.getName());
if (convertedValue != null) {
if (convertedValue != null || isNullable(field.getType())) {
try {
field.set(object, convertedValue);
} catch (IllegalAccessException e) {
Expand All @@ -95,6 +83,10 @@ private static <T> void setValue(T object, Field field, JsonElement value) throw
}
}

private static boolean isNullable(Class<?> type) {
return !type.isPrimitive();
}

private static Object convertJsonElementToTargetType(JsonElement value, Class<?> targetType, String fieldName)
throws InvalidConfigException {
// If the value is JsonNull, return null for any type
Expand Down

0 comments on commit b84200e

Please sign in to comment.