Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Can't use @Mapping#source for map keys containg the dot character #3066

@sajkol

Description

@sajkol

Expected behavior

Using the new map->bean mapping feature it is not possible to use @Mapping#source for keys containing a . character. Instead, the Map type itself seems to be considered as the source type and its methods are proposed as alternatives.

It's still possible to achieve that with expression of course (as in the commented out line in the example), but it's far less elegant as just using the source.

Actual behavior

error: No property named "a.b" exists in source parameter(s). Did you mean "a.bytes"?
        @Mapping(target = "value", source = "a.b")

Steps to reproduce the problem

package com.example;

import org.junit.jupiter.api.Test;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class MapstructExampleTest {

    public static record ExampleBean(String value) { }

    @Mapper
    public interface ExampleMapper {

        @Mapping(target = "value", source = "a.b")
        //@Mapping(target = "value", expression = "java(source.get(\"a.b\"))")
        ExampleBean map(Map<String, String> source);
    }

    @Test
    void shouldMapToBean() {
        Map<String, String> source = Map.of("a.b", "test");
        var output = Mappers.getMapper(ExampleMapper.class).map(source);
        assertThat(output.value()).isEqualTo("test");
    }

}

MapStruct Version

1.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions