Skip to content

Commit a22dd43

Browse files
authored
Merge 8317dc0 into 0743d5a
2 parents 0743d5a + 8317dc0 commit a22dd43

File tree

7 files changed

+43
-53
lines changed

7 files changed

+43
-53
lines changed

firebase-functions/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
2-
* [fixed] Fixed HttpsCallableResult.data resolution in Kotlin
2+
* [fixed] Minor internal infrastructure improvements. (#6544)
3+
* [fixed] Fixed HttpsCallableResult.data resolution in Kotlin. (#6530)
34

45
# 21.1.0
56
* [changed] Migrated to Kotlin
@@ -209,4 +210,3 @@ updates.
209210
optional region to override the default "us-central1".
210211
* [feature] New `useFunctionsEmulator` method allows testing against a local
211212
instance of the [Cloud Functions Emulator](https://firebase.google.com/docs/functions/local-emulator).
212-

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseContextProvider.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ constructor(
4747
appCheckDeferred.whenAvailable { p: Provider<InteropAppCheckTokenProvider> ->
4848
val appCheck = p.get()
4949
appCheckRef.set(appCheck)
50-
appCheck.addAppCheckTokenListener { unused: AppCheckTokenResult? -> }
50+
appCheck.addAppCheckTokenListener {
51+
// Do nothing; we just need to register a listener so that the App Check SDK knows
52+
// to auto-refresh the token.
53+
}
5154
}
5255
}
5356

54-
override fun getContext(limitedUseAppCheckToken: Boolean): Task<HttpsCallableContext?>? {
57+
override fun getContext(getLimitedUseAppCheckToken: Boolean): Task<HttpsCallableContext?>? {
5558
val authToken = getAuthToken()
56-
val appCheckToken = getAppCheckToken(limitedUseAppCheckToken)
59+
val appCheckToken = getAppCheckToken(getLimitedUseAppCheckToken)
5760
return Tasks.whenAll(authToken, appCheckToken).onSuccessTask(executor) { _ ->
5861
Tasks.forResult(
5962
HttpsCallableContext(authToken.result, instanceId.get().token, appCheckToken.result)

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ internal constructor(
5858
@UiThread uiExecutor: Executor
5959
) {
6060
// The network client to use for HTTPS requests.
61-
private val client: OkHttpClient
61+
private val client: OkHttpClient = OkHttpClient()
6262

6363
// A serializer to encode/decode parameters and return values.
64-
private val serializer: Serializer
64+
private val serializer: Serializer = Serializer()
6565

6666
// A provider of client metadata to include with calls.
67-
private val contextProvider: ContextProvider
67+
private val contextProvider: ContextProvider = Preconditions.checkNotNull(contextProvider)
6868

6969
// The projectId to use for all functions references.
70-
private val projectId: String
70+
private val projectId: String = Preconditions.checkNotNull(projectId)
7171

7272
// The region to use for all function references.
7373
private var region: String? = null
@@ -82,12 +82,7 @@ internal constructor(
8282
private var emulatorSettings: EmulatedServiceSettings? = null
8383

8484
init {
85-
client = OkHttpClient()
86-
serializer = Serializer()
87-
this.contextProvider = Preconditions.checkNotNull(contextProvider)
88-
this.projectId = Preconditions.checkNotNull(projectId)
89-
val isRegion: Boolean
90-
isRegion =
85+
val isRegion: Boolean =
9186
try {
9287
URL(regionOrCustomDomain)
9388
false
@@ -182,9 +177,7 @@ internal constructor(
182177
options: HttpsCallOptions
183178
): Task<HttpsCallableResult> {
184179
return providerInstalled.task
185-
.continueWithTask(executor) { task: Task<Void>? ->
186-
contextProvider.getContext(options.limitedUseAppCheckTokens)
187-
}
180+
.continueWithTask(executor) { contextProvider.getContext(options.limitedUseAppCheckTokens) }
188181
.continueWithTask(executor) { task: Task<HttpsCallableContext?> ->
189182
if (!task.isSuccessful) {
190183
return@continueWithTask Tasks.forException<HttpsCallableResult>(task.exception!!)
@@ -204,9 +197,7 @@ internal constructor(
204197
*/
205198
internal fun call(url: URL, data: Any?, options: HttpsCallOptions): Task<HttpsCallableResult> {
206199
return providerInstalled.task
207-
.continueWithTask(executor) { task: Task<Void>? ->
208-
contextProvider.getContext(options.limitedUseAppCheckTokens)
209-
}
200+
.continueWithTask(executor) { contextProvider.getContext(options.limitedUseAppCheckTokens) }
210201
.continueWithTask(executor) { task: Task<HttpsCallableContext?> ->
211202
if (!task.isSuccessful) {
212203
return@continueWithTask Tasks.forException<HttpsCallableResult>(task.exception!!)
@@ -277,16 +268,15 @@ internal constructor(
277268
@Throws(IOException::class)
278269
override fun onResponse(ignored: Call, response: Response) {
279270
val code = fromHttpStatus(response.code())
280-
val body = response.body()!!.string()
281-
val exception = fromResponse(code, body, serializer)
271+
val bodyAsString = response.body()!!.string()
272+
val exception = fromResponse(code, bodyAsString, serializer)
282273
if (exception != null) {
283274
tcs.setException(exception)
284275
return
285276
}
286-
val bodyJSON: JSONObject
287-
bodyJSON =
277+
val bodyAsJson: JSONObject =
288278
try {
289-
JSONObject(body)
279+
JSONObject(bodyAsString)
290280
} catch (je: JSONException) {
291281
val e: Exception =
292282
FirebaseFunctionsException(
@@ -298,10 +288,10 @@ internal constructor(
298288
tcs.setException(e)
299289
return
300290
}
301-
var dataJSON = bodyJSON.opt("data")
291+
var dataJSON = bodyAsJson.opt("data")
302292
// TODO: Allow "result" instead of "data" for now, for backwards compatibility.
303293
if (dataJSON == null) {
304-
dataJSON = bodyJSON.opt("result")
294+
dataJSON = bodyAsJson.opt("result")
305295
}
306296
if (dataJSON == null) {
307297
val e: Exception =

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctionsException.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class FirebaseFunctionsException : FirebaseException {
2727
* canonical error codes for Google APIs, as documented here:
2828
* https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto#L26
2929
*/
30-
public enum class Code(private val value: Int) {
30+
public enum class Code(@Suppress("unused") private val value: Int) {
3131
/**
3232
* The operation completed successfully. FirebaseFunctionsException will never have a status of
3333
* OK.
@@ -199,21 +199,21 @@ public class FirebaseFunctionsException : FirebaseException {
199199
serializer: Serializer
200200
): FirebaseFunctionsException? {
201201
// Start with reasonable defaults from the status code.
202-
var code = code
203-
var description = code.name
202+
var actualCode = code
203+
var description = actualCode.name
204204
var details: Any? = null
205205

206206
// Then look through the body for explicit details.
207207
try {
208-
val json = JSONObject(body)
208+
val json = JSONObject(body ?: "")
209209
val error = json.getJSONObject("error")
210210
if (error.opt("status") is String) {
211-
code = Code.valueOf(error.getString("status"))
211+
actualCode = Code.valueOf(error.getString("status"))
212212
// TODO: Add better default descriptions for error enums.
213213
// The default description needs to be updated for the new code.
214-
description = code.name
214+
description = actualCode.name
215215
}
216-
if (error.opt("message") is String && !error.getString("message").isEmpty()) {
216+
if (error.opt("message") is String && error.getString("message").isNotEmpty()) {
217217
description = error.getString("message")
218218
}
219219
details = error.opt("details")
@@ -222,16 +222,16 @@ public class FirebaseFunctionsException : FirebaseException {
222222
}
223223
} catch (iae: IllegalArgumentException) {
224224
// This most likely means the status string was invalid, so consider this malformed.
225-
code = Code.INTERNAL
226-
description = code.name
225+
actualCode = Code.INTERNAL
226+
description = actualCode.name
227227
} catch (ioe: JSONException) {
228228
// If we couldn't parse explicit error data, that's fine.
229229
}
230-
return if (code == Code.OK) {
230+
return if (actualCode == Code.OK) {
231231
// Technically, there's an edge case where a developer could explicitly return an error code
232232
// of OK, and we will treat it as success, but that seems reasonable.
233233
null
234-
} else FirebaseFunctionsException(description, code, details)
234+
} else FirebaseFunctionsException(description, actualCode, details)
235235
}
236236
}
237237
}

firebase-functions/src/main/java/com/google/firebase/functions/FunctionsRegistrar.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.google.firebase.components.Dependency
2727
import com.google.firebase.components.Qualified
2828
import com.google.firebase.iid.internal.FirebaseInstanceIdInternal
2929
import com.google.firebase.platforminfo.LibraryVersionComponent
30-
import java.util.Arrays
3130
import java.util.concurrent.Executor
3231

3332
/**
@@ -40,7 +39,7 @@ public class FunctionsRegistrar : ComponentRegistrar {
4039
override fun getComponents(): List<Component<*>> {
4140
val liteExecutor = Qualified.qualified(Lightweight::class.java, Executor::class.java)
4241
val uiExecutor = Qualified.qualified(UiThread::class.java, Executor::class.java)
43-
return Arrays.asList(
42+
return listOf(
4443
Component.builder(FunctionsMultiResourceComponent::class.java)
4544
.name(LIBRARY_NAME)
4645
.add(Dependency.required(Context::class.java))
@@ -60,7 +59,7 @@ public class FunctionsRegistrar : ComponentRegistrar {
6059
.setIid(c.getProvider(FirebaseInstanceIdInternal::class.java))
6160
.setAppCheck(c.getDeferred(InteropAppCheckTokenProvider::class.java))
6261
.build()
63-
?.multiResourceComponent
62+
.multiResourceComponent
6463
}
6564
.build(),
6665
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME)

firebase-functions/src/main/java/com/google/firebase/functions/Serializer.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ public class Serializer {
6565
}
6666
if (obj is Map<*, *>) {
6767
val result = JSONObject()
68-
val m = obj
69-
for (k in m.keys) {
68+
for (k in obj.keys) {
7069
require(k is String) { "Object keys must be strings." }
71-
val value = encode(m[k])
70+
val value = encode(obj[k])
7271
try {
7372
result.put(k, value)
7473
} catch (e: JSONException) {
@@ -87,11 +86,10 @@ public class Serializer {
8786
}
8887
if (obj is JSONObject) {
8988
val result = JSONObject()
90-
val m = obj
91-
val keys = m.keys()
89+
val keys = obj.keys()
9290
while (keys.hasNext()) {
9391
val k = keys.next() ?: throw IllegalArgumentException("Object keys cannot be null.")
94-
val value = encode(m.opt(k))
92+
val value = encode(obj.opt(k))
9593
try {
9694
result.put(k, value)
9795
} catch (e: JSONException) {
@@ -103,18 +101,18 @@ public class Serializer {
103101
}
104102
if (obj is JSONArray) {
105103
val result = JSONArray()
106-
val l = obj
107-
for (i in 0 until l.length()) {
108-
val o = l.opt(i)
104+
for (i in 0 until obj.length()) {
105+
val o = obj.opt(i)
109106
result.put(encode(o))
110107
}
111108
return result
112109
}
113110
throw IllegalArgumentException("Object cannot be encoded in JSON: $obj")
114111
}
115112

116-
public fun decode(obj: Any): Any? {
113+
public fun decode(obj: Any?): Any? {
117114
// TODO: Maybe this should throw a FirebaseFunctionsException instead?
115+
if (obj == null) return null
118116
if (obj is Number) {
119117
return obj
120118
}

firebase-functions/src/test/java/com/google/firebase/functions/FunctionsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class FunctionsTests : BaseTestCase() {
9898
class LibraryVersionTest : BaseTestCase() {
9999
@Test
100100
fun `library version should be registered with runtime`() {
101-
val publisher = Firebase.app.get(UserAgentPublisher::class.java)
101+
Firebase.app.get(UserAgentPublisher::class.java)
102102
}
103103
}
104104

0 commit comments

Comments
 (0)