diff --git a/CHANGELOG.md b/CHANGELOG.md index ca43060f94d..4a3c66e2ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,6 +102,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue where the tab showing the fulltext search results would appear blank after switching library. [#13241](https://github.com/JabRef/jabref/issues/13241) - We fixed an issue where the groups were still displayed after closing all libraries. [#13382](https://github.com/JabRef/jabref/issues/13382) - Enhanced field selection logic in the Merge Entries dialog when fetching from DOI to prefer valid years and entry types. [#12549](https://github.com/JabRef/jabref/issues/12549) +- We fixed an issue where the merge dialog throws an exception upon trying to load an entry with an invalid year field. [#13673](https://github.com/JabRef/jabref/issues/13673) ### Removed diff --git a/jablib/src/main/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparator.java b/jablib/src/main/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparator.java index 5a9b48195ce..7d2b07e46b1 100644 --- a/jablib/src/main/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparator.java +++ b/jablib/src/main/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparator.java @@ -29,6 +29,10 @@ public ComparisonResult compare(String leftValue, String rightValue) { Optional leftYear = extractYear(leftValue); Optional rightYear = extractYear(rightValue); + if (leftYear.isEmpty() || rightYear.isEmpty()) { + return checkEmptyValues(leftYear, rightYear); + } + boolean leftYearInRange = (leftYear.get() >= 1800) && (leftYear.get() <= Year.now().getValue() + 2); if (leftYearInRange) { @@ -59,4 +63,21 @@ private Optional extractYear(String value) { } return Optional.empty(); } + + /** + * Prevents the years from being empty, so the following logic has non empty values + * + * @param leftValue year from the library (or candidate) + * @param rightValue year from the fetcher (or existing record) + * @return ComparisonResult depending on which value is empty: RIGHT_BETTER, LEFT_BETTER, or UNDETERMINED + */ + private ComparisonResult checkEmptyValues(Optional leftValue, Optional rightValue) { + if (leftValue.isEmpty() && rightValue.isEmpty()) { + return ComparisonResult.UNDETERMINED; + } else if (leftValue.isEmpty()) { + return ComparisonResult.RIGHT_BETTER; + } else { + return ComparisonResult.LEFT_BETTER; + } + } } diff --git a/jablib/src/test/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparatorTest.java b/jablib/src/test/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparatorTest.java new file mode 100644 index 00000000000..1b957f5227f --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparatorTest.java @@ -0,0 +1,23 @@ +package org.jabref.logic.bibtex.comparator; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class YearFieldValuePlausibilityComparatorTest { + + private final YearFieldValuePlausibilityComparator comparator = new YearFieldValuePlausibilityComparator(); + @Test + void compareEmptyValuesTest() { + String emptyString = ""; + String validYearString = "1999"; + + ComparisonResult leftRight = ComparisonResult.LEFT_BETTER; + ComparisonResult rightRight = ComparisonResult.RIGHT_BETTER; + ComparisonResult undetermined = ComparisonResult.UNDETERMINED; + + assertEquals(rightRight, comparator.compare(emptyString, validYearString)); + assertEquals(leftRight, comparator.compare(validYearString, emptyString)); + assertEquals(undetermined, comparator.compare(emptyString, emptyString)); + } +}