improve error messages in json parser

This commit is contained in:
Grahame Grieve 2022-10-29 11:59:00 +11:00
parent 27511f40dd
commit ddf769851b
2 changed files with 19 additions and 10 deletions

View File

@ -59,6 +59,7 @@ import org.hl7.fhir.r5.model.Extension;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.utils.FHIRPathEngine;
import org.hl7.fhir.r5.utils.ToolingExtensions;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.StringPair;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -301,18 +302,18 @@ public class JsonParser extends ParserBase {
String code = property.getJsonKeyProperty();
List<Property> properties = property.getChildProperties(element.getName(), null);
if (properties.size() != 2) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_CHILD_COUNT), IssueSeverity.ERROR);
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_CHILD_COUNT, propNames(properties)), IssueSeverity.ERROR);
} else {
Property propK = properties.get(0);
Property propV = properties.get(1);
if (!propK.getName().equals(code)) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_NAME), IssueSeverity.ERROR);
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_NAME, propNames(properties)), IssueSeverity.ERROR);
} else if (!propK.isPrimitive()) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_TYPE), IssueSeverity.ERROR);
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_TYPE, propNames(properties), propK.typeSummary()), IssueSeverity.ERROR);
} else if (propV.isList()) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_NO_LIST), IssueSeverity.ERROR);
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_NO_LIST, propV.getName()), IssueSeverity.ERROR);
} else if (propV.isChoice()) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_NO_CHOICE), IssueSeverity.ERROR);
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.OBJECT_CANNOT_BE_KEYED_ARRAY_NO_CHOICE, propV.getName()), IssueSeverity.ERROR);
} else if (!(e instanceof JsonObject)) {
logError(line(e), col(e), npath, IssueType.INVALID, context.formatMessage(I18nConstants.THIS_PROPERTY_MUST_BE_AN_OBJECT_NOT_, describe(e)), IssueSeverity.ERROR);
} else {
@ -355,6 +356,14 @@ public class JsonParser extends ParserBase {
}
}
private Object propNames(List<Property> properties) {
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
for (Property p: properties) {
b.append(p.getName());
}
return b.toString();
}
private String describeType(JsonElement e) {
if (e.isJsonArray())
return "an Array";

View File

@ -383,11 +383,11 @@ This_property_must_be_an_object_not_ = This property must be an object, not {0}
This_property_must_be_an_simple_value_not_ = This property must be an simple value, not {0} ({1} at {2})
This_property_must_be__not_ = The property {2} must be {0}, not {1} (at {3})
This_property_must_be_an_Array_not_ = The property {1} must be a JSON Array, not {0} (at {2})
OBJECT_CANNOT_BE_KEYED_ARRAY_CHILD_COUNT = This object cannot be an keyed Array in Json because it does not have two children in the definitions
OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_NAME = This object is defined as a keyed Array in Json but the definition does not name the first child element as the key
OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_TYPE = This object is defined as a keyed Array in Json but the key property named in the definitions is not a primitive type
OBJECT_CANNOT_BE_KEYED_ARRAY_NO_CHOICE = This object is defined as a keyed Array in Json but the value property named in the definitions is a choice - this is not supported
OBJECT_CANNOT_BE_KEYED_ARRAY_NO_LIST = This object is defined as a keyed Array in Json but the value property named in the definitions is a list - this is not supported
OBJECT_CANNOT_BE_KEYED_ARRAY_CHILD_COUNT = This object cannot be an keyed Array in Json because it does not have two children in the definitions (children = {0})
OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_NAME = This object is defined as a keyed Array in Json but the definition does not name the first child element as the key (children = {0})
OBJECT_CANNOT_BE_KEYED_ARRAY_PROP_TYPE = This object is defined as a keyed Array in Json but the key property named in the definitions is not a primitive type (children = {0}, type = {1})
OBJECT_CANNOT_BE_KEYED_ARRAY_NO_CHOICE = This object is defined as a keyed Array in Json but the value property named in the definitions is a choice - this is not supported (value property = {0})
OBJECT_CANNOT_BE_KEYED_ARRAY_NO_LIST = This object is defined as a keyed Array in Json but the value property named in the definitions is a list - this is not supported (value property = {0})
Unrecognised_property_ = Unrecognized property ''{0}''
Object_must_have_some_content = Object must have some content
Error_parsing_JSON_ = Error parsing JSON: {0}