Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): exclude content of values from names checks (4.6.2) #1038

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading