Skip to content

modelmapper can't map from a NULL or empty String to LocalDate #13

Open
@felipealvesgnu

Description

@felipealvesgnu
Contributor

I'm trying to map from a NULL json string field and I'm getting:

1) Converter org.modelmapper.module.jsr310.ToTemporalConverter@e3441d9 
      failed to convert java.lang.String to java.time.LocalDate.

1 error] with root cause
                 java.lang.NullPointerException: null
...

and when with an empty string field I get:

1) Converter org.modelmapper.module.jsr310.ToTemporalConverter@25b6e24d 
       failed to convert java.lang.String to java.time.LocalDate.

1 error] with root cause
                 java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
	...

Activity

internetstaff

internetstaff commented on Aug 7, 2020

@internetstaff

We're hitting this too, which renders the module useless to us.

Is there a simple universal work-around we're not aware of?

If not, is this by design or should it be fixed?

felipealvesgnu

felipealvesgnu commented on Aug 8, 2020

@felipealvesgnu
ContributorAuthor

in the beginning I tried this:


    @Bean
     public ModelMapper modelMapper() {
         ModelMapper modelMapper = new ModelMapper();

          Jsr310ModuleConfig config = Jsr310ModuleConfig.builder()
                 .dateTimePattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") // default is yyyy-MM-dd HH:mm:ss
                 .datePattern("yyyy-MM-dd")
                 .build();
         modelMapper.registerModule(new Jsr310Module(config));

        return modelMapper;
     }

With that code, you can format the pattern you receive the data, from your front-end.
So I started to had some problems with empty data that was coming from my Json, because of that I removed this module, and stayed just with modelmapper core and simply made a change in my DTO replacing date attributes from String to Localdate in DTOs and JPA entities.
Have you tried it?

internetstaff

internetstaff commented on Aug 8, 2020

@internetstaff

In my case I don't control the data coming in, and it's protobuf, so it has optional blank strings for dates that I simply need to skip.

felipealvesgnu

felipealvesgnu commented on Aug 8, 2020

@felipealvesgnu
ContributorAuthor

You can take this piece of code, that uses skip as an example:

modelMapper.typeMap(ActivityDTO.class, Person.class).addMappings(map -> map.skip(Person::setId));

In that case, I'm letting the mapper know that I don't want to set Activity.id attribute into Person.id, telling it to skip the mapping on that part.
Do you got it?

internetstaff

internetstaff commented on Aug 8, 2020

@internetstaff

Yes, I can skip individual fields, but I have dozens and dozens of fields to deal with, so a universal solution would be preferable.

Of course, it's easy enough to setup a custom blank-skipping converter for LocalDate/LocalDateTime, and that's what I've done, but that does mean this module is useless. :)

felipealvesgnu

felipealvesgnu commented on Aug 8, 2020

@felipealvesgnu
ContributorAuthor

Have you tried this, in your config class? It will ignore all incoming null attributes:

@Configuration
public class ModelMapperConfig {

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setSkipNullEnabled(true);

        return modelMapper;
    }
}
internetstaff

internetstaff commented on Aug 28, 2020

@internetstaff

@felipealvesgnu again, thanks, but in our case the incoming fields are empty strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @felipealvesgnu@internetstaff

        Issue actions

          modelmapper can't map from a NULL or empty String to LocalDate · Issue #13 · modelmapper/modelmapper-module-java8