Skip to content

Commit

Permalink
Issue #2424 - introduce ignoringUnrecognizedElements field
Browse files Browse the repository at this point in the history
Signed-off-by: John T.E. Timm <[email protected]>
  • Loading branch information
JohnTimm committed Jun 1, 2021
1 parent 8d0eb6e commit 3a7e480
Show file tree
Hide file tree
Showing 7 changed files with 4,085 additions and 1,353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public abstract class FHIRAbstractParser implements FHIRParser {
protected Map<String, Object> properties = new HashMap<>();
protected boolean validating = true;
protected boolean ignoringUnrecognizedElements = false;

@Override
public abstract <T extends Resource> T parse(InputStream in) throws FHIRParserException;
Expand All @@ -35,6 +36,16 @@ public boolean isValidating() {
return validating;
}

@Override
public void setIgnoringUnrecognizedElements(boolean ignoringUnrecognizedElements) {
this.ignoringUnrecognizedElements = ignoringUnrecognizedElements;
}

@Override
public boolean isIgnoringUnrecognizedElements() {
return ignoringUnrecognizedElements;
}

@Override
public void setProperty(String name, Object value) {
Objects.requireNonNull(name);
Expand Down
1,338 changes: 665 additions & 673 deletions fhir-model/src/main/java/com/ibm/fhir/model/parser/FHIRJsonParser.java

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions fhir-model/src/main/java/com/ibm/fhir/model/parser/FHIRParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface FHIRParser {
/**
* Property name for a property that controls whether the parser will ignore or throw an exception on unrecognized elements
*/
@Deprecated
public static final String PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS = "com.ibm.fhir.model.parser.ignoreUnrecognizedElements";

/**
Expand Down Expand Up @@ -69,6 +70,22 @@ public interface FHIRParser {
*/
boolean isValidating();

/**
* Set the ignoring unrecognized elements indicator for this parser
*
* @param ignoringUnrecognizedElements
* the ignoring unrecognized elements indicator
*/
void setIgnoringUnrecognizedElements(boolean ignoringUnrecognizedElements);

/**
* Indicates whether this parser is ignoring unrecognized elements
*
* @return
* true if this parser is ignoring unrecognized elements, false otherwise
*/
boolean isIgnoringUnrecognizedElements();

/**
* Set the property with the given name to the passed value
*
Expand Down
3,984 changes: 3,320 additions & 664 deletions fhir-model/src/main/java/com/ibm/fhir/model/parser/FHIRXMLParser.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* (C) Copyright IBM Corp. 2021
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.ibm.fhir.model.test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import java.io.StringReader;

import org.testng.annotations.Test;

import com.ibm.fhir.model.format.Format;
import com.ibm.fhir.model.parser.FHIRParser;
import com.ibm.fhir.model.parser.exception.FHIRParserException;
import com.ibm.fhir.model.resource.Patient;

public class IgnoringUnrecognizedElementsTest {
@Test
public void testIgnoringUnrecognizedElements1() {
try {
String xmlString = "<Patient xmlns=\"http://hl7.org/fhir\"><hamburger/></Patient>";
FHIRParser parser = FHIRParser.parser(Format.XML);
parser.parse(new StringReader(xmlString));
} catch (FHIRParserException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
assertEquals(e.getCause().getMessage(), "Unrecognized element: 'hamburger'");
}
}

@Test
public void testIgnoringUnrecognizedElements2() {
try {
String jsonString = "{\"resourceType\":\"Patient\",\"hamburger\":true}";
FHIRParser parser = FHIRParser.parser(Format.JSON);
parser.parse(new StringReader(jsonString));
} catch (Exception e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
assertEquals(e.getCause().getMessage(), "Unrecognized element: 'hamburger'");
}
}

@Test
public void testIgnoringUnrecognizedElements3() throws Exception {
String xmlString = "<Patient xmlns=\"http://hl7.org/fhir\"><hamburger/></Patient>";
FHIRParser parser = FHIRParser.parser(Format.XML);
parser.setIgnoringUnrecognizedElements(true);
Patient patient = parser.parse(new StringReader(xmlString));
assertNotNull(patient);
}

@Test
public void testIgnoringUnrecognizedElements4() throws Exception {
String jsonString = "{\"resourceType\":\"Patient\",\"hamburger\":true}";
FHIRParser parser = FHIRParser.parser(Format.JSON);
parser.setIgnoringUnrecognizedElements(true);
Patient patient = parser.parse(new StringReader(jsonString));
assertNotNull(patient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public Resource readFrom(Class<Resource> type, Type genericType, Annotation[] an
FHIRRequestContext requestContext = FHIRRequestContext.get();
Format format = getFormat(mediaType);
FHIRParser parser = FHIRParser.parser(format);
if (parser.isPropertySupported(FHIRParser.PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS)) {
parser.setProperty(FHIRParser.PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS,
HTTPHandlingPreference.LENIENT.equals(requestContext.getHandlingPreference()));
}
parser.setIgnoringUnrecognizedElements(HTTPHandlingPreference.LENIENT.equals(requestContext.getHandlingPreference()));
return parser.parse(entityStream);
} catch (FHIRParserException e) {
if (RuntimeType.SERVER.equals(runtimeType)) {
Expand Down
19 changes: 7 additions & 12 deletions fhir-tools/src/main/java/com/ibm/fhir/tools/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,11 @@ private void generateXMLParseMethod(String generatedClassName, JsonObject struct
}

cb._default()
._throw(_new("IllegalArgumentException", args("\"Unrecognized element: '\" + localName + \"'\"")));
._if("!ignoringUnrecognizedElements")
._throw(_new("IllegalArgumentException", args("\"Unrecognized element: '\" + localName + \"'\"")))
._end()
.invoke("reader", "nextTag", args())
._break();

cb._end();

Expand Down Expand Up @@ -3039,15 +3043,6 @@ private void generateJsonParser(String basePath) {
.end();
cb.newLine();

cb.override();
cb.method(mods("public"), "boolean", "isPropertySupported", params("java.lang.String name"))
._if("FHIRParser.PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS.equals(name)")
._return("true")
._end()
._return("super.isPropertySupported(name)")
.end();
cb.newLine();

cb.method(mods("private"), "Resource", "parseResource", params("java.lang.String elementName", "JsonObject jsonObject", "int elementIndex"));
cb._if("jsonObject == null");
cb._return("null");
Expand Down Expand Up @@ -3240,7 +3235,7 @@ private void generateParseMethod(String generatedClassName, JsonObject structure
._return("null")
._end();
cb.invoke("stackPush", args("elementName", "elementIndex"));
cb._if("getPropertyOrDefault(FHIRParser.PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS, java.lang.Boolean.FALSE, java.lang.Boolean.class) == false")
cb._if("!ignoringUnrecognizedElements")
.invoke("checkForUnrecognizedElements", args(generatedClassName + ".class", "jsonObject"))
._end();
}
Expand Down Expand Up @@ -3331,7 +3326,7 @@ private void generatePrimitiveTypeParseMethod(String generatedClassName, JsonObj

cb._if("_jsonValue != null && _jsonValue.getValueType() == JsonValue.ValueType.OBJECT")
.assign("JsonObject jsonObject", "(JsonObject) _jsonValue")
._if("getPropertyOrDefault(FHIRParser.PROPERTY_IGNORE_UNRECOGNIZED_ELEMENTS, java.lang.Boolean.FALSE, java.lang.Boolean.class) == false")
._if("!ignoringUnrecognizedElements")
.invoke("checkForUnrecognizedElements", args("Element.class", "jsonObject"))
._end()
.invoke("parseElement", args("builder", "jsonObject"))
Expand Down

0 comments on commit 3a7e480

Please sign in to comment.