Skip to content

Commit 480258d

Browse files
fix(openapi): fix structured properties mapping (#10260)
1 parent 2624e19 commit 480258d

File tree

1 file changed

+43
-2
lines changed
  • metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/util

1 file changed

+43
-2
lines changed

metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/util/MappingUtil.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.core.JsonProcessingException;
1010
import com.fasterxml.jackson.databind.JsonNode;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.node.ArrayNode;
1213
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
1314
import com.fasterxml.jackson.databind.node.ObjectNode;
1415
import com.linkedin.avro2pegasus.events.KafkaAuditHeader;
@@ -43,6 +44,7 @@
4344
import io.datahubproject.openapi.generated.OneOfEnvelopedAspectValue;
4445
import io.datahubproject.openapi.generated.OneOfGenericAspectValue;
4546
import io.datahubproject.openapi.generated.Status;
47+
import io.datahubproject.openapi.generated.StructuredProperties;
4648
import java.lang.reflect.InvocationTargetException;
4749
import java.lang.reflect.Method;
4850
import java.util.HashMap;
@@ -274,8 +276,47 @@ public static OneOfEnvelopedAspectValue mapAspectValue(
274276
ENVELOPED_ASPECT_TYPE_MAP.get(aspectName);
275277
DataMap wrapper = insertDiscriminator(aspectClass, aspect.data());
276278
try {
277-
String dataMapAsJson = objectMapper.writeValueAsString(wrapper);
278-
return objectMapper.readValue(dataMapAsJson, aspectClass);
279+
if (aspectClass.equals(StructuredProperties.class)) {
280+
return mapStructuredPropertyValues(wrapper, objectMapper);
281+
} else {
282+
String dataMapAsJson = objectMapper.writeValueAsString(wrapper);
283+
return objectMapper.readValue(dataMapAsJson, aspectClass);
284+
}
285+
} catch (JsonProcessingException e) {
286+
throw new RuntimeException(e);
287+
}
288+
}
289+
290+
public static OneOfEnvelopedAspectValue mapStructuredPropertyValues(
291+
DataMap dataMap, ObjectMapper objectMapper) {
292+
try {
293+
String dataMapAsJson = objectMapper.writeValueAsString(dataMap);
294+
JsonNode jsonObject = objectMapper.readTree(dataMapAsJson);
295+
ArrayNode properties = (ArrayNode) jsonObject.get("properties");
296+
297+
if (properties.isEmpty()) {
298+
return objectMapper.readValue(dataMapAsJson, StructuredProperties.class);
299+
}
300+
301+
properties.forEach(
302+
property -> {
303+
ArrayNode values = (ArrayNode) property.get("values");
304+
ArrayNode newValues = JsonNodeFactory.instance.arrayNode();
305+
values.forEach(
306+
value -> {
307+
if (value.has("string")) {
308+
newValues.add(value.get("string").textValue());
309+
} else if (value.has("double")) {
310+
newValues.add(value.get("double").doubleValue());
311+
}
312+
});
313+
if (!newValues.isEmpty()) {
314+
values.removeAll();
315+
values.addAll(newValues);
316+
}
317+
});
318+
return objectMapper.readValue(
319+
objectMapper.writeValueAsString(jsonObject), StructuredProperties.class);
279320
} catch (JsonProcessingException e) {
280321
throw new RuntimeException(e);
281322
}

0 commit comments

Comments
 (0)