Skip to content

Commit

Permalink
Adds handling of emptry entries (#238)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de>
  • Loading branch information
FrankSchnicke authored Jan 31, 2024
1 parent d52e98b commit 7d23642
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 16 deletions.
6 changes: 5 additions & 1 deletion dataformat-xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand All @@ -71,5 +70,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
*/
package org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.deserialization;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -23,19 +31,15 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Custom deserializer for lists without individual list entry wrappers for parametrized classes.
*
* @param <T> deserialized class within the list
*/
public class NoEntryWrapperListDeserializer<T extends Object> extends JsonDeserializer<List<T>> {
public class NoEntryWrapperListDeserializer<T> extends JsonDeserializer<List<T>> {
protected final String elementName;
private CustomJsonNodeDeserializer<T> nodeDeserializer;
private static Logger logger = LoggerFactory.getLogger(NoEntryWrapperListDeserializer.class);

public NoEntryWrapperListDeserializer(String elementName, CustomJsonNodeDeserializer<T> nodeDeserializer) {
this.elementName = elementName;
Expand All @@ -44,12 +48,17 @@ public NoEntryWrapperListDeserializer(String elementName, CustomJsonNodeDeserial

@Override
public List<T> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectNode node = DeserializationHelper.getRootObjectNode(parser);
JsonNode langStringNode = node.get(elementName);
if (langStringNode.isObject()) {
return createEntriesFromObjectNode(langStringNode, parser);
} else {
return createEntriesFromArrayNode((ArrayNode) langStringNode, parser);
try {
ObjectNode node = DeserializationHelper.getRootObjectNode(parser);
JsonNode langStringNode = node.get(elementName);
if (langStringNode.isObject()) {
return createEntriesFromObjectNode(langStringNode, parser);
} else {
return createEntriesFromArrayNode((ArrayNode) langStringNode, parser);
}
} catch (ClassCastException e) {
logger.info("Found empty list items (e.g., '<preferredName />' of dataSpecificationIec61360) in XML. This is most likely an error.");
return new ArrayList<T>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.eclipse.digitaltwin.aas4j.v3.dataformat.xml;

import java.io.FileNotFoundException;
import java.util.List;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.AASFull;
Expand All @@ -30,9 +33,6 @@
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.FileNotFoundException;
import java.util.List;

public class XMLDeserializerTest {

@Test
Expand Down Expand Up @@ -133,4 +133,10 @@ public void deserializeAASWithExtensionMaximal() throws SerializationException,

Assert.assertEquals(Examples.EXTENSION_MAXIMAL, env);
}

@Test
public void deserializeWithEmptyKeys() throws FileNotFoundException, DeserializationException {
java.io.File file = new java.io.File("src/test/resources/empty_entries.xml");
new XmlDeserializer().read(file);
}
}
88 changes: 88 additions & 0 deletions dataformat-xml/src/test/resources/empty_entries.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<environment xmlns="https://admin-shell.io/aas/3/0">
<submodels>
<submodel>
<idShort>Nameplate</idShort>
<id>https://example.com/ids/sm/2593_9052_1042_2364</id>
<kind>Template</kind>
<semanticId>
<type>ExternalReference</type>
<keys>
<key>
<type>GlobalReference</type>
<value>https://admin-shell.io/zvei/nameplate/2/0/Nameplate</value>
</key>
</keys>
</semanticId>
<submodelElements>
<multiLanguageProperty>
<idShort>ManufacturerName</idShort>
<description>
<langStringTextType>
<language>en</language>
<text>Note: see also [IRDI] 0112/2///61987#ABA565#007 manufacturer Note: mandatory property according to EU Machine Directive 2006/42/EC. </text>
</langStringTextType>
</description>
<semanticId>
<type>ExternalReference</type>
<keys>
<key>
<type>GlobalReference</type>
<value>0173-1#02-AAO677#002</value>
</key>
</keys>
</semanticId>
<qualifiers>
<qualifier>
<type>Multiplicity</type>
<valueType>xs:string</valueType>
<value>One</value>
</qualifier>
</qualifiers>
<embeddedDataSpecifications>
<embeddedDataSpecification>
<dataSpecification>
<type>ExternalReference</type>
<keys>
<key>
<type>GlobalReference</type>
<value>http://admin-shell.io/DataSpecificationTemplates/DataSpecificationIEC61360/3/0</value>
</key>
</keys>
</dataSpecification>
<dataSpecificationContent>
<dataSpecificationIec61360>
<preferredName />
</dataSpecificationIec61360>
</dataSpecificationContent>
</embeddedDataSpecification>
<embeddedDataSpecification>
<dataSpecification>
<type>ModelReference</type>
<keys />
</dataSpecification>
<dataSpecificationContent>
<dataSpecificationIec61360>
<preferredName />
</dataSpecificationIec61360>
</dataSpecificationContent>
</embeddedDataSpecification>
</embeddedDataSpecifications>
<value>
<langStringTextType>
<language>de</language>
<text>Muster AG</text>
</langStringTextType>
<langStringTextType>
<language>en</language>
<text>Muster AG</text>
</langStringTextType>
</value>
<valueId>
<type>ExternalReference</type>
<keys />
</valueId>
</multiLanguageProperty>
</submodelElements>
</submodel>
</submodels>
</environment>

0 comments on commit 7d23642

Please sign in to comment.