JSON objects with duplicated properties should fail validity and equality checks
See original GitHub issueWhich version of Kotest are you using 5.3.2
I want to verify that a JSON object does not contain duplicated fields. However shouldBeValidJson()
and shouldEqualJson()
do not throw errors.
I would like an additional comparison flag added to shouldBeValidJson()
and compareJsonOptions { }
for verifying that properties are distinct.
import io.kotest.assertions.json.CompareMode
import io.kotest.assertions.json.FieldComparison
import io.kotest.assertions.json.PropertyOrder
import io.kotest.assertions.json.TypeCoercion
import io.kotest.assertions.json.compareJsonOptions
import io.kotest.assertions.json.shouldBeValidJson
import io.kotest.assertions.json.shouldEqualJson
import org.junit.jupiter.api.Test
class JsonTest {
@Test
fun `expect json with duplicated properties is invalid`() {
"""
{
"test": 123,
"test": 456
}
""".trimIndent().shouldBeValidJson()
}
@Test
fun `expect json with duplicated properties does not match de-duplicated object`() {
"""
{
"test": 123,
"test": 456
}
""".trimIndent().shouldEqualJson(
"""
{
"test": 456
}
""".trimIndent(),
compareJsonOptions {
propertyOrder = PropertyOrder.Strict
fieldComparison = FieldComparison.Strict
}
)
}
}
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Does JSON syntax allow duplicate keys in an object?
The short answer: Yes but is not recommended. The long answer: It depends on what you call valid... ECMA-404 "The JSON Data Interchange...
Read more >JSON API's schema disallows duplicate resource identifiers ...
I suspect that the schema validation is complaining about adding duplicate item multiple times, which should not be allowed by the spec. @ ......
Read more >Removing Duplicate Elements from a JavaScript Array (part 2)
A story of how deduplicating a JavaScript array led to a rabbit hole filled with existential questions.
Read more >Resolve JSON errors in Amazon Athena - AWS
Athena processes JSON data using one of two JSON SerDes: The native Apache Hive/HCatalog JsonSerDe (org.apache.hive.hcatalog.data.
Read more >12: 9.15. JSON Functions and Operators - PostgreSQL
text[], json or jsonb, Get JSON object at the specified path ... text representation will be used, in such a fashion that it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, I think so
I think it’s still worthwhile since it enables the syntax:
as discussed here: https://github.com/kotest/kotest/issues/3161#issuecomment-1215835513
I’ve been working on this check on the kotlinx serialization side (https://github.com/Kotlin/kotlinx.serialization/issues/1990). Once I’m done, it should be a piece of cake to use those checks in kotest 😄