mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix case "skipped empty list and not null parameter"
GitOrigin-RevId: 42e81739dee2a0585c0b52825c7c4bfab3ef32e9
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b09f8151e0
commit
9302cc5457
@@ -45,6 +45,11 @@ internal class CollectionBinding(type: ParameterizedType, context: BindingInitia
|
||||
private val collectionClass = ClassUtil.typeToClass(type)
|
||||
|
||||
override fun deserialize(context: ReadContext): Collection<Any?> {
|
||||
if (context.reader.type == IonType.INT) {
|
||||
LOG.assertTrue(context.reader.intValue() == 0)
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val result = createCollection()
|
||||
readInto(result, context)
|
||||
return result
|
||||
@@ -53,6 +58,12 @@ internal class CollectionBinding(type: ParameterizedType, context: BindingInitia
|
||||
override fun serialize(obj: Any, context: WriteContext) {
|
||||
val writer = context.writer
|
||||
val collection = obj as Collection<*>
|
||||
if (context.filter.skipEmptyCollection && collection.isEmpty()) {
|
||||
// some value must be written otherwise on deserialize null will be used for constructor parameters (and it can be not expected)
|
||||
writer.writeInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
writer.stepIn(IonType.LIST)
|
||||
collection.forEach(createItemConsumer(context))
|
||||
writer.stepOut()
|
||||
@@ -64,6 +75,9 @@ internal class CollectionBinding(type: ParameterizedType, context: BindingInitia
|
||||
property.set(hostObject, null)
|
||||
return
|
||||
}
|
||||
else if (type == IonType.INT /* empty collection if context.filter.skipEmptyCollection */) {
|
||||
return
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
var result = property.readUnsafe(hostObject) as MutableCollection<Any?>?
|
||||
@@ -105,11 +119,27 @@ internal class CollectionBinding(type: ParameterizedType, context: BindingInitia
|
||||
internal class ArrayBinding(private val itemClass: Class<*>, context: BindingInitializationContext) : BaseCollectionBinding(itemClass, context) {
|
||||
override fun deserialize(context: ReadContext) = readArray(context)
|
||||
|
||||
override fun deserialize(hostObject: Any, property: MutableAccessor, context: ReadContext) {
|
||||
val type = context.reader.type
|
||||
if (type == IonType.NULL) {
|
||||
property.set(hostObject, null)
|
||||
}
|
||||
else if (type != IonType.INT) {
|
||||
property.set(hostObject, readArray(context))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(obj: Any, context: WriteContext) {
|
||||
val array = obj as Array<*>
|
||||
val writer = context.writer
|
||||
if (context.filter.skipEmptyArray && array.isEmpty()) {
|
||||
writer.writeInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
writer.stepIn(IonType.LIST)
|
||||
val consumer = createItemConsumer(context)
|
||||
(obj as Array<*>).forEach { consumer.accept(it) }
|
||||
array.forEach { consumer.accept(it) }
|
||||
writer.stepOut()
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,13 @@ internal class MapBinding(keyType: Type, valueType: Type, context: BindingInitia
|
||||
private val isKeyComparable = Comparable::class.java.isAssignableFrom(ClassUtil.typeToClass(keyType))
|
||||
|
||||
override fun serialize(obj: Any, context: WriteContext) {
|
||||
val writer = context.writer
|
||||
val map = obj as Map<*, *>
|
||||
val writer = context.writer
|
||||
|
||||
if (context.filter.skipEmptyMap && map.isEmpty()) {
|
||||
writer.writeInt(0)
|
||||
return
|
||||
}
|
||||
|
||||
fun writeEntry(key: Any?, value: Any?) {
|
||||
if (key == null) {
|
||||
|
||||
@@ -85,19 +85,29 @@ class ObjectSerializer {
|
||||
}
|
||||
|
||||
interface SerializationFilter {
|
||||
val skipEmptyCollection: Boolean
|
||||
get() = false
|
||||
|
||||
val skipEmptyMap: Boolean
|
||||
get() = false
|
||||
|
||||
val skipEmptyArray: Boolean
|
||||
get() = false
|
||||
|
||||
fun isSkipped(value: Any?): Boolean
|
||||
}
|
||||
|
||||
object SkipNullAndEmptySerializationFilter : SerializationFilter {
|
||||
override fun isSkipped(value: Any?): Boolean {
|
||||
return when (value) {
|
||||
null -> true
|
||||
is Collection<*> -> value.isEmpty()
|
||||
is Array<*> -> value.isEmpty()
|
||||
is Map<*, *> -> value.isEmpty()
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
override fun isSkipped(value: Any?) = value == null
|
||||
|
||||
override val skipEmptyCollection: Boolean
|
||||
get() = true
|
||||
|
||||
override val skipEmptyMap: Boolean
|
||||
get() = true
|
||||
|
||||
override val skipEmptyArray: Boolean
|
||||
get() = true
|
||||
}
|
||||
|
||||
class ObjectIdWriter {
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
'@id':0,
|
||||
bean:{
|
||||
'@id':1,
|
||||
bean:0
|
||||
}
|
||||
bean:0,
|
||||
list:0,
|
||||
children:0
|
||||
},
|
||||
list:0,
|
||||
children:0
|
||||
}
|
||||
@@ -19,13 +19,18 @@ class NonDefaultConstructorTest {
|
||||
@Rule
|
||||
val fsRule = InMemoryFsRule()
|
||||
|
||||
private fun test(bean: Any) = test(bean, testName, defaultTestWriteConfiguration)
|
||||
private fun test(bean: Any, writeConfiguration: WriteConfiguration = defaultTestWriteConfiguration) = test(bean, testName, writeConfiguration)
|
||||
|
||||
@Test
|
||||
fun `no default constructor`() {
|
||||
test(NoDefaultConstructorBean("foo", arrayListOf(42, 21)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `skipped empty list and not null parameter`() {
|
||||
test(NoDefaultConstructorBean("foo", emptyList()), defaultTestWriteConfiguration.copy(filter = SkipNullAndEmptySerializationFilter))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `null string as arg`() {
|
||||
test(NullableArgBean("foo", null, ""))
|
||||
|
||||
Reference in New Issue
Block a user