-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent crash when Friendica returns a null
voted_on
property (#…
…456) Friendica can return a null `voted_on` property, in violation of the API spec. Introduce a `BooleanIfNull` annotation that will convert the `null` to `false` if encountered. While I'm here update the other adapters as classes on their relevant annotations instead of standalone classes to keep the code consistent. Fixes #455
- Loading branch information
1 parent
23e3cf1
commit 73c947e
Showing
14 changed files
with
307 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
core/network/src/main/kotlin/app/pachli/core/network/json/BooleanIfNull.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.core.network.json | ||
|
||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.JsonQualifier | ||
import com.squareup.moshi.JsonReader | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import java.lang.reflect.Type | ||
|
||
/** | ||
* A [JsonQualifier] for use with [Boolean] properties to indicate that their | ||
* value be set to the given [value] if the JSON property is `null`. | ||
* | ||
* Absent properties use the property's default value as normal. | ||
* | ||
* Usage: | ||
* ``` | ||
* val moshi = Moshi.Builder() | ||
* .add(BooleanIfNull.Factory()) | ||
* .build() | ||
* | ||
* @JsonClass(generateAdapter = true) | ||
* data class Foo( | ||
* @BooleanIfNull(false) val data: Boolean | ||
* ) | ||
* ``` | ||
*/ | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@JsonQualifier | ||
annotation class BooleanIfNull(val value: Boolean) { | ||
class Factory : JsonAdapter.Factory { | ||
override fun create( | ||
type: Type, | ||
annotations: MutableSet<out Annotation>, | ||
moshi: Moshi, | ||
): JsonAdapter<*>? { | ||
val delegateAnnotations = Types.nextAnnotations( | ||
annotations, | ||
BooleanIfNull::class.java, | ||
) ?: return null | ||
val delegate = moshi.nextAdapter<Any>( | ||
this, | ||
type, | ||
delegateAnnotations, | ||
) | ||
|
||
val annotation = annotations.first { it is BooleanIfNull } as BooleanIfNull | ||
return Adapter(delegate, annotation.value) | ||
} | ||
|
||
private class Adapter(private val delegate: JsonAdapter<Any>, val default: Boolean) : JsonAdapter<Any>() { | ||
override fun fromJson(reader: JsonReader): Any { | ||
val value = reader.readJsonValue() | ||
return value as? Boolean ?: default | ||
} | ||
|
||
override fun toJson(writer: JsonWriter, value: Any?) = delegate.toJson(writer, value) | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/network/src/main/kotlin/app/pachli/core/network/json/DefaultIfNull.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.core.network.json | ||
|
||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.JsonQualifier | ||
import com.squareup.moshi.JsonReader | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import java.lang.reflect.Type | ||
|
||
@Retention(AnnotationRetention.RUNTIME) | ||
@JsonQualifier | ||
annotation class DefaultIfNull { | ||
class Factory : JsonAdapter.Factory { | ||
override fun create( | ||
type: Type, | ||
annotations: MutableSet<out Annotation>, | ||
moshi: Moshi, | ||
): JsonAdapter<*>? { | ||
val delegateAnnotations = Types.nextAnnotations( | ||
annotations, | ||
DefaultIfNull::class.java, | ||
) ?: return null | ||
val delegate = moshi.nextAdapter<Any>( | ||
this, | ||
type, | ||
delegateAnnotations, | ||
) | ||
return DefaultIfNullAdapter(delegate) | ||
} | ||
|
||
private class DefaultIfNullAdapter(private val delegate: JsonAdapter<Any>) : JsonAdapter<Any>() { | ||
override fun fromJson(reader: JsonReader): Any? { | ||
val value = reader.readJsonValue() | ||
if (value is Map<*, *>) { | ||
val withoutNulls = value.filterValues { it != null } | ||
return delegate.fromJsonValue(withoutNulls) | ||
} | ||
return delegate.fromJsonValue(value) | ||
} | ||
|
||
override fun toJson(writer: JsonWriter, value: Any?) { | ||
return delegate.toJson(writer, value) | ||
} | ||
} | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
core/network/src/main/kotlin/app/pachli/core/network/json/DefaultIfNullAdapter.kt
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
core/network/src/main/kotlin/app/pachli/core/network/json/Guarded.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.core.network.json | ||
|
||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.JsonDataException | ||
import com.squareup.moshi.JsonQualifier | ||
import com.squareup.moshi.JsonReader | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import java.lang.reflect.Type | ||
|
||
/** | ||
* Deserialize this field as the given type, or null if the field value is not | ||
* this type. | ||
*/ | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@JsonQualifier | ||
annotation class Guarded { | ||
class Factory : JsonAdapter.Factory { | ||
override fun create( | ||
type: Type, | ||
annotations: MutableSet<out Annotation>, | ||
moshi: Moshi, | ||
): JsonAdapter<*>? { | ||
val delegateAnnotations = Types.nextAnnotations( | ||
annotations, | ||
Guarded::class.java, | ||
) ?: return null | ||
val delegate = moshi.nextAdapter<Any>( | ||
this, | ||
type, | ||
delegateAnnotations, | ||
) | ||
return GuardedAdapter(delegate) | ||
} | ||
|
||
private class GuardedAdapter(private val delegate: JsonAdapter<*>) : JsonAdapter<Any>() { | ||
override fun fromJson(reader: JsonReader): Any? { | ||
val peeked = reader.peekJson() | ||
val result = try { | ||
delegate.fromJson(peeked) | ||
} catch (_: JsonDataException) { | ||
null | ||
} finally { | ||
peeked.close() | ||
} | ||
reader.skipValue() | ||
return result | ||
} | ||
|
||
override fun toJson(writer: JsonWriter, value: Any?) { | ||
throw UnsupportedOperationException("@Guarded is only used to desererialize objects") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.