MapStruct ignores primitive parameters when all non-primitive parameters are null due to generated null-check logic
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện vấn đề từ chữ ký mapper và logic được sinh ra như mô tả trong báo cáo bằng MapStruct 1.6.3. Theo dõi việc sinh các kiểm tra null cho các ánh xạ nhiều tham số, sau đó xác minh rằng các phương thức được sinh ra vẫn tạo và điền một đích khi các tham số tham chiếu là null nhưng các tham số nguyên thủy có giá trị.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Expected behavior
MapStruct generates a null-check that considers only non-primitive method parameters. When all non-primitive arguments are null, the generated mapper method returns null early, even if primitive parameters contain valid values.
As a result, mappings for primitive (or boxed-with-default) arguments are skipped entirely.
Expected behavior
If primitive parameters (e.g. int, boolean) have valid values, MapStruct should still map them to the target object, even when all non-primitive parameters are null.
At minimum, primitive arguments should not be ignored solely because non-primitive arguments are null.
Actual behavior
MapStruct generates an early return null when all non-primitive parameters are null, causing primitive argument mappings to be ignored.
Example
Mapper method:
Target map(
String sku,
String name,
Long categoryId,
int size,
int pageNumber,
boolean draft
);
Generated logic (simplified):
if (sku == null && name == null && categoryId == null) {
return null;
}
target.setSize(size);
target.setPageNumber(pageNumber);
target.setDraft(draft);
In this case:
- sku, name, and categoryId are null
- size, pageNumber, and draft have valid values
However, the method returns null, and primitive fields are never mapped.
Impact
- This behavior makes it impossible to create partially populated target objects when:
- all reference-type parameters are null
- primitive parameters still carry meaningful values (e.g. pagination, flags)
- This is especially problematic for search/filter DTOs.
Workaround
- Convert primitive parameters to wrapper types (Integer, Boolean)
- Or manually implement mapping logic instead of relying on generated code
Actual behavior
No response
Steps to reproduce the problem
I have told in description
MapStruct Version
1.6.3
- 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
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
Tất cả issue của mapstruct/mapstruct
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
-
Two open-case totals on one screen: the Programs tile says 15,858 and the nav badge says 15,868 Đang mởbug frontend maui-pilot
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
objectionary/eo-graphs#74 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100