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

@JsonSerialize with nullUsing option not working for String properties #33

Closed
esoni88 opened this issue Oct 17, 2017 · 4 comments
Closed
Milestone

Comments

@esoni88
Copy link

esoni88 commented Oct 17, 2017

Hi All,
if i use AfterModule
this.registerModule(new AfterburnerModule());

the following annotation
@JsonSerialize(nullsUsing=CustomSerialize.class)

not work.
How can enable it ? i want to customize null serialization for certain fields.
Thanks
Dario

@cowtowncoder
Copy link
Member

@dario-frongillo It should just work. If not, it would be great to have a unit test that shows the problem so we can fix it.

@esoni88
Copy link
Author

esoni88 commented Oct 18, 2017

Hi @cowtowncoder ,
this is a a simple spring boot app that show you the issue: https://github.com/dario-frongillo/test-null-serializer-afterburnerModule

I customized the object mapper in order to use AfterburnerModule

@Component
@Primary
public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper() {
        // if you comment this instruction the  NULL field with annotation  
 //@JsonSerialize(nullsUsing=MyNullSerializer.class)
        // are correctly serialized with MyNullSerializer
        this.registerModule(new AfterburnerModule());
        disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
    }
}

I have a model Person with certain fields with annotation :@JsonSerialize(nullsUsing=MyNullSerializer.class)

for i.e

    @JsonSerialize(nullsUsing=MyNullSerializer.class)
    private String c;

I have a simple controller to test it with a curl tool as postman

    @RequestMapping("/greeting")
    public ResponseEntity<?> getProva(){
        Person a = new Person();
        a.setA("x");
        a.setB(null);
        a.setC(null);
        return ResponseEntity.ok(a);
    }

the controller return c field with null so MyNullSerializer should intercept the serialization but this does not happen.
If i comment the instruction

this.registerModule(new AfterburnerModule());`

on my object mapper all it works.. so AfterburnerModule not allow you to use null serializer.. can you kindly check it ?
Thanks a lot
Dario

@cowtowncoder
Copy link
Member

cowtowncoder commented Oct 20, 2017

Ok I'll try to create stand-alone test (it makes no sense to add Spring Boot dependency for unit tests) and may be able to resolve this. Afterburner should not change behavior in any way here.

@cowtowncoder cowtowncoder added this to the 2.8.0 milestone Oct 20, 2017
@cowtowncoder cowtowncoder changed the title @JsonSerialize with nullusing option not work @JsonSerialize with nullUsing option not working for String properties Oct 20, 2017
@cowtowncoder
Copy link
Member

Thank you for reporting this -- this turned out to specifically affect String valued properties, since they are optimized separately (along with int, long and boolean); and being the only Object value (as opposed to primitive) was missing one check that "general Object-valued" handler was doing.

Fix will be in 2.9.3 but I also back-ported it in 2.8 branch in case 2.8.11 might be released at some point.

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

No branches or pull requests

2 participants