mapstruct / mapstruct/mapstruct

Decorator not working with object references

Open
#781 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi,

I want to user spring decorator for reference object.
It is a bug or I have something wrong in my code but problem is that when using decorator for Dto generated code call base mapping method (InspectionResultMapper2.tireToTireDto2(tire) instead my decorated method (InspectionResultMapperDecorator2.tireToTireDto2).

Example dto and model with problem

```java
public class InspectionDto2 {
private Long id;

private TireDto2 tire;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public TireDto2 getTire() {
return tire;
}

public void setTire(TireDto2 tire) {
this.tire = tire;
}
}
```

```java
public class TireDto2 {

private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
```

Model:

```java
public class Inspection implements Serializable {

private Tire tire;

public Tire getTire() {
return tire;
}

public void setTire(Tire tire) {
this.tire = tire;
}
}
```

```java
public class Tire implements Serializable {
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
```

Mapping condig:

```java
@Mapper(componentModel = "spring")
@DecoratedWith(InspectionResultMapperDecorator2.class)
public interface InspectionResultMapper2 {

InspectionDto2 InspectionToInspectionDto2(Inspection inspection);
TireDto2 tireToTireDto2(Tire tire);
}
```

```java
public abstract class InspectionResultMapperDecorator2 implements InspectionResultMapper2 {

@Autowired
@Qualifier("delegate")
private InspectionResultMapper2 delegate;

@Override
public TireDto2 tireToTireDto2(Tire tire) {
//TireDto tireDto = delegate.tireToTireDto(tire);
TireDto2 tireDto = new TireDto2();
tireDto.setId(222L);
return tireDto;
}
}
```

Generated code is:

```java
@Component
@Qualifier("delegate")
public class InspectionResultMapper2Impl_ implements InspectionResultMapper2 {

@Override
public InspectionDto2 InspectionToInspectionDto2(Inspection inspection) {
if ( inspection == null ) {
return null;
}

InspectionDto2 inspectionDto2 = new InspectionDto2();

inspectionDto2.setId( inspection.getId() );
/* HERE CALL UNDECORADER METHOD from InspectionResultMapper2 */
inspectionDto2.setTire( tireToTireDto2( inspection.getTire()) );

return inspectionDto2;
}

@Override
public TireDto2 tireToTireDto2(Tire tire) {
if ( tire == null ) {
return null;
}

TireDto2 tireDto2_ = new TireDto2();

tireDto2_.setId( tire.getId() );

return tireDto2_;
}
}
```

When I map Inspection with method "InspectionResultMapper2.inspectionToInspectionDto2" my decorator is not called. In generated code mapper call base method (see comment).
It is possible to use decorator for reference object ?

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 from the generated InspectionResultMapper2Impl_ shown in the report and the InspectionResultMapper2/InspectionResultMapperDecorator2 mapping declarations. Reproduce the Inspection-to-InspectionDto2 mapping and trace how the nested tire mapping is selected; done means the nested call reaches the decorated tireToTireDto2 method and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.