Skip to content

Commit

Permalink
Fix metadata serialization order (#5514)
Browse files Browse the repository at this point in the history
The save actions and/or customized entry types were not serialized alphabetically. This lead to constant changes when used with version control systems.
  • Loading branch information
tobiasdiez authored Oct 25, 2019
1 parent fdd06d7 commit b8d00f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed a few problems that prevented JabFox to communicate with JabRef. [#4737](https://github.com/JabRef/jabref/issues/4737) [#4303](https://github.com/JabRef/jabref/issues/4303)
- We fixed an error where the groups containing an entry loose their highlight color when scrolling. [#5022](https://github.com/JabRef/jabref/issues/5022)
- We fixed an error where an exception was thrown when merging entries. [#5169](https://github.com/JabRef/jabref/issues/5169)
- We fixed an error where certain metadata items were not serialized alphabetically.
- After assigning an entry to a group, the item count is now properly colored to reflect the new membership of the entry. [#3112](https://github.com/JabRef/jabref/issues/3112)
- The group panel is now properly updated when switching between libraries (or when closing/opening one). [#3142](https://github.com/JabRef/jabref/issues/3142)
- We fixed an error where the number of matched entries shown in the group pane was not updated correctly. [#4441](https://github.com/JabRef/jabref/issues/4441)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.TreeMap;

import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -91,7 +92,7 @@ public List<String> getAsStringList(String newline) {

private static String getMetaDataString(List<FieldFormatterCleanup> actionList, String newline) {
//first, group all formatters by the field for which they apply
Map<Field, List<String>> groupedByField = new HashMap<>();
Map<Field, List<String>> groupedByField = new TreeMap<>(Comparator.comparing(Field::getName));
for (FieldFormatterCleanup cleanup : actionList) {
Field key = cleanup.getField();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.model.entry;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.SortedSet;
Expand All @@ -10,6 +11,7 @@

import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.field.BibField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.types.BiblatexEntryTypeDefinitions;
import org.jabref.model.entry.types.BibtexEntryTypeDefinitions;
Expand Down Expand Up @@ -56,7 +58,12 @@ public static String serialize(BibEntryType entryType) {
builder.append(": req[");
builder.append(FieldFactory.serializeOrFieldsList(entryType.getRequiredFields()));
builder.append("] opt[");
builder.append(FieldFactory.serializeFieldsList(entryType.getOptionalFields().stream().map(BibField::getField).collect(Collectors.toSet())));
builder.append(FieldFactory.serializeFieldsList(
entryType.getOptionalFields()
.stream()
.map(BibField::getField)
.sorted(Comparator.comparing(Field::getName))
.collect(Collectors.toList())));
builder.append("]");
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Scanner;

import org.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import org.jabref.logic.formatter.casechanger.TitleCaseFormatter;
import org.jabref.logic.formatter.casechanger.UpperCaseFormatter;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
Expand Down Expand Up @@ -260,8 +262,17 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception {
EntryType customizedType = new UnknownEntryType("customizedType");
BibEntryType customizedBibType = new BibEntryType(
customizedType,
Arrays.asList(new BibField(StandardField.TITLE, FieldPriority.IMPORTANT), new BibField(StandardField.YEAR, FieldPriority.IMPORTANT)),
Collections.singleton(new OrFields(StandardField.TITLE)));
Arrays.asList(
new BibField(StandardField.TITLE, FieldPriority.IMPORTANT),
new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT),
new BibField(StandardField.DATE, FieldPriority.IMPORTANT),
new BibField(StandardField.YEAR, FieldPriority.IMPORTANT),
new BibField(StandardField.MONTH, FieldPriority.IMPORTANT),
new BibField(StandardField.PUBLISHER, FieldPriority.IMPORTANT)),
Arrays.asList(
new OrFields(StandardField.TITLE),
new OrFields(StandardField.AUTHOR),
new OrFields(StandardField.DATE)));
entryTypesManager.addCustomizedEntryType(customizedBibType, BibDatabaseMode.BIBTEX);
BibEntry entry = new BibEntry(customizedType);
database.insertEntry(entry);
Expand All @@ -273,7 +284,7 @@ void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws Exception {
"@Customizedtype{," + OS.NEWLINE + "}" + OS.NEWLINE + OS.NEWLINE
+ "@Comment{jabref-meta: databaseType:bibtex;}"
+ OS.NEWLINE + OS.NEWLINE
+ "@Comment{jabref-entrytype: customizedtype: req[title] opt[year]}" + OS.NEWLINE,
+ "@Comment{jabref-entrytype: customizedtype: req[author;date;title] opt[month;publisher;year]}" + OS.NEWLINE,
stringWriter.toString());
}

Expand Down Expand Up @@ -433,13 +444,23 @@ void reformatStringIfAskedToDoSo() throws Exception {
@Test
void writeSaveActions() throws Exception {
FieldFormatterCleanups saveActions = new FieldFormatterCleanups(true,
Collections.singletonList(new FieldFormatterCleanup(StandardField.TITLE, new LowerCaseFormatter())));
Arrays.asList(
new FieldFormatterCleanup(StandardField.TITLE, new LowerCaseFormatter()),
new FieldFormatterCleanup(StandardField.JOURNAL, new TitleCaseFormatter()),
new FieldFormatterCleanup(StandardField.DAY, new UpperCaseFormatter())));
metaData.setSaveActions(saveActions);

databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList());

assertEquals(OS.NEWLINE + "@Comment{jabref-meta: saveActions:enabled;" + OS.NEWLINE
+ "title[lower_case]" + OS.NEWLINE + ";}" + OS.NEWLINE, stringWriter.toString());
assertEquals(
OS.NEWLINE +
"@Comment{jabref-meta: saveActions:enabled;"
+ OS.NEWLINE
+ "day[upper_case]" + OS.NEWLINE
+ "journal[title_case]" + OS.NEWLINE
+ "title[lower_case]" + OS.NEWLINE
+ ";}"
+ OS.NEWLINE, stringWriter.toString());
}

@Test
Expand Down

0 comments on commit b8d00f2

Please sign in to comment.