Skip to content

Commit b158bbd

Browse files
authored
Handle null JSON extension gracefully (#6814)
* Handle null extension JSON value gracefully * Better handling * Spotless
1 parent f96d707 commit b158bbd

File tree

4 files changed

+762
-696
lines changed

4 files changed

+762
-696
lines changed

hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import ca.uhn.fhir.rest.api.EncodingEnum;
5656
import ca.uhn.fhir.util.ElementUtil;
5757
import ca.uhn.fhir.util.FhirTerser;
58+
import jakarta.annotation.Nonnull;
5859
import org.apache.commons.lang3.StringUtils;
5960
import org.apache.commons.lang3.Validate;
6061
import org.apache.commons.text.WordUtils;
@@ -1474,17 +1475,19 @@ private void parseExtension(ParserState<?> theState, BaseJsonLikeArray theValues
14741475
int allUnderscoreNames = 0;
14751476
int handledUnderscoreNames = 0;
14761477

1478+
if (theValues == null) {
1479+
String parentElementName = getExtensionElementName(theIsModifier);
1480+
getErrorHandler()
1481+
.missingRequiredElement(new ParseLocation().setParentElementName(parentElementName), "url");
1482+
return;
1483+
}
1484+
14771485
for (int i = 0; i < theValues.size(); i++) {
14781486
BaseJsonLikeObject nextExtObj = BaseJsonLikeValue.asObject(theValues.get(i));
14791487
BaseJsonLikeValue jsonElement = nextExtObj.get("url");
14801488
String url;
14811489
if (null == jsonElement || !(jsonElement.isScalar())) {
1482-
String parentElementName;
1483-
if (theIsModifier) {
1484-
parentElementName = "modifierExtension";
1485-
} else {
1486-
parentElementName = "extension";
1487-
}
1490+
String parentElementName = getExtensionElementName(theIsModifier);
14881491
getErrorHandler()
14891492
.missingRequiredElement(new ParseLocation().setParentElementName(parentElementName), "url");
14901493
url = null;
@@ -1553,6 +1556,17 @@ private void parseExtension(ParserState<?> theState, BaseJsonLikeArray theValues
15531556
}
15541557
}
15551558

1559+
@Nonnull
1560+
private static String getExtensionElementName(boolean theIsModifier) {
1561+
String parentElementName;
1562+
if (theIsModifier) {
1563+
parentElementName = "modifierExtension";
1564+
} else {
1565+
parentElementName = "extension";
1566+
}
1567+
return parentElementName;
1568+
}
1569+
15561570
private void parseFhirComments(BaseJsonLikeValue theObject, ParserState<?> theState) {
15571571
if (isSupportsFhirComment()) {
15581572
if (theObject.isArray()) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
type: add
3+
issue: 6814
4+
title: "When parsing a JSON resource, the following fragment caused the
5+
parser to abort parsing with an unhelpful error message:
6+
`\"extension\": null`. We will now correctly detect this error and pass
7+
it to the parser error handler."

0 commit comments

Comments
 (0)