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

How do I map an empty array in the source object? #700

Open
asmith2306 opened this issue Oct 24, 2022 · 1 comment
Open

How do I map an empty array in the source object? #700

asmith2306 opened this issue Oct 24, 2022 · 1 comment

Comments

@asmith2306
Copy link

If my source object has an empty array in it's hierarchy it doesn't get mapped (or is ignored maybe?). I created a program here to demonstrate: https://onecompiler.com/java/3yktwzjc3

The output is:

FooDTO before: FooDTO{name='My foo DTO', bar=BarDTO{name='My foo's bar', jars=[]}}

Foo before: Foo{name='My Foo', bar=Bar{name='null', jars=[Jar{name='Jar 1'}, Jar{name='Jar 2'}]}}

Foo after: Foo{name='My foo DTO', bar=Bar{name='My foo's bar', jars=[Jar{name='Jar 1'}, Jar{name='Jar 2'}]}}

You can see I map FooDTO -> Foo. The mapping for everything works e.g. the name fields, but the empty 'jars' array in the FooDTO's bar object doesn't get mapped.

Any ideas? Thanks

@devrishal
Copy link

devrishal commented Jan 31, 2023

You need to write convertors to get the mapping done and add the same in your modelmapper.

EG:

Converter<List<JarDTO>, List<Jar>> convertJarList= new AbstractConverter<List<JarDTO>, List<Jar>>() {
            protected List<Jar> convert(List<JarDTO> source) {
                return source.stream().map(identity -> {
                    Jar jar= new Jar();
                    jar.setName(identity.getName());
                    return jar;
                }).collect(Collectors.toList());
            }
        };
        modelMapper.addConverter(convertJarList);

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