fix(api): handle Author sort name generation above field length (#2276)
This commit is contained in:
@@ -26,7 +26,7 @@ public class AuthorEntity {
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "sort_name")
|
||||
@Column(name = "sort_name", length = 255)
|
||||
private String sortName;
|
||||
|
||||
@Column(name = "sort_name_locked", nullable = false)
|
||||
@@ -60,6 +60,12 @@ public class AuthorEntity {
|
||||
public void computeSortName() {
|
||||
if (!sortNameLocked) {
|
||||
sortName = AuthorSortName.compute(name);
|
||||
|
||||
if (sortName != null && sortName.length() > 255) {
|
||||
// The sort name MAY end up longer than 255 characters and hit
|
||||
// limits.
|
||||
sortName = sortName.substring(0, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -44,9 +44,7 @@ public class PopulateAuthorSortNameMigration implements Migration {
|
||||
}
|
||||
|
||||
for (var author : authors) {
|
||||
if (!author.isSortNameLocked()) {
|
||||
author.setSortName(AuthorSortName.compute(author.getName()));
|
||||
}
|
||||
author.computeSortName();
|
||||
}
|
||||
|
||||
authorRepository.saveAll(authors);
|
||||
|
||||
@@ -59,4 +59,21 @@ class AuthorEntityTest {
|
||||
|
||||
assertThat(author.getSortName()).isEqualTo("Orwell, George");
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorSortNameTruncation() {
|
||||
AuthorEntity author = AuthorEntity.builder()
|
||||
.name(
|
||||
"George Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell" +
|
||||
"Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell" +
|
||||
"Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell" +
|
||||
"Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell Orwell"
|
||||
)
|
||||
.build();
|
||||
|
||||
author.computeSortName();
|
||||
|
||||
assertThat(author.getSortName()).isNotNull();
|
||||
assertThat(author.getSortName()).hasSizeLessThan(256);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user