-
Notifications
You must be signed in to change notification settings - Fork 640
dataconnect: ignore unknown keys in response data instead of throwing an exception #7314
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
Open
dconeybe
wants to merge
5
commits into
main
Choose a base branch
from
dconeybe/dataconnect/IgnoreUnknownPropertiesInResponse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2f254b
ProtoStructDecoder.kt: format code
dconeybe 437b85b
ProtoStructDecoderUnitTest.kt: add test for decoding unknown keys
dconeybe 8d9dcc6
UnknownKeysIntegrationTest.kt added with a test for query data (still…
dconeybe 9d232db
UnknownKeysIntegrationTest.kt added with a test for mutation data
dconeybe 1c7d3d7
CHANGELOG.md entry added
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
140 changes: 140 additions & 0 deletions
140
...nect/src/androidTest/kotlin/com/google/firebase/dataconnect/UnknownKeysIntegrationTest.kt
This file contains hidden or 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,140 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalFirebaseDataConnect::class) | ||
|
||
package com.google.firebase.dataconnect | ||
|
||
import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase | ||
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect | ||
import com.google.firebase.dataconnect.testutil.schemas.AllTypesSchema | ||
import com.google.firebase.dataconnect.testutil.schemas.PersonSchema | ||
import io.kotest.assertions.withClue | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.next | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.serializer | ||
import org.junit.Test | ||
|
||
class UnknownKeysIntegrationTest : DataConnectIntegrationTestBase() { | ||
|
||
private val personSchema by lazy { PersonSchema(dataConnectFactory) } | ||
private val allTypesSchema by lazy { AllTypesSchema(dataConnectFactory) } | ||
|
||
@Test | ||
fun unknownKeysInQueryResponseDataShouldBeIgnored() = runTest { | ||
val id = Arb.dataConnect.uuid().next() | ||
allTypesSchema | ||
.createPrimitive( | ||
AllTypesSchema.PrimitiveData( | ||
id = id, | ||
idFieldNullable = "0d2fcdf1c4a84c64a87f3c7932b31749", | ||
intField = 42, | ||
intFieldNullable = 43, | ||
floatField = 123.45, | ||
floatFieldNullable = 678.91, | ||
booleanField = true, | ||
booleanFieldNullable = false, | ||
stringField = "TestString", | ||
stringFieldNullable = "TestNullableString" | ||
) | ||
) | ||
.execute() | ||
|
||
@Serializable | ||
data class PrimitiveQueryDataValues( | ||
val intFieldNullable: Int?, | ||
val booleanField: Boolean, | ||
val booleanFieldNullable: Boolean?, | ||
val stringField: String, | ||
val stringFieldNullable: String?, | ||
) | ||
|
||
/** An adaptation of [AllTypesSchema.GetPrimitiveQuery.Data] with some keys missing. */ | ||
@Serializable | ||
data class PrimitiveQueryDataMissingSomeKeys(val primitive: PrimitiveQueryDataValues) | ||
|
||
val result = | ||
allTypesSchema | ||
.getPrimitive(id = id) | ||
.withDataDeserializer(serializer<PrimitiveQueryDataMissingSomeKeys>()) | ||
.execute() | ||
|
||
result.data.primitive shouldBe | ||
PrimitiveQueryDataValues( | ||
intFieldNullable = 43, | ||
booleanField = true, | ||
booleanFieldNullable = false, | ||
stringField = "TestString", | ||
stringFieldNullable = "TestNullableString" | ||
) | ||
} | ||
|
||
@Test | ||
fun unknownKeysInMutationResponseDataShouldBeIgnored() = runTest { | ||
@Serializable data class PersonKeyWithId(val id: String) | ||
val person1 = PersonKeyWithId(id = "8a218894521f457a9be45a0986494058") | ||
val person4 = PersonKeyWithId(id = "ef8de2a4a6de400e94d555f148b643c0") | ||
val mutationRef = | ||
personSchema.dataConnect.mutation("create5People", Unit, serializer<Unit>(), serializer()) | ||
|
||
// Precondition check: Verify that the response contains person1..person5 and the expected IDs. | ||
withClue("precondition check") { | ||
@Serializable | ||
data class Create5PeopleData( | ||
val person1: PersonKeyWithId, | ||
val person3: PersonKeyWithId, | ||
val person2: PersonKeyWithId, | ||
val person4: PersonKeyWithId, | ||
val person5: PersonKeyWithId, | ||
) | ||
|
||
val mutationResult = | ||
mutationRef.withDataDeserializer(serializer<Create5PeopleData>()).execute() | ||
mutationResult.data shouldBe | ||
Create5PeopleData( | ||
person1 = person1, | ||
person2 = PersonKeyWithId(id = "464443371f284194be4b2e78c3ef000c"), | ||
person3 = PersonKeyWithId(id = "903d83db81754bd29860458f127ef124"), | ||
person4 = person4, | ||
person5 = PersonKeyWithId(id = "8584fd7ca2b6453da18d21d4341f1804"), | ||
) | ||
} | ||
|
||
withClue("actual test") { | ||
// Create5PeopleDataWithMissingKeys is missing "person2.id", "person3", and "person5" which | ||
// will be present in the response. | ||
@Serializable data class PersonKeyWithoutId(val foo: Nothing? = null) | ||
@Serializable | ||
data class Create5PeopleDataWithMissingKeys( | ||
val person1: PersonKeyWithId, | ||
val person2: PersonKeyWithoutId, | ||
val person4: PersonKeyWithId, | ||
) | ||
|
||
val mutationResult = | ||
mutationRef.withDataDeserializer(serializer<Create5PeopleDataWithMissingKeys>()).execute() | ||
mutationResult.data shouldBe | ||
Create5PeopleDataWithMissingKeys( | ||
person1 = person1, | ||
person2 = PersonKeyWithoutId(), | ||
person4 = PersonKeyWithId(id = "ef8de2a4a6de400e94d555f148b643c0"), | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.