Skip to content

Move references validation to the latter stage #45

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 1 commit into from
Feb 1, 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 @@ -26,7 +26,7 @@ private const val SCHEMA_PROPERTY: String = "\$schema"

internal class SchemaLoader : JsonSchemaLoader {
private val references: MutableMap<RefId, AssertionWithPath> = linkedMapOf()
private val usedRefs: MutableSet<RefId> = linkedSetOf()
private val usedRefs: MutableSet<ReferenceLocation> = linkedSetOf()

override fun register(
schema: JsonElement,
Expand Down Expand Up @@ -67,11 +67,12 @@ internal class SchemaLoader : JsonSchemaLoader {
draft: SchemaType?,
): JsonSchema {
val assertion: JsonSchemaAssertion = loadSchemaData(schemaElement, draft, references, usedRefs)
validateReferences(references, usedRefs)
return createSchema(
LoadResult(
assertion,
references.toMutableMap(),
usedRefs.toMutableSet(),
usedRefs.mapTo(hashSetOf()) { it.refId },
),
)
}
Expand Down Expand Up @@ -107,17 +108,18 @@ internal object IsolatedLoader : JsonSchemaLoader {
draft: SchemaType?,
): JsonSchema {
val references: MutableMap<RefId, AssertionWithPath> = linkedMapOf()
val usedRefs: MutableSet<RefId> = hashSetOf()
val usedRefs: MutableSet<ReferenceLocation> = hashSetOf()
val assertion: JsonSchemaAssertion = loadSchemaData(schemaElement, draft, references, usedRefs)
return createSchema(LoadResult(assertion, references, usedRefs))
validateReferences(references, usedRefs)
return createSchema(LoadResult(assertion, references, usedRefs.mapTo(hashSetOf()) { it.refId }))
}
}

private fun loadSchemaData(
schemaDefinition: JsonElement,
defaultType: SchemaType?,
references: MutableMap<RefId, AssertionWithPath>,
usedRefs: MutableSet<RefId>,
usedRefs: MutableSet<ReferenceLocation>,
externalUri: Uri? = null,
): JsonSchemaAssertion {
val schemaType = extractSchemaType(schemaDefinition, defaultType)
Expand All @@ -138,12 +140,18 @@ private fun loadSchemaData(
}
val schemaAssertion = loadSchema(schemaDefinition, context)
references.putAll(isolatedReferences)
context.usedRef.mapTo(usedRefs) { it.refId }
usedRefs.addAll(context.usedRef)
return schemaAssertion
}

private fun validateReferences(
references: Map<RefId, AssertionWithPath>,
usedRefs: Set<ReferenceLocation>,
) {
ReferenceValidator.validateReferences(
references.mapValues { it.value.run { PointerWithBaseId(this.baseId, schemaPath) } },
context.usedRef,
usedRefs,
)
return schemaAssertion
}

private fun createSchema(result: LoadResult): JsonSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.optimumcode.json.schema.ErrorCollector
import io.github.optimumcode.json.schema.JsonSchemaLoader
import io.github.optimumcode.json.schema.ValidationError
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldNotThrowAnyUnit
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContainExactly
Expand Down Expand Up @@ -161,5 +163,53 @@ class JsonSchemaLoaderTest : FunSpec() {
}
}
}

test("does not report missing refs when schema is registered") {
shouldNotThrowAnyUnit {
JsonSchemaLoader.create()
.register(
"""
{
"id": "https://test.com/a",
"properties": {
"anotherName": {
"${KEY}ref": "https://test.com/b#/properties/name"
}
}
}
""".trimIndent(),
)
}
}

test("reports missing refs when schema is created from definition") {
shouldThrow<IllegalArgumentException> {
JsonSchemaLoader.create()
.register(
"""
{
"${KEY}id": "https://test.com/a",
"properties": {
"anotherName": {
"${KEY}ref": "https://test.com/b#/properties/name"
}
}
}
""".trimIndent(),
).fromDefinition(
"""
{
"${KEY}id": "https://test.com/c",
"properties": {
"subName": {
"${KEY}ref": "https://test.com/a#/properties/anotherName"
}
}
}
""".trimIndent(),
)
}.message shouldBe "cannot resolve references: " +
"{\"https://test.com/b#/properties/name\": [\"/properties/anotherName\"]}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private fun FunSpec.executeFromDirectory(
JsonSchemaLoader.create()
.apply {
SchemaType.entries.forEach(::registerWellKnown)
for ((uri, schema) in remoteSchemas.entries.reversed()) {
for ((uri, schema) in remoteSchemas) {
if (uri.contains("draft4", ignoreCase = true)) {
// skip draft4 schemas
continue
Expand Down