feign-form icon indicating copy to clipboard operation
feign-form copied to clipboard

POJO encoder doesn't handle final fields

Open PeterKelecom opened this issue 5 years ago • 3 comments

Final fields are ignored in PojoUtils.toMap:

@SneakyThrows
  public static Map<String, Object> toMap (@NonNull Object object) {
    val result = new HashMap<String, Object>();
    val type = object.getClass();
    val setAccessibleAction = new SetAccessibleAction();
    for (val field : type.getDeclaredFields()) {
      val modifiers = field.getModifiers();
      if (isFinal(modifiers) || isStatic(modifiers)) {
        continue;
      }
      setAccessibleAction.setField(field);
      AccessController.doPrivileged(setAccessibleAction);

      val fieldValue = field.get(object);
      if (fieldValue == null) {
        continue;
      }

      val propertyKey = field.isAnnotationPresent(FormProperty.class)
                        ? field.getAnnotation(FormProperty.class).value()
                        : field.getName();

      result.put(propertyKey, fieldValue);
    }
    return result;
  }

This means we can't use data classes in Kotlin. Is there a compelling reason why final fields are being ignored?

PeterKelecom avatar Aug 26 '19 13:08 PeterKelecom

can be connected with: #95

slawekjaranowski avatar Oct 09 '20 06:10 slawekjaranowski

This means we can't use data classes in Kotlin.

You can use them, but you cant use val, just var (valid for both class and data class)

WtfJoke avatar Sep 17 '21 07:09 WtfJoke

This has broader consequences: not only final fields are silently ignored, but also it requires all form params to be actual fields. I can imagine a scenario where form param is calculated on the fly and is not backed by any field (e.g. String getFoo() { return "xxx" }. On the other hand, it will send private fields that are not exposed from the class and therefore not a part of it's bean definition; for example Kotlin delegates, etc.

Any reason why param extraction is done this way instead of using standard java.beans.Introspector?

zarebski-m avatar Nov 08 '21 20:11 zarebski-m