Optional<Map> / Optional<Collection> source property generates unguarded Optional#get, throwing NoSuchElementException when empty
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Tái hiện vấn đề với Source.java, Target.java, SourceMapper.java và Demo.java bằng lệnh javac đã cho, sau đó kiểm tra SourceMapperImpl.java được tạo ra. Theo dõi việc xử lý map và collection so với việc xử lý scalar có guard; được xem là hoàn tất khi các giá trị Optional và Optional rỗng được map mà không xảy ra NoSuchElementException và phần coverage hồi quy liên quan đều đạt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Expected behavior
When a source property is Optional<T>, the generated mapper should read the value only when it is present — as it already does for scalar types:
if ( source.getName().isPresent() ) {
name = source.getName().get();
}
Actual behavior
For Optional<Map<K, V>> and Optional<List<E>> source properties the presence check is omitted and Optional#get is called unconditionally, so mapping a source whose Optional is empty throws NoSuchElementException.
Generated mapper (note the scalar property in the same method is guarded):
@Override
public Target toTarget(Source source) {
if ( source == null ) {
return null;
}
String name = null;
Map<String, String> attributes = null;
List<String> tags = null;
if ( source.getName().isPresent() ) {
name = source.getName().get();
}
attributes = source.getAttributes().get(); // no presence check
tags = source.getTags().get(); // no presence check
Target target = new Target( name, attributes, tags );
return target;
}
Runtime, mapping a Source whose three properties are all Optional.empty():
Exception in thread "main" java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at repro.SourceMapperImpl.toTarget(SourceMapperImpl.java:27)
at Demo.main(Demo.java:9)
The inconsistency between the scalar and the map/collection properties in the same generated method suggests the presence check is lost on the branch that handles map and collection types.
This makes Optional-returning getters unusable in practice for map and collection properties, since an absent value is exactly the case Optional exists to express.
Related to #3976, but that one concerns get() vs orElseThrow(); here the presence check is missing altogether.
Steps to reproduce the problem
Source.java:
package repro;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Source
{
private final Optional<String> name;
private final Optional<Map<String, String>> attributes;
private final Optional<List<String>> tags;
public Source( Optional<String> name, Optional<Map<String, String>> attributes, Optional<List<String>> tags )
{
this.name = name;
this.attributes = attributes;
this.tags = tags;
}
public Optional<String> getName()
{
return name;
}
public Optional<Map<String, String>> getAttributes()
{
return attributes;
}
public Optional<List<String>> getTags()
{
return tags;
}
}
Target.java:
package repro;
import java.util.List;
import java.util.Map;
public record Target( String name, Map<String, String> attributes, List<String> tags )
{ }
SourceMapper.java:
package repro;
import org.mapstruct.Mapper;
@Mapper
public interface SourceMapper
{
Target toTarget( Source source );
}
Demo.java:
import java.util.Optional;
import repro.*;
public class Demo
{
public static void main( String[] args )
{
Source source = new Source( Optional.empty(), Optional.empty(), Optional.empty() );
System.out.println( new SourceMapperImpl().toTarget( source ) );
}
}
Compiled with plain javac (no build tool, no other annotation processors):
javac -cp mapstruct-1.7.0.Beta2.jar \
-processorpath mapstruct-processor-1.7.0.Beta2.jar:mapstruct-1.7.0.Beta2.jar \
-s gen -d out src/repro/*.java
Also reproduces with nullValueMapMappingStrategy = RETURN_NULL and with nullValueCheckStrategy = ALWAYS.
MapStruct Version
1.7.0.Beta2, Java 25.0.4 (Eclipse Adoptium), javac.
- Ngôn ngữ chính
- Java
- Star
- 7.7k
- Fork
- 1.1k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của mapstruct/mapstruct
-
bug test
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 68/100
-
bug JSpecify
-
JSpecify
Tất cả issue của mapstruct/mapstruct
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
elastic/gradle-plugins#157 ·
-
enhancement Tools
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/rocketmq-dashboard#5008 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
DETECT_PARAMETER_NAMES=false silently disables @ConstructorProperties-based Creator detection too Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
FasterXML/jackson-databind#6229 ·