question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON objects with duplicated properties should fail validity and equality checks

See original GitHub issue

Which 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:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Kantiscommented, Oct 5, 2022

Did I correctly identify the changes that should be done

Yes, I think so

Since there are no options yet available for shouldBeValidJson, I doubt that creating an object would be useful but what do you think?

I think it’s still worthwhile since it enables the syntax:


actual shouldBeValidJson {
  allowDuplicateProperties = false
  expected
}

as discussed here: https://github.com/kotest/kotest/issues/3161#issuecomment-1215835513

1reaction
Lysouncommented, Oct 9, 2022

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 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found