[ 
https://issues.apache.org/jira/browse/JOHNZON-176?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16530342#comment-16530342
 ] 

Romain Manni-Bucau commented on JOHNZON-176:
--------------------------------------------

Hi

 

A correct fix is either to use a LinkedHashMap to make it deterministic or to 
select more precisely the class to use but in all cases it is key to not loop 
over all classes otherwise configuration cant be refined as needed in some 
cases.

 

Cause the last option will soon also require generics handling if we start to 
go that path, Im tempted to keep it simple and just ensure the entry set is 
sorted (or rephrased, that the user order is guaranteed using a list or 
linkedhashmap or equivalent).

 

Would that work for you?

> setIgnoreFieldsForType does not keep hierarchy in mind
> ------------------------------------------------------
>
>                 Key: JOHNZON-176
>                 URL: https://issues.apache.org/jira/browse/JOHNZON-176
>             Project: Johnzon
>          Issue Type: Improvement
>          Components: Mapper
>    Affects Versions: 1.1.8
>            Reporter: Haino
>            Priority: Major
>              Labels: newbie
>         Attachments: JohnzonIgnoreTest.java
>
>
> When serializing objects with a Mapper which has a bunch of 
> setIgnoreFieldsForType configs, only the first matching class gets its 
> properties removed for serializing in BaseAccessMode. These 
> {color:#660e7a}fieldsToRemove{color} are stored in a HashMap, which doesn't 
> guarantee order, so repeating classes from more to less specific is not 
> reliable to ensure all desired fields are ignored. Below, you can see why: I 
> marked the line which causes the sanitize method to stop too soon. I expect 
> all classes in the {color:#660e7a}fieldsToRemove {color}{color:#333333}map to 
> be looped and used to sanitize. I added a test in attachment to reproduce 
> this issue. Unfortunately, my project does not allow using Annotations to 
> reach the expected behavior, as 90+% of the objects are being generated.
> {color}
> {color:#333333}Note: the output of my test even changes depending on the 
> order in which the 2 calls to setIgnoreFieldsForType are added.{color}
>  
>  
>  
> {color:#000080}private {color}<{color:#20999d}T{color}> Map<String, 
> {color:#20999d}T{color}> sanitize({color:#000080}final {color}Class<?> type, 
> {color:#000080}final {color}Map<String, {color:#20999d}T{color}> delegate) {
> {color:#000080}    for {color}({color:#000080}final 
> {color}Map.Entry<Class<?>, String[]> entry : 
> {color:#660e7a}fieldsToRemove{color}.entrySet()) {
>         {color:#000080}if {color}(entry.getKey().isAssignableFrom(type)) {
>             {color:#000080}for {color}({color:#000080}final {color}String 
> field : entry.getValue()) {
>                 delegate.remove(field);
>             }
>             -{color:#000080}return {color}delegate;-
>         }
>      }
>      {color:#000080}return {color}delegate;
> }



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to