Skip to content

Commit 6e8dd75

Browse files
author
Oleg
committed
Fix incorrect circled dependencies report
1 parent 4cd0d24 commit 6e8dd75

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/commonMain/kotlin/com/github/optimumcode/json/schema/internal/ReferenceValidator.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ internal object ReferenceValidator {
3333
return alwaysRunAssertions.any { path.contains(it) }
3434
}
3535
for ((location, refFragment) in locationToRef) {
36-
val (otherLocation, otherRefFragment) = locationToRef.entries.find { it.key.startsWith(refFragment) }
37-
?: continue
36+
val (otherLocation, otherRefFragment) = locationToRef.entries.find { (refKey) ->
37+
val startsWith = refKey.startsWith(refFragment)
38+
startsWith && (refKey[refFragment.length] == JsonPointer.SEPARATOR || refKey == refFragment)
39+
} ?: continue
3840
if (!location.startsWith(otherRefFragment)) {
3941
continue
4042
}

src/commonTest/kotlin/com/github/optimumcode/json/schema/base/JsonSchemaTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,29 @@ class JsonSchemaTest : FunSpec() {
124124
}.message shouldBe "circled references: /definitions/alice/allOf/0 ref to /definitions/bob" +
125125
" and /definitions/bob/allOf/0 ref to /definitions/alice"
126126
}
127+
128+
test("does not report circled references if definitions have similar names") {
129+
shouldNotThrowAny {
130+
JsonSchema.fromDefinition(
131+
"""
132+
{
133+
"${KEY}schema": "http://json-schema.org/draft-07/schema#",
134+
"definitions": {
135+
"nonNegativeInteger": {
136+
"type": "integer",
137+
"minimum": 0
138+
},
139+
"nonNegativeIntegerDefault0": {
140+
"allOf": [
141+
{ "${KEY}ref": "#/definitions/nonNegativeInteger" },
142+
{ "default": 0 }
143+
]
144+
}
145+
}
146+
}
147+
""".trimIndent(),
148+
)
149+
}
150+
}
127151
}
128152
}

0 commit comments

Comments
 (0)