Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 23fdec6

Browse files
committed
Bugifx AML serializer: now correctly generates Attributes with correct AttributeType for null values. If value is null and there is no type info then no Attribute is generated.
1 parent d30ff42 commit 23fdec6

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/DefaultMapper.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.lang.reflect.InvocationTargetException;
2626
import java.util.Arrays;
2727
import java.util.HashSet;
28-
import java.util.List;
2928
import java.util.Set;
30-
import java.util.stream.Collectors;
3129

3230
public class DefaultMapper<T> implements Mapper<T> {
3331

@@ -117,7 +115,9 @@ protected String getAttributeName(T value, MappingContext context) {
117115
}
118116

119117
protected AttributeType.Builder toAttribute(T value, AmlGenerator generator, MappingContext context) throws MappingException {
120-
Class<?> aasType = ReflectionHelper.getAasInterface(value.getClass());
118+
Class<?> aasType = value != null
119+
? ReflectionHelper.getAasInterface(value.getClass())
120+
: null;
121121
AttributeType.Builder builder = AttributeType.builder();
122122
if (context.getProperty() != null) {
123123
builder = builder
@@ -127,7 +127,6 @@ protected AttributeType.Builder toAttribute(T value, AmlGenerator generator, Map
127127
if (aasType != null) {
128128
mapProperties(value, generator.with(builder), context);
129129
} else {
130-
131130
builder = builder.withValue(value);
132131
}
133132
return builder;
@@ -138,7 +137,6 @@ protected boolean skipProperty(PropertyDescriptor property) {
138137
}
139138

140139
protected void mapProperties(T value, AmlGenerator generator, MappingContext context) throws MappingException {
141-
List<String> collect = AasUtils.getAasProperties(value.getClass()).stream().map(x -> x.getName()).collect(Collectors.toList());
142140
for (PropertyDescriptor property : AasUtils.getAasProperties(value.getClass())) {
143141
if (!skipProperty(property)) {
144142
context.with(property)

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/mappers/AbstractElementMapperWithValueType.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,26 @@ protected InternalElementType.Builder toInternalElement(T value, AmlGenerator ge
6363
.copyOf(original)
6464
.withAttribute(untouchedAttributes);
6565
for (String property : typedProperties) {
66+
AttributeType.Builder typedAttributeBuilder;
6667
Optional<AttributeType> attributeToType = original.getAttribute().stream()
6768
.filter(x -> property.equals(x.getName()))
6869
.findFirst();
6970
if (attributeToType.isPresent()) {
70-
AttributeType.Builder typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
71-
if (valueTypeProperty.isPresent()) {
72-
try {
73-
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(
74-
PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + valueTypeProperty.get().getReadMethod().invoke(value));
75-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
76-
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
77-
}
71+
typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
72+
} else {
73+
typedAttributeBuilder = super.toAttribute(null, generator, context.with(AasUtils.getProperty(value, property)));
74+
}
75+
Object type = null;
76+
if (valueTypeProperty.isPresent()) {
77+
try {
78+
type = valueTypeProperty.get().getReadMethod().invoke(value);
79+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
80+
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
81+
}
82+
}
83+
if (attributeToType.isPresent() || type != null) {
84+
if (type != null) {
85+
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + type);
7886
}
7987
builder = builder.addAttribute(typedAttributeBuilder.build());
8088
}

dataformat-aml/src/test/resources/test_demo_full_example.aml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@
17891789
<Attribute Name="description">
17901790
<RefSemantic CorrespondingAttributePath="AAS:Referable/description"/>
17911791
<Attribute Name="aml-lang=en-us">
1792-
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">An example concept description for the test application</Value>
1792+
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">An example concept description for the test application</Value>
17931793
</Attribute>
17941794
<Attribute Name="aml-lang=de">
17951795
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">Ein Beispiel-ConceptDescription für eine Test-Anwendung</Value>
@@ -1849,7 +1849,7 @@
18491849
<Attribute Name="description">
18501850
<RefSemantic CorrespondingAttributePath="AAS:Referable/description"/>
18511851
<Attribute Name="aml-lang=en-us">
1852-
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">An example concept description for the test application</Value>
1852+
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">An example concept description for the test application</Value>
18531853
</Attribute>
18541854
<Attribute Name="aml-lang=de">
18551855
<Value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">Ein Beispiel-ConceptDescription für eine Test-Anwendung</Value>

0 commit comments

Comments
 (0)