Skip to content

@Default on Java Record's constructor isn't respected #3231

@AlexElin

Description

@AlexElin

Expected behavior

Record's constructor having @Default is called in the generated code.

Actual behavior

Record's constructor having @Default isn't called in the generated code.
The generated code is below.

class TaskMapperImpl implements TaskMapper {

    @Override
    public Task from(TaskDto testDbRecord) {
        if ( testDbRecord == null ) {
            return null;
        }

        String id = null;
        Long number = null;

        id = testDbRecord.id();
        number = testDbRecord.number();

        Task task = new Task( id, number );

        return task;
    }
}

Steps to reproduce the problem

  1. Configure the maven compiler plugin with the settings below (it's taken from docs)
  2. mvn clean compile for the below code snippets
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <release>19</release>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.5.3.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
record Task(String id, Long number) {
    @Default
    Task(String id) {
        this(id, 1L);
    }
}
record TaskDto(String id, Long number) {
}
@Mapper
interface TaskMapper {
    Task from(TaskDto testDbRecord);
}

MapStruct Version

1.5.3.Final

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions