Skip to content

Fix incorrect URI resolution when root schema $id is not an absolute URI #88

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

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ private data class DefaultLoadingContext(
id.isAbsolute -> register(id.buildRefId(), assertion, dynamic) // register JSON schema by absolute URI
id.isRelative ->
when {
// For root schema we should not apply any transformations to ID
schemaPath === JsonPointer.ROOT && !id.path.isNullOrBlank() ->
// Empty URI is used to normalize the path
// Instead of 'path/segment' it will result '/path/segment
register(Uri.EMPTY.appendPathToParent(id.path!!).buildRefId(), assertion, dynamic)
!id.path.isNullOrBlank() ->
register(
// register JSON schema by related path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class JsonSchemaMultipleOfValidationTest : FunSpec() {
}
}

// https://github.com/OptimumCode/json-schema-validator/issues/70
test("BUG_70 rounding problem with some numbers because double does not behave as you expect") {
val schema =
JsonSchema.fromDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.github.optimumcode.json.schema.JsonSchemaLoader
import io.github.optimumcode.json.schema.SchemaOption
import io.github.optimumcode.json.schema.ValidationError
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldNotThrowAnyUnit
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
Expand Down Expand Up @@ -303,5 +304,55 @@ class JsonSchemaLoaderTest : FunSpec() {
.withCustomFormat("magic", MagicFormatValidator())
}.message shouldBe "format 'magic' already registered"
}

// https://github.com/OptimumCode/json-schema-validator/issues/87
test("BUG_87 relative uri-ref in root \$id causes incorrect reference resolution for root schema") {
val schema =
shouldNotThrowAny {
JsonSchemaLoader.create()
.register(
"""
{
"${'$'}schema": "https://json-schema.org/draft/2020-12/schema",
"${'$'}id": "myproject/enums/foo",
"type": "integer"
}
""".trimIndent(),
).fromDefinition(
"""
{
"${'$'}schema": "https://json-schema.org/draft/2020-12/schema",
"${'$'}id": "myproject/data/request",
"type": "object",
"properties": {
"foobar": {
"${'$'}ref": "/myproject/enums/foo"
}
}
}
""".trimIndent(),
)
}

val errors = mutableListOf<ValidationError>()
val valid =
schema.validate(
buildJsonObject {
put("foobar", JsonPrimitive("test"))
},
errors::add,
)
assertSoftly {
valid shouldBe false
errors.shouldContainExactly(
ValidationError(
schemaPath = JsonPointer("/properties/foobar/\$ref/type"),
objectPath = JsonPointer("/foobar"),
message = "element is not a integer",
absoluteLocation = JsonPointer("/type"),
),
)
}
}
}
}