Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Pojo encoder not considered the super class attributes. #75

Open
mariasekar opened this issue Jul 31, 2019 · 6 comments · May be fixed by #102
Open

Pojo encoder not considered the super class attributes. #75

mariasekar opened this issue Jul 31, 2019 · 6 comments · May be fixed by #102

Comments

@mariasekar
Copy link

Hello Feign team,
The pojo encoder is not considering the base class fields.
We could restrict it till Object.

While looking into feign form code, we have come across the PojoUtil.java.
There is a method called "toMap() which will convert pojo to respective map objects.
The reflection uses the "getDeclaredFields()" which will give only current class attributes only. Hence the base class attributes will be missed out during the map conversion.
So please consider the base class attributes as well in the encoder.

Thanks for your Time,
Maria Sekar

@mariasekar
Copy link
Author

mariasekar commented Jul 31, 2019

Here is the code,

public static Map<String, Object> toMap (Object object) {
HashMap<String, Object> result = new HashMap<String, Object>();
try {
Class<?> clazz = object.getClass();
while(clazz != null) { // Dont want to proces Object.class
for (Field field : clazz.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (isFinal(modifiers) || isStatic(modifiers)) {
continue;
}
setFeieldAccessible(field);

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

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

                result.put(propertyKey, fieldValue);
            }
            clazz = clazz.getSuperclass();
        }

    } catch (Exception e) {

    }

    return result;
}

For the time being i have extended the formencoder and did the modifications.

@rsercano
Copy link

+1

@slawekjaranowski
Copy link

can be connected with: #95

@Sidabw
Copy link

Sidabw commented May 8, 2021

+1

@muhrifqii muhrifqii linked a pull request Oct 7, 2021 that will close this issue
@alexisgayte
Copy link

alexisgayte commented Mar 24, 2023

Hi,
Is it possible to use a class method that call "toMap()" instead of a static method ("toMap()")?
This will at least allow user to override it.

@FiratShahverdiyev
Copy link

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants