Skip to content

Commit

Permalink
[PM-14526] Add JsonNames annotation to SyncResponseJson (#4247)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored and SaintPatrck committed Nov 8, 2024
1 parent 8dd0cee commit e3e703e
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
{
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "ee697e71290c92fe5b607d0b7665481b",
"entities": [
{
"tableName": "ciphers",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `user_id` TEXT NOT NULL, `cipher_type` TEXT NOT NULL, `cipher_json` TEXT NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "cipherType",
"columnName": "cipher_type",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "cipherJson",
"columnName": "cipher_json",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_ciphers_user_id",
"unique": false,
"columnNames": [
"user_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_ciphers_user_id` ON `${TABLE_NAME}` (`user_id`)"
}
],
"foreignKeys": []
},
{
"tableName": "collections",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `user_id` TEXT NOT NULL, `organization_id` TEXT NOT NULL, `should_hide_passwords` INTEGER NOT NULL, `name` TEXT NOT NULL, `external_id` TEXT, `read_only` INTEGER NOT NULL, `manage` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "organizationId",
"columnName": "organization_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "shouldHidePasswords",
"columnName": "should_hide_passwords",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "externalId",
"columnName": "external_id",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "isReadOnly",
"columnName": "read_only",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "canManage",
"columnName": "manage",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_collections_user_id",
"unique": false,
"columnNames": [
"user_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_collections_user_id` ON `${TABLE_NAME}` (`user_id`)"
}
],
"foreignKeys": []
},
{
"tableName": "domains",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`user_id` TEXT NOT NULL, `domains_json` TEXT, PRIMARY KEY(`user_id`))",
"fields": [
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "domainsJson",
"columnName": "domains_json",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"user_id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "folders",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `user_id` TEXT NOT NULL, `name` TEXT, `revision_date` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "revisionDate",
"columnName": "revision_date",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_folders_user_id",
"unique": false,
"columnNames": [
"user_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_folders_user_id` ON `${TABLE_NAME}` (`user_id`)"
}
],
"foreignKeys": []
},
{
"tableName": "sends",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `user_id` TEXT NOT NULL, `send_type` TEXT NOT NULL, `send_json` TEXT NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "sendType",
"columnName": "send_type",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "sendJson",
"columnName": "send_json",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_sends_user_id",
"unique": false,
"columnNames": [
"user_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_sends_user_id` ON `${TABLE_NAME}` (`user_id`)"
}
],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ee697e71290c92fe5b607d0b7665481b')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface VaultDiskSource {
/**
* Retrieves all domains from the data source for a given [userId].
*/
fun getDomains(userId: String): Flow<SyncResponseJson.Domains>
fun getDomains(userId: String): Flow<SyncResponseJson.Domains?>

/**
* Deletes a folder from the data source for the given [userId] and [folderId].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
Expand Down Expand Up @@ -122,13 +121,12 @@ class VaultDiskSourceImpl(
},
)

override fun getDomains(userId: String): Flow<SyncResponseJson.Domains> =
override fun getDomains(userId: String): Flow<SyncResponseJson.Domains?> =
domainsDao
.getDomains(userId)
.filterNotNull()
.map { entity ->
withContext(dispatcherManager.default) {
json.decodeFromString<SyncResponseJson.Domains>(entity.domainsJson)
entity?.domainsJson?.let { json.decodeFromString<SyncResponseJson.Domains>(it) }
}
}

Expand Down Expand Up @@ -242,7 +240,7 @@ class VaultDiskSourceImpl(
domainsDao.insertDomains(
domains = DomainsEntity(
userId = userId,
domainsJson = json.encodeToString(vault.domains),
domainsJson = vault.domains?.let { json.encodeToString(it) },
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.x8bit.bitwarden.data.vault.datasource.disk.entity.SendEntity
FolderEntity::class,
SendEntity::class,
],
version = 4,
version = 5,
exportSchema = true,
)
@TypeConverters(ZonedDateTimeTypeConverter::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ data class DomainsEntity(
val userId: String,

@ColumnInfo(name = "domains_json")
val domainsJson: String,
val domainsJson: String?,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.x8bit.bitwarden.data.vault.datasource.network.model

import kotlinx.serialization.Contextual
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
import kotlinx.serialization.json.JsonObject
import java.time.ZonedDateTime

Expand All @@ -21,6 +23,7 @@ private const val DEFAULT_FIDO_2_KEY_CURVE = "P-256"
* @property domains A domains object associated with the vault data.
* @property sends A list of send objects associated with the vault data (nullable).
*/
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class SyncResponseJson(
@SerialName("folders")
Expand All @@ -30,6 +33,7 @@ data class SyncResponseJson(
val collections: List<Collection>?,

@SerialName("profile")
@JsonNames("Profile")
val profile: Profile,

@SerialName("ciphers")
Expand All @@ -39,7 +43,8 @@ data class SyncResponseJson(
val policies: List<Policy>?,

@SerialName("domains")
val domains: Domains,
@JsonNames("Domains")
val domains: Domains?,

@SerialName("sends")
val sends: List<Send>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import com.x8bit.bitwarden.data.vault.repository.model.DomainsData
/**
* Map the API [Domains] model to the internal [DomainsData] model.
*/
fun Domains.toDomainsData(): DomainsData {
fun Domains?.toDomainsData(): DomainsData {
val globalEquivalentDomains = this
.globalEquivalentDomains
?.globalEquivalentDomains
?.map { it.toInternalModel() }
.orEmpty()

return DomainsData(
equivalentDomains = this.equivalentDomains.orEmpty(),
equivalentDomains = this?.equivalentDomains.orEmpty(),
globalEquivalentDomains = globalEquivalentDomains,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,13 @@ class VaultDiskSourceTest {
// We cannot compare the JSON strings directly because of formatting differences
// So we split that off into its own assertion.
assertEquals(
DOMAINS_ENTITY.copy(domainsJson = ""),
storedDomainsEntity.copy(domainsJson = ""),
DOMAINS_ENTITY.copy(domainsJson = null),
storedDomainsEntity.copy(domainsJson = null),
)
assertJsonEquals(
requireNotNull(DOMAINS_ENTITY.domainsJson),
requireNotNull(storedDomainsEntity.domainsJson),
)
assertJsonEquals(DOMAINS_ENTITY.domainsJson, storedDomainsEntity.domainsJson)

// Verify the folders dao is updated
assertEquals(listOf(FOLDER_ENTITY), foldersDao.storedFolders)
Expand Down
Loading

0 comments on commit e3e703e

Please sign in to comment.