NullValuePropertyMappingStrategy.IGNORE strategy ignored for Optional fields
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 68/100
Direzione di ricerca
Inizia riproducendo il mapper generato per il campo Optional con NullValuePropertyMappingStrategy.IGNORE e ispeziona il percorso di gestione di null che produce l’implementazione mostrata. Il lavoro è completato quando il codice generato preserva il valore di destinazione esistente per una sorgente Optional null continuando a mappare Optional.empty() e i valori presenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Expected behavior
The NullValuePropertyMappingStrategy.IGNORE strategy should be applied to Optional fields in exactly the same way as it is applied to other field types. Its behavior is clearly defined in the Javadoc:
If a source bean property equals null the target bean property will be ignored and retain its existing value.
Instead, NullValuePropertyMappingStrategy.IGNORE is effectively ignored for Optional fields. This also doesn't align with common JSON mapping semantics, where an absent field is ignored (i.e. nothing is done), while an explicitly provided null value is mapped to Optional.empty().
This approach is often used to cover patch semantics during mapping. A patch needs to distinguish between three states, which is why Optional is used:
null- ignore the field and keep its existing valueOptional.empty()- clear the field by setting it to nullOptional.of(value)- update the field with the wrapped value
Previously, NullValuePropertyMappingStrategy.IGNORE did exactly what was expected: it applied the necessary null checks and preserved the distinction between an absent field and an explicitly provided empty value.
Quite a few production systems are already built around these semantics. The recent changes therefore introduce breaking changes for existing applications that rely on this distinction.
Actual behavior
The NullValuePropertyMappingStrategy.IGNORE strategy is simply ignored, and no null check is generated:
Steps to reproduce the problem
- Create source and target objects with
Optionalfield:
public class Source {
private Optional<String> foo;
public Optional<String> getFoo() { return foo; }
public void setFoo(Optional<String> foo) { this.foo = foo; }
}
public class Target {
private Optional<String> foo;
public Optional<String> getFoo() { return foo; }
public void setFoo(Optional<String> foo) { this.foo = foo; }
}
- Create mapper to map source to target:
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface InvalidMapper {
public static final InvalidMapper INSTANCE = Mappers.getMapper(InvalidMapper.class);
public Target map(Source source);
}
- Check generated mapper implementation:
@Override
public Target map(Source source) {
if ( source == null ) {
return null;
}
Target target = new Target();
target.setFoo( source.getFoo() );
return target;
}
No null checks are applied
MapStruct Version
1.7.0.Beta2
- Lingua principale
- Java
- Stelle
- 7.7k
- Fork
- 1.1k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di mapstruct/mapstruct
-
bug test
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
-
bug JSpecify
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
-
JSpecify
Tutte le issue di mapstruct/mapstruct
Issue simili
-
executions.Query — startDate and timeRange filters are sent with inverted comparison operators Apertaarea/plugin
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
kestra-io/plugin-kestra#190 ·
-
litertlm-android AAR ships no consumer ProGuard rules → "mid == null" SIGABRT in minified apps Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
apache/rocketmq-dashboard#5064 ·