Skip to content

Commit e5a97e0

Browse files
Removes Required Field Check for BibliographyConsistentcyCheck.check
This method was not intended to check for required fields according to its documentation. Even then, the way how it handled checking for required fields was incorrect for OrFields.
1 parent a985422 commit e5a97e0

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,15 @@ private static Set<Field> filterExcludedFields(Collection<Field> fields) {
6767
/// Filters the given entries to those that violate consistency:
6868
///
6969
/// - Fields not set (but set in other entries of the same type)
70-
/// - Required fields not set
70+
///
7171
///
7272
/// Additionally, the entries are sorted
7373
@VisibleForTesting
74-
List<BibEntry> filterAndSortEntriesWithFieldDifferences(Set<BibEntry> entries, Set<Field> differingFields, Set<Field> requiredFields) {
74+
List<BibEntry> filterAndSortEntriesWithFieldDifferences(Set<BibEntry> entries, Set<Field> differingFields) {
7575
return entries.stream()
7676
.filter(entry ->
7777
// This removes entries that have all differing fields set (could be confusing to the user)
78-
!Collections.disjoint(entry.getFields(), differingFields)
79-
// This ensures that all entries with missing required fields are included
80-
|| !entry.getFields().containsAll(requiredFields))
78+
!Collections.disjoint(entry.getFields(), differingFields))
8179
.sorted(new FieldComparatorStack<>(List.of(
8280
new BibEntryByCitationKeyComparator(),
8381
new BibEntryByFieldsComparator()
@@ -95,7 +93,7 @@ public record EntryTypeResult(Collection<Field> fields, SequencedCollection<BibE
9593
* Checks the consistency of the given entries by looking at the present and absent fields.
9694
* <p>
9795
* Computation takes place grouped by each entryType.
98-
* Computes the fields set in all entries. In case entries of the same type has more fields defined, it is output.
96+
* Computes the fields set in all entries. In case entries of the same type have more fields defined, they are output.
9997
* <p>
10098
* This class <em>does not</em> check whether all required fields are present or if the fields are valid for the entry type.
10199
* That result can a) be retrieved by using the JabRef UI and b) by checking the CSV output of {@link BibliographyConsistencyCheckResultCsvWriter#writeFindings}
@@ -138,20 +136,14 @@ public Result check(BibDatabaseContext bibContext, BiConsumer<Integer, Integer>
138136
.filter(def -> def.getType().equals(entryType))
139137
.findFirst();
140138

141-
Set<Field> requiredFields = typeDefOpt.map(typeDef ->
142-
typeDef.getRequiredFields().stream()
143-
.flatMap(orFields -> orFields.getFields().stream())
144-
.collect(Collectors.toSet())
145-
).orElse(Set.of());
146-
147139
Set<BibEntry> entries = entryTypeToEntriesMap.get(entryType);
148140
assert entries != null;
149141
if (entries == null || entries.size() <= 1 || differingFields.isEmpty()) {
150142
// entries.size == 1 can happen if there is only one entry for one type. (E.g., only one `@Book` entry)
151143
continue;
152144
}
153145

154-
List<BibEntry> sortedEntries = filterAndSortEntriesWithFieldDifferences(entries, differingFields, requiredFields);
146+
List<BibEntry> sortedEntries = filterAndSortEntriesWithFieldDifferences(entries, differingFields);
155147
if (!sortedEntries.isEmpty()) {
156148
resultMap.put(entryType, new EntryTypeResult(differingFields, sortedEntries));
157149
}

jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
import org.jabref.model.database.BibDatabaseContext;
1010
import org.jabref.model.database.BibDatabaseMode;
1111
import org.jabref.model.entry.BibEntry;
12+
import org.jabref.model.entry.BibEntryType;
13+
import org.jabref.model.entry.BibEntryTypeBuilder;
14+
import org.jabref.model.entry.BibEntryTypesManager;
15+
import org.jabref.model.entry.field.BibField;
1216
import org.jabref.model.entry.field.Field;
17+
import org.jabref.model.entry.field.FieldPriority;
1318
import org.jabref.model.entry.field.SpecialField;
1419
import org.jabref.model.entry.field.StandardField;
1520
import org.jabref.model.entry.field.UnknownField;
1621
import org.jabref.model.entry.field.UserSpecificCommentField;
22+
import org.jabref.model.entry.types.EntryType;
1723
import org.jabref.model.entry.types.StandardEntryType;
24+
import org.jabref.model.entry.types.UnknownEntryType;
1825

1926
import org.junit.jupiter.api.Test;
2027
import org.junit.jupiter.api.io.TempDir;
@@ -23,6 +30,8 @@
2330

2431
class BibliographyConsistencyCheckTest {
2532

33+
private static final EntryType CUSTOM_TYPE = new UnknownEntryType("customType");
34+
2635
@Test
2736
void checkSimpleLibrary(@TempDir Path tempDir) {
2837
BibEntry first = new BibEntry(StandardEntryType.Article, "first")
@@ -232,14 +241,14 @@ void checkFieldEntriesWithFieldDifferences() {
232241
StandardField.TITLE,
233242
StandardField.PAGES,
234243
new UnknownField("customField"),
235-
StandardField.PUBLISHER
244+
StandardField.PUBLISHER,
245+
StandardField.PDF
236246
);
237247
List<BibEntry> result = new BibliographyConsistencyCheck().filterAndSortEntriesWithFieldDifferences(
238248
Set.of(entry1, entry2, entry3, entry4, entry5),
239-
differingFields,
240-
Set.of(StandardField.AUTHOR, StandardField.TITLE, StandardField.PAGES, StandardField.PDF));
249+
differingFields);
241250

242-
assertEquals(List.of(entry1, entry2, entry3, entry4, entry5), result);
251+
assertEquals(List.of(entry1, entry3, entry4, entry5), result);
243252
}
244253

245254
@Test
@@ -279,4 +288,31 @@ void checkComplexLibraryWithAdditionalEntry(@TempDir Path tempDir) {
279288
));
280289
assertEquals(expectedResult, actualResult);
281290
}
291+
292+
@Test
293+
void checkCustomEntryTypes() {
294+
295+
BibEntry first = new BibEntry(StandardEntryType.Article, "first")
296+
.withField(StandardField.AUTHOR, "Author One")
297+
.withField(StandardField.PAGES, "some pages");
298+
BibEntry second = new BibEntry(StandardEntryType.Article, "second")
299+
.withField(StandardField.AUTHOR, "Author One")
300+
.withField(StandardField.PUBLISHER, "publisher");
301+
BibDatabase database = new BibDatabase(List.of(first, second));
302+
BibDatabaseContext bibContext = new BibDatabaseContext(database);
303+
bibContext.setMode(BibDatabaseMode.BIBTEX);
304+
305+
BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
306+
BibEntryType customEntryType = new BibEntryType(CUSTOM_TYPE,
307+
List.of(new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT), new BibField(StandardField.ABSTRACT, FieldPriority.IMPORTANT)),
308+
Set.of());
309+
entryTypesManager.addCustomOrModifiedType(customEntryType, bibContext.getMode());
310+
311+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
312+
});
313+
314+
BibliographyConsistencyCheck.EntryTypeResult entryTypeResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER), List.of(first, second));
315+
BibliographyConsistencyCheck.Result expected = new BibliographyConsistencyCheck.Result(Map.of(StandardEntryType.Article, entryTypeResult));
316+
assertEquals(expected, result);
317+
}
282318
}

0 commit comments

Comments
 (0)