From 2a7373e9a228ac343e65363eed2b755682084460 Mon Sep 17 00:00:00 2001 From: Andrii Rublov Date: Tue, 6 Aug 2024 20:01:15 +0200 Subject: [PATCH] [ef-core]: Fix RIDER-112371: Relax rules for `appsettings.json` and `secrets.json` parsing, allow trailing commas --- .../connections/impl/AppSettingsConnectionProvider.kt | 2 +- .../connections/impl/JsonConnectionStringsManager.kt | 6 +++--- .../efcore/features/shared/services/JsonSerializer.kt | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/AppSettingsConnectionProvider.kt b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/AppSettingsConnectionProvider.kt index 44824e7f..227b0fc5 100644 --- a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/AppSettingsConnectionProvider.kt +++ b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/AppSettingsConnectionProvider.kt @@ -23,7 +23,7 @@ class AppSettingsConnectionProvider(private val intellijProject: Project) : DbCo override fun getAvailableConnections(project: RdProjectDescriptor) = buildList { - val directory = (project.location as RdCustomLocation?)?.customLocation?.let(::Path)?.parent ?: return@buildList + val directory = (project.location as? RdCustomLocation)?.customLocation?.let(::Path)?.parent ?: return@buildList @NonNls val connectionStrings = directory.listDirectoryEntries("appsettings*.json") .filter { it.isRegularFile() } diff --git a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/JsonConnectionStringsManager.kt b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/JsonConnectionStringsManager.kt index cd608cb1..5b8c5e39 100644 --- a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/JsonConnectionStringsManager.kt +++ b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/connections/impl/JsonConnectionStringsManager.kt @@ -32,9 +32,9 @@ class JsonConnectionStringsManager(intellijProject: Project) { * } */ private fun getConnectionsFromObject(fileName: String, jsonFile: JsonNode): List = buildList { - val obj = jsonFile.get("ConnectionStrings") as ObjectNode? ?: return emptyList() + val obj = jsonFile.get("ConnectionStrings") as? ObjectNode ?: return emptyList() obj.fieldNames().forEach { connName -> - val connString = (obj[connName] as TextNode?)?.textValue() + val connString = (obj[connName] as? TextNode)?.textValue() if (connString != null) add(DbConnectionInfo(connName, connString, fileName, null)) } @@ -53,7 +53,7 @@ class JsonConnectionStringsManager(intellijProject: Project) { null -> return@forEach else -> { val name = match.groups["name"]?.value ?: return@forEach - val connection = (jsonFile[settingsField] as TextNode?)?.textValue() ?: return@forEach + val connection = (jsonFile[settingsField] as? TextNode)?.textValue() ?: return@forEach add(DbConnectionInfo(name, connection, fileName, null)) } } diff --git a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/shared/services/JsonSerializer.kt b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/shared/services/JsonSerializer.kt index 8fccb575..02f98109 100644 --- a/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/shared/services/JsonSerializer.kt +++ b/src/rider/main/kotlin/com/jetbrains/rider/plugins/efcore/features/shared/services/JsonSerializer.kt @@ -2,6 +2,7 @@ package com.jetbrains.rider.plugins.efcore.features.shared.services import com.fasterxml.jackson.core.JacksonException import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.core.json.JsonReadFeature import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.intellij.openapi.components.Service @@ -17,6 +18,7 @@ class JsonSerializer { jacksonObjectMapper() .enable(JsonParser.Feature.ALLOW_COMMENTS) .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES) + .enable(JsonReadFeature.ALLOW_TRAILING_COMMA.mappedFeature()) fun getInstance() = service() }