Merge pull request #1550 from hapifhir/do-20230119-fix-filetype-detect-in-service
Fix file type inference in ValidationService
This commit is contained in:
commit
c34f439da5
|
@ -110,39 +110,29 @@ public class ValidationService {
|
||||||
fileToValidate.getFileContent().getBytes(),
|
fileToValidate.getFileContent().getBytes(),
|
||||||
fileToValidate.getFileName(),
|
fileToValidate.getFileName(),
|
||||||
false);
|
false);
|
||||||
fileToValidate.setFileType(format.getExtension());
|
if (format != null) {
|
||||||
|
fileToValidate.setFileType(format.getExtension());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ValidationMessage> messages = new ArrayList<>();
|
List<ValidationMessage> messages = new ArrayList<>();
|
||||||
|
|
||||||
ValidatedFragments validatedFragments = validator.validateAsFragments(fileToValidate.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fileToValidate.getFileType()),
|
if (fileToValidate.getFileType() == null) {
|
||||||
request.getCliContext().getProfiles(), messages);
|
ValidationOutcome outcome = getValidationOutcomeForUnknownFileFormat(
|
||||||
|
new FileInfo(fileToValidate.getFileName(), fileToValidate.getFileContent(), null));
|
||||||
if (validatedFragments.getValidatedFragments().size() == 1 && !validatedFragments.getValidatedFragments().get(0).isDerivedContent()) {
|
|
||||||
ValidatedFragment validatedFragment = validatedFragments.getValidatedFragments().get(0);
|
|
||||||
ValidationOutcome outcome = new ValidationOutcome();
|
|
||||||
FileInfo fileInfo = new FileInfo(
|
|
||||||
fileToValidate.getFileName(),
|
|
||||||
new String(validatedFragment.getContent()),
|
|
||||||
validatedFragment.getExtension());
|
|
||||||
outcome.setMessages(validatedFragment.getErrors());
|
|
||||||
outcome.setFileInfo(fileInfo);
|
|
||||||
response.addOutcome(outcome);
|
|
||||||
} else {
|
|
||||||
for (ValidatedFragment validatedFragment : validatedFragments.getValidatedFragments()) {
|
|
||||||
ValidationOutcome outcome = new ValidationOutcome();
|
|
||||||
FileInfo fileInfo = new FileInfo(
|
|
||||||
validatedFragment.getFilename(),
|
|
||||||
new String(validatedFragment.getContent()),
|
|
||||||
validatedFragment.getExtension());
|
|
||||||
outcome.setMessages(validatedFragment.getErrors());
|
|
||||||
outcome.setFileInfo(fileInfo);
|
|
||||||
response.addOutcome(outcome);
|
response.addOutcome(outcome);
|
||||||
}
|
} else {
|
||||||
}
|
ValidatedFragments validatedFragments = validator.validateAsFragments(fileToValidate.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fileToValidate.getFileType()),
|
||||||
|
request.getCliContext().getProfiles(), messages);
|
||||||
|
|
||||||
if (request.getCliContext().isShowTimes()) {
|
List<ValidationOutcome> validationOutcomes = getValidationOutcomesFromValidatedFragments(fileToValidate, validatedFragments);
|
||||||
response.getValidationTimes().put(fileToValidate.getFileName(), validatedFragments.getValidationTime());
|
for (ValidationOutcome validationOutcome : validationOutcomes) {
|
||||||
|
response.addOutcome(validationOutcome);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getCliContext().isShowTimes()) {
|
||||||
|
response.getValidationTimes().put(fileToValidate.getFileName(), validatedFragments.getValidationTime());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,6 +140,45 @@ public class ValidationService {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<ValidationOutcome> getValidationOutcomesFromValidatedFragments(FileInfo fileToValidate, ValidatedFragments validatedFragments) {
|
||||||
|
List<ValidationOutcome> outcomes = new LinkedList<>();
|
||||||
|
if (validatedFragments.getValidatedFragments().size() == 1 && !validatedFragments.getValidatedFragments().get(0).isDerivedContent()) {
|
||||||
|
ValidatedFragment validatedFragment = validatedFragments.getValidatedFragments().get(0);
|
||||||
|
ValidationOutcome outcome = new ValidationOutcome();
|
||||||
|
FileInfo fileInfo = new FileInfo(
|
||||||
|
fileToValidate.getFileName(),
|
||||||
|
new String(validatedFragment.getContent()),
|
||||||
|
validatedFragment.getExtension());
|
||||||
|
outcome.setMessages(validatedFragment.getErrors());
|
||||||
|
outcome.setFileInfo(fileInfo);
|
||||||
|
outcomes.add(outcome);
|
||||||
|
} else {
|
||||||
|
for (ValidatedFragment validatedFragment : validatedFragments.getValidatedFragments()) {
|
||||||
|
ValidationOutcome outcome = new ValidationOutcome();
|
||||||
|
FileInfo fileInfo = new FileInfo(
|
||||||
|
validatedFragment.getFilename(),
|
||||||
|
new String(validatedFragment.getContent()),
|
||||||
|
validatedFragment.getExtension());
|
||||||
|
outcome.setMessages(validatedFragment.getErrors());
|
||||||
|
outcome.setFileInfo(fileInfo);
|
||||||
|
outcomes.add(outcome);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return outcomes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ValidationOutcome getValidationOutcomeForUnknownFileFormat(FileInfo fileInfo) {
|
||||||
|
ValidationOutcome outcome = new ValidationOutcome();
|
||||||
|
|
||||||
|
List<ValidationMessage> errorList = new ArrayList<>() {{
|
||||||
|
add(new ValidationMessage().setType(ValidationMessage.IssueType.EXCEPTION).setLevel(ValidationMessage.IssueSeverity.FATAL).setMessage("Unable to infer format from file. Please check that your file is in a valid FHIR format."));
|
||||||
|
|
||||||
|
} };
|
||||||
|
outcome.setMessages(errorList);
|
||||||
|
outcome.setFileInfo(fileInfo);
|
||||||
|
return outcome;
|
||||||
|
}
|
||||||
|
|
||||||
public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception {
|
public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception {
|
||||||
VersionSourceInformation versions = new VersionSourceInformation();
|
VersionSourceInformation versions = new VersionSourceInformation();
|
||||||
IgLoader igLoader = new IgLoader(
|
IgLoader igLoader = new IgLoader(
|
||||||
|
|
Loading…
Reference in New Issue