Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants