From 5a575804c9aa18fb83ce12b7e8f6e4d59e6d0011 Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Sun, 5 Nov 2023 09:20:45 +0100 Subject: [PATCH 1/2] fix(core): exclude content of values from names checks (4.6.2) --- .../com/egm/stellio/shared/util/JsonUtils.kt | 30 +++++++++++-------- 1 file changed, 18 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 = From 66b333273e35c8cf521c10dc6e20e04af6bf1fdc Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Sun, 5 Nov 2023 10:51:30 +0100 Subject: [PATCH 2/2] test: add a test for an entity containing a dateTime property --- .../util/DataRepresentationUtilsTests.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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 =