mapstruct / mapstruct/mapstruct

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

Open
#3,066 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
7.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ExampleMapper and shouldMapToBean reproducer, focusing on how @Mapping#source resolves the dotted key "a.b" for a Map<String, String> source. Run the reproducer with MapStruct 1.5.3 and trace the source-property resolution path. Done means the mapping compiles and the test produces an ExampleBean whose value is "test".

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.