-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds account_numbers permission and parses unknown enum values to `UN…
…KNOWN`. (#5583) * Adds account number permission. * Regenerates API. * Added unknown enum serializer. * Adds unknown enum test. * Moves to stripe-core.
- Loading branch information
1 parent
420a500
commit 5701790
Showing
5 changed files
with
134 additions
and
76 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
34 changes: 34 additions & 0 deletions
34
financial-connections/src/test/resources/json/linked_account_session_unknown_permission.json
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,34 @@ | ||
{ | ||
"id": "las_dhgfsklhgfkdsjhgk", | ||
"object": "link_account_session", | ||
"client_secret": "las_client_secret_ldafjlfkjlsfadkjk", | ||
"linked_accounts": { | ||
"object": "list", | ||
"data": [ | ||
], | ||
"has_more": false, | ||
"total_count": 0, | ||
"url": "/v1/linked_accounts" | ||
}, | ||
"livemode": true, | ||
"payment_account": { | ||
"id": "la_dfsdfasfds", | ||
"object": "financial_connections.account", | ||
"balance": null, | ||
"balance_refresh": null, | ||
"category": "credit", | ||
"created": 1648749414, | ||
"display_name": "CREDIT CARD", | ||
"institution_name": "Chase", | ||
"last4": "5579", | ||
"livemode": true, | ||
"permissions": [ | ||
"payment_method", | ||
"hola" | ||
], | ||
"status": "active", | ||
"subcategory": "credit_card", | ||
"supported_payment_method_types": [ | ||
] | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...re/src/main/java/com/stripe/android/core/model/serializers/EnumIgnoreUnknownSerializer.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,43 @@ | ||
package com.stripe.android.core.model.serializers | ||
|
||
import androidx.annotation.RestrictTo | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
/** | ||
* Parses an enum using [values], and on unknown values, falls back to [defaultValue]. | ||
*/ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
abstract class EnumIgnoreUnknownSerializer<T : Enum<T>>( | ||
values: Array<out T>, | ||
private val defaultValue: T | ||
) : | ||
KSerializer<T> { | ||
// Alternative to taking values in param, take clazz: Class<T> | ||
// - private val values = clazz.enumConstants | ||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor(values.first()::class.qualifiedName!!, PrimitiveKind.STRING) | ||
|
||
// Build maps for faster parsing, used @SerialName annotation if present, fall back to name | ||
private val lookup = values.associateBy({ it }, { it.serialName }) | ||
private val revLookup = values.associateBy { it.serialName } | ||
|
||
private val Enum<T>.serialName: String | ||
get() = this::class.java.getField(this.name).getAnnotation(SerialName::class.java)?.value | ||
?: name | ||
|
||
override fun serialize(encoder: Encoder, value: T) { | ||
encoder.encodeString(lookup.getValue(value)) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): T { | ||
// only run 'decoder.decodeString()' once | ||
return revLookup[decoder.decodeString()] | ||
?: defaultValue // map.getOrDefault is not available < API-24 | ||
} | ||
} |