From d43c28ed1ff223fe29b2df1fac0bfaf75c261b3e Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Sun, 5 Nov 2023 17:23:30 +0100 Subject: [PATCH] fix(core): exclude content of values from names checks (4.6.2) (#1038) --- .../com/egm/stellio/shared/util/JsonUtils.kt | 30 +++++++++++-------- .../util/DataRepresentationUtilsTests.kt | 20 +++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonUtils.kt b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonUtils.kt index 695657a42..8eb56484e 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonUtils.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonUtils.kt @@ -1,6 +1,7 @@ package com.egm.stellio.shared.util import com.egm.stellio.shared.model.InvalidRequestException +import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VALUE_TERM import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.DeserializationFeature @@ -94,19 +95,24 @@ object JsonUtils { fun Map.getAllKeys(): Set = this.entries.fold(emptySet()) { acc, entry -> - val valueKeys = when (entry.value) { - is Map<*, *> -> (entry.value as Map).getAllKeys() - is List<*> -> - (entry.value as List).map { - // type value can be a list, not interested in it here - if (it is Map<*, *>) - (it as Map).getAllKeys() - else emptySet() - }.flatten().toSet() - // if it is not a list or an object, it is a value (and thus not a key) - else -> emptySet() + // what is inside the value of a property is not a key + if (entry.key == JSONLD_VALUE_TERM) + acc.plus(entry.key) + else { + val valueKeys = when (entry.value) { + is Map<*, *> -> (entry.value as Map).getAllKeys() + is List<*> -> + (entry.value as List).map { + // type value can be a list, not interested in it here + if (it is Map<*, *>) + (it as Map).getAllKeys() + else emptySet() + }.flatten().toSet() + // if it is not a list or an object, it is a value (and thus not a key) + else -> emptySet() + } + acc.plus(entry.key).plus(valueKeys) } - acc.plus(entry.key).plus(valueKeys) } fun Map.getAllValues(): Set = diff --git a/shared/src/test/kotlin/com/egm/stellio/shared/util/DataRepresentationUtilsTests.kt b/shared/src/test/kotlin/com/egm/stellio/shared/util/DataRepresentationUtilsTests.kt index c92694e33..9157c7345 100644 --- a/shared/src/test/kotlin/com/egm/stellio/shared/util/DataRepresentationUtilsTests.kt +++ b/shared/src/test/kotlin/com/egm/stellio/shared/util/DataRepresentationUtilsTests.kt @@ -70,6 +70,26 @@ class DataRepresentationUtilsTests { rawEntity.deserializeAsMap().checkNamesAreNgsiLdSupported().shouldSucceed() } + @Test + fun `it should validate an entity with special characters in the value of a property`() = runTest { + val rawEntity = + """ + { + "id": "urn:ngsi-ld:Device:01234", + "type": "Property", + "dateObserved": { + "type": "Property", + "value": { + "@type": "DateTime", + "@value": "2023-11-05T12:00:00Z" + } + } + } + """.trimIndent() + + rawEntity.deserializeAsMap().checkNamesAreNgsiLdSupported().shouldSucceed() + } + @Test fun `it should not validate an entity with an invalid attribute name`() = runTest { val rawEntity =