Skip to content

Commit

Permalink
fix: invalid FHIR id for Bundle and DiagnosticReport
Browse files Browse the repository at this point in the history
  • Loading branch information
jabberwoc committed Jul 31, 2024
1 parent 58493b1 commit 673d29c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public Bundle apply(LaboratoryReport report) {
Bundle bundle = new Bundle();
try {

// set meta information
bundle.setId(report.getReportIdentifierValue());
// set meta information (with valid id)
bundle.setId(report.getValidReportId());
bundle.setType(BundleType.TRANSACTION);

processMetaResults(report)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,28 @@ public void setResource(DiagnosticReport resource) {
}

private void sanitizeIdentifierValue(DiagnosticReport resource) {
var identifierValue = resource.getIdentifierFirstRep().getValue();
var identifierValue = resource.getIdentifierFirstRep()
.getValue();
var idPart =
StringUtils.substringAfterLast(identifierValue, "SWISSLAB_");
if (StringUtils.isNotBlank(idPart)) {
resource.getIdentifierFirstRep().setValue(idPart);
resource.getIdentifierFirstRep()
.setValue(idPart);
}
}

public String getReportIdentifierValue() {
return resource.getIdentifierFirstRep().getValue();
return resource.getIdentifierFirstRep()
.getValue();
}

@SuppressWarnings("checkstyle:MagicNumber")
public String getValidReportId() {
// replace invalid characters with "-"
var replaced =
getReportIdentifierValue().replaceAll("[^A-Za-z0-9\\-\\.]", "-");
// max 64 characters
return replaced.substring(0, Math.min(replaced.length(), 64));
}

public String getMetaCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,33 @@ void setResourceStripsIdentifierPrefix() {

assertThat(labReport.getReportIdentifierValue()).isEqualTo(expectedId);
}

@Test
void getValidReportIdReplacesInvalidCharacters() {
var source = new DiagnosticReport().addIdentifier(
new Identifier().setValue("SWISSLAB_20240710_123456"));
var expectedId = "20240710-123456";

var labReport = new LaboratoryReport();
labReport.setResource(source);

assertThat(labReport.getValidReportId()).isEqualTo(expectedId);
}

@Test
void getValidReportIdTrimsByMaxLength() {
var source =
new DiagnosticReport().addIdentifier(new Identifier().setValue(
// 70 characters
"111111111122222222223333333333444444444455555555556666666666"
+ "7777777777"));
// only 64 characters are valid
var expectedId =
"1111111111222222222233333333334444444444555555555566666666667777";

var labReport = new LaboratoryReport();
labReport.setResource(source);

assertThat(labReport.getValidReportId()).isEqualTo(expectedId);
}
}

0 comments on commit 673d29c

Please sign in to comment.