-
Notifications
You must be signed in to change notification settings - Fork 645
Disallow missing commas between fields (#2287 #2520) #2889
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
alisa101rs
wants to merge
6
commits into
Kotlin:dev
Choose a base branch
from
alisa101rs:master
base: dev
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a99156
Disallow missing commas between fields (#2287)
alisa101rs 520cacb
fix: failing example
alisa101rs 591bc8f
fix: formatting
alisa101rs 76897d9
feat: move comma check to lexer and improve trailing comma reports
alisa101rs 02691d5
fix: remove wrong hint
alisa101rs 719acbb
fix: use consumeNextToken() instead of incrementing currentPosition
alisa101rs 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
108 changes: 108 additions & 0 deletions
108
formats/json-tests/commonTest/src/kotlinx/serialization/json/MissingCommaTest.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,108 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.json | ||
|
||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.test.* | ||
import kotlin.test.* | ||
|
||
class MissingCommaTest : JsonTestBase() { | ||
@Serializable | ||
class Holder( | ||
val i: Int, | ||
val c: Child, | ||
) | ||
|
||
@Serializable | ||
class Child( | ||
val i: String | ||
) | ||
|
||
private val withTrailingComma = Json { allowTrailingComma = true } | ||
|
||
@Test | ||
fun missingCommaBetweenFieldsAfterPrimitive() { | ||
val message = | ||
"Unexpected JSON token at offset 8: Expected end of the object or comma at path: \$" | ||
val json = """{"i":42 "c":{"i":"string"}}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", message) { | ||
default.decodeFromString<Holder>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun missingCommaBetweenFieldsAfterObject() { | ||
val message = | ||
"Unexpected JSON token at offset 19: Expected end of the object or comma at path: \$" | ||
val json = """{"c":{"i":"string"}"i":42}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", message) { | ||
default.decodeFromString<Holder>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun allowTrailingCommaDoesNotApplyToCommaBetweenFields() { | ||
val message = | ||
"Unexpected JSON token at offset 8: Expected end of the object or comma at path: \$" | ||
val json = """{"i":42 "c":{"i":"string"}}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", message) { | ||
withTrailingComma.decodeFromString<Holder>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun lenientSerializeDoesNotAllowToSkipCommaBetweenFields() { | ||
val message = | ||
"Unexpected JSON token at offset 8: Expected end of the object or comma at path: \$" | ||
val json = """{"i":42 "c":{"i":"string"}}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", message) { | ||
lenient.decodeFromString<Holder>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun missingCommaInDynamicMap() { | ||
val m = "Unexpected JSON token at offset 8: Expected end of the object or comma at path: \$" | ||
val json = """{"i":42 "c":{"i":"string"}}""" | ||
assertFailsWithSerialMessage("JsonDecodingException", m) { | ||
default.parseToJsonElement(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun missingCommaInArray() { | ||
val m = "Unexpected JSON token at offset 3: Expected end of the array or comma at path: \$" | ||
val json = """[1 2 3 4]""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", m) { | ||
default.decodeFromString<List<Int>>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun missingCommaInStringMap() { | ||
val m = "Unexpected JSON token at offset 9: Expected end of the object or comma at path: \$" | ||
val json = """{"a":"1" "b":"2"}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", m) { | ||
default.decodeFromString<Map<String, String>>(json) | ||
} | ||
} | ||
|
||
@Test | ||
fun missingCommaInUnknownKeys() { | ||
val m = "Unexpected JSON token at offset 17: Expected end of the object or comma at path: \$" | ||
val json = """{"i":"1","b":"2" "c":"2"}""" | ||
|
||
assertFailsWithSerialMessage("JsonDecodingException", m) { | ||
lenient.decodeFromString<Child>(json) | ||
} | ||
} | ||
} |
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
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.