GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Two different constants are ignored · Issue #3673 · mapstruct/mapstruct

Two different constants are ignored #3673

New issue
Copy link
New issue
Copy link
Closed
#3677
Closed
Two different constants are ignored#3673
#3677
Copy link
Assignees
filiphr
Labels
bug
Milestone
1.6.1

Description

@jacobbetz
jacobbetz
opened on Aug 16, 2024
Issue body actions

Expected behavior

I have this mapping defined

    @Mapping(target = "details.name", source = "details.name")
    @Mapping(target = "details.type", constant = "DOG")
    Animal map(Dog dog);

    @Mapping(target = "details.name", source = "details.name")
    @Mapping(target = "details.type", constant = "CAT")
    Animal map(Cat cat);

I would expect, that the field details.type is set dependent on the source object.

Actual behavior

I have definied this test

class AnimalMapperTest {

    @Test
    void mapCat() {
        Cat cat = new Cat(new Details("cat"));
        Animal animal = AnimalMapper.INSTANCE.map(cat);
        assertEquals(AnimalDetails.Type.CAT, animal.getDetails().getType());
        assertEquals(cat.getDetails().getName(), animal.getDetails().getName());
    }

    @Test
    void mapDog() {
        Dog dog = new Dog(new Details("cat"));
        Animal animal = AnimalMapper.INSTANCE.map(dog);
        assertEquals(AnimalDetails.Type.DOG, animal.getDetails().getType());
        assertEquals(dog.getDetails().getName(), animal.getDetails().getName());
    }

}

There, I get this exception:

org.opentest4j.AssertionFailedError: 
Expected :CAT
Actual   :DOG
<Click to see difference>

This is because mapstruct uses this method
animal.setDetails( detailsToAnimalDetails( dog.getDetails() ) );

    protected AnimalDetails detailsToAnimalDetails(Details details) {
        if ( details == null ) {
            return null;
        }

        AnimalDetails animalDetails = new AnimalDetails();

        animalDetails.setName( details.getName() );

        animalDetails.setType( AnimalDetails.Type.DOG );

        return animalDetails;
    }

And there, it does not distinguish between Dog and Cat.

With the version 1.5.5.Final this mapping did not fail.

Steps to reproduce the problem

import lombok.Data;

@Data
public class Animal {

    private AnimalDetails details;


}
import lombok.Data;

@Data
public class AnimalDetails {
    private Type type;
    private String name;

    public enum Type {
        CAT,
        DOG
    }

}
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface AnimalMapper {

    AnimalMapper INSTANCE = Mappers.getMapper(AnimalMapper.class);

    @Mapping(target = "details.name", source = "details.name")
    @Mapping(target = "details.type", constant = "DOG")
    Animal map(Dog dog);

    @Mapping(target = "details.name", source = "details.name")
    @Mapping(target = "details.type", constant = "CAT")
    Animal map(Cat cat);
}
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AnimalMapperTest {

    @Test
    void mapCat() {
        Cat cat = new Cat(new Details("cat"));
        Animal animal = AnimalMapper.INSTANCE.map(cat);
        assertEquals(AnimalDetails.Type.CAT, animal.getDetails().getType());
        assertEquals(cat.getDetails().getName(), animal.getDetails().getName());
    }

    @Test
    void mapDog() {
        Dog dog = new Dog(new Details("cat"));
        Animal animal = AnimalMapper.INSTANCE.map(dog);
        assertEquals(AnimalDetails.Type.DOG, animal.getDetails().getType());
        assertEquals(dog.getDetails().getName(), animal.getDetails().getName());
    }

}
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Cat {

    Details details;
}

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Details {
    String name;
}
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Dog {

        Details details;
}

MapStruct Version

1.6.0

Activity

  1. jacobbetz
    added
    bug
    on Aug 16, 2024
  2. henrikhgbg commented on Aug 16, 2024

    @henrikhgbg
    henrikhgbg
    on Aug 16, 2024
    More actions

    Possibly related to issue #3667
    Both cases have multiple deep target mappings.

  3. filiphr
    added this to the 1.6.1 milestone on Aug 16, 2024
  4. filiphr commented on Aug 16, 2024

    @filiphr
    filiphr
    on Aug 16, 2024
    Member
    More actions

    Thanks @henrikhgbg. This has the same root cause as #3667. I'll try to get to the bottom of it and fix it

  5. filiphr
    added a commit that references this issue on Aug 18, 2024

    mapstruct#3667, mapstruct#3673 MappingReference should custom Mapping…

    f179d73
  6. filiphr
    self-assigned this
    on Aug 18, 2024
  7. filiphr
    added a commit that references this issue on Aug 24, 2024

    #3667, #3673 MappingReference should custom MappingOption equality in…

    6c8a2e1
  8. filiphr
    closed this as completedin #3677on Aug 24, 2024
  9. ndwlocatieservices
    added a commit that references this issue on Sep 25, 2024

    Merged PR 60999: Update all non-major dependencies

    b5e4a99
  10. ndwlocatieservices
    added a commit that references this issue on Aug 31, 2026

    Merged PR 60999: Update all non-major dependencies

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

Metadata

Metadata

Assignees

  • @filiphr
    filiphr

Labels

bug

Type

No type

Projects

No projects

    Milestone

    • 1.6.1

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.