Skip to content

Commit 2c0b101

Browse files
committed
Make EditorSettings throw for invalid JSON
1 parent 6f8d82d commit 2c0b101

File tree

5 files changed

+166
-168
lines changed

5 files changed

+166
-168
lines changed

android/Gutenberg/src/main/java/org/wordpress/gutenberg/model/EditorSettings.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,18 @@ data class EditorSettings(
4141
* be cached.
4242
*
4343
* @param data The raw JSON string from the block editor settings endpoint.
44+
* @throws kotlinx.serialization.SerializationException if [data] is not valid JSON.
4445
*/
4546
fun fromData(data: String): EditorSettings {
46-
val jsonValue = try {
47-
json.parseToJsonElement(data)
48-
} catch (e: Exception) {
49-
null
50-
}
47+
val jsonValue = json.parseToJsonElement(data)
48+
val stringValue = jsonValue.toString().orEmpty()
5149

52-
val stringValue = jsonValue?.toString() ?: ""
53-
54-
val themeStyles = try {
55-
val settings = json.decodeFromString<InternalEditorSettings>(data)
56-
settings.styles.mapNotNull { it.css }.joinToString("\n")
57-
} catch (e: Exception) {
58-
""
59-
}
50+
val themeStyles = runCatching {
51+
json.decodeFromString<InternalEditorSettings>(data)
52+
.styles
53+
.mapNotNull { it.css }
54+
.joinToString("\n")
55+
}.getOrDefault("")
6056

6157
return EditorSettings(
6258
jsonValue = jsonValue,

android/Gutenberg/src/test/java/org/wordpress/gutenberg/model/EditorSettingsTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import kotlinx.serialization.encodeToString
44
import kotlinx.serialization.json.Json
55
import org.junit.Assert.assertEquals
66
import org.junit.Assert.assertNull
7+
import org.junit.Assert.assertThrows
78
import org.junit.Assert.assertTrue
89
import org.junit.Test
910

1011
class EditorSettingsTest {
1112

1213
private val json = Json { ignoreUnknownKeys = true }
1314

15+
@Test
16+
fun `throws when JSON is invalid`() {
17+
val invalidJSON = "not valid json"
18+
19+
assertThrows(Exception::class.java, {
20+
EditorSettings.fromData(invalidJSON)
21+
})
22+
}
23+
1424
// MARK: - themeStyles Tests
1525

1626
@Test
@@ -48,13 +58,6 @@ class EditorSettingsTest {
4858
assertEquals("h1 { font-size: 2em; }", settings.themeStyles)
4959
}
5060

51-
@Test
52-
fun `themeStyles is empty when JSON is invalid`() {
53-
val invalidJSON = "not valid json"
54-
val settings = EditorSettings.fromData(invalidJSON)
55-
assertTrue(settings.themeStyles.isEmpty())
56-
}
57-
5861
@Test
5962
fun `themeStyles is empty when styles key is missing`() {
6063
val jsonString = """{"otherKey": "value"}"""

ios/Sources/GutenbergKit/Sources/Model/EditorSettings.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ public struct EditorSettings: Sendable, Codable, Equatable, Hashable {
2525
/// be cached.
2626
///
2727
/// - Parameter data: The raw JSON data from the block editor settings endpoint.
28-
init(data: Data) {
29-
let json = try? JSON(data)
28+
init(data: Data) throws {
29+
let json = try JSON(data)
3030
self.jsonValue = json
3131

32-
let encodedJSON = try? JSONEncoder().encode(json)
33-
self.stringValue = String(decoding: encodedJSON ?? Data(), as: UTF8.self)
32+
let encodedJSON = try JSONEncoder().encode(json)
33+
self.stringValue = String(decoding: encodedJSON, as: UTF8.self)
3434

35-
if let settings = try? JSONDecoder().decode(InternalEditorSettings.self, from: data) {
36-
self.themeStyles = settings.styles.compactMap { $0.css }.joined(separator: "\n")
37-
} else {
38-
self.themeStyles = ""
39-
}
35+
let settings = try JSONDecoder().decode(InternalEditorSettings.self, from: data)
36+
self.themeStyles = settings.styles?.compactMap { $0.css }.joined(separator: "\n") ?? ""
4037
}
4138

4239
private init(
@@ -67,5 +64,5 @@ struct InternalEditorSettings: Decodable {
6764
}
6865

6966
/// All style entries from the theme.
70-
let styles: [CSSStyle]
67+
let styles: [CSSStyle]?
7168
}

ios/Sources/GutenbergKit/Sources/RESTAPIRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct RESTAPIRepository: Sendable {
7272
let request = URLRequest(method: .GET, url: editorSettingsUrl)
7373
let response = try await self.httpClient.perform(request)
7474

75-
let editorSettings = EditorSettings(data: response.0)
75+
let editorSettings = try EditorSettings(data: response.0)
7676

7777
let urlResponse = EditorURLResponse((try JSONEncoder().encode(editorSettings), response.1))
7878
try self.cache.store(urlResponse, for: editorSettingsUrl, httpMethod: .GET)

0 commit comments

Comments
 (0)