Skip to content

Commit

Permalink
fix(core): exclude content of values from names checks (4.6.2) (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal authored Nov 5, 2023
1 parent 0c1248b commit d43c28e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
30 changes: 18 additions & 12 deletions shared/src/main/kotlin/com/egm/stellio/shared/util/JsonUtils.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -94,19 +95,24 @@ object JsonUtils {

fun Map<String, Any>.getAllKeys(): Set<String> =
this.entries.fold(emptySet()) { acc, entry ->
val valueKeys = when (entry.value) {
is Map<*, *> -> (entry.value as Map<String, Any>).getAllKeys()
is List<*> ->
(entry.value as List<Any>).map {
// type value can be a list, not interested in it here
if (it is Map<*, *>)
(it as Map<String, Any>).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<String, Any>).getAllKeys()
is List<*> ->
(entry.value as List<Any>).map {
// type value can be a list, not interested in it here
if (it is Map<*, *>)
(it as Map<String, Any>).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<String, Any>.getAllValues(): Set<Any?> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit d43c28e

Please sign in to comment.