Skip to content
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 @@ -712,8 +712,46 @@ private fun Uri.appendPathToParent(path: String): Uri {
}
}.appendEncodedPath(path)
.build()
.normalizeUri()
}

private fun Uri.normalizeUri(): Uri {
if (pathSegments.none { it in RELATIVE_PATH_SEGMENTS }) {
// Nothing to normalize
return this
}

val newPathSegments = ArrayDeque<String>()
for (segment in pathSegments) {
when (segment) {
SAME_LEVEL_SEGMENT -> { // skip
}

PARENT_LEVEL_SEGMENT ->
if (newPathSegments.isEmpty()) {
error("cannot normalize URI '$this'. Path goes beyond root")
} else {
newPathSegments.removeLast()
}

else -> newPathSegments.addLast(segment)
}
}

return buildUpon()
.encodedPath(null)
.apply {
for (segment in newPathSegments) {
appendEncodedPath(segment)
}
}.build()
}

private const val SAME_LEVEL_SEGMENT = "."
private const val PARENT_LEVEL_SEGMENT = ".."

private val RELATIVE_PATH_SEGMENTS = setOf(SAME_LEVEL_SEGMENT, PARENT_LEVEL_SEGMENT)

private val ANCHOR_REGEX: Regex = "^[A-Za-z][A-Za-z0-9-_:.]*$".toRegex()

private fun Uri.buildRefId(): RefId = RefId(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal object IdnHostnameFormatValidator : AbstractStringFormatValidator() {
if (value.isEmpty()) {
return FormatValidator.Invalid()
}
if (value.length == 1 && isLabelSeparator(value[0])) {
if (isLabelSeparator(value[0]) || isLabelSeparator(value[value.lastIndex])) {
return FormatValidator.Invalid()
}

Expand Down Expand Up @@ -113,6 +113,11 @@ internal object IdnHostnameFormatValidator : AbstractStringFormatValidator() {
return false
}

if (unicode.isEmpty()) {
// empty labels are not valid
return false
}

// https://datatracker.ietf.org/doc/html/rfc5891#section-4.2.3.1
if (unicode[0] == '-' || unicode.codePointBefore(unicode.length) == '-'.code) {
// cannot start or end with hyphen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class JsonSchemaIdnHostnameFormatValidationTest : FunSpec() {
listOf(
TestCase("", "empty value"),
TestCase(".", "single separator"),
TestCase(".example", "leading separator"),
TestCase("example.", "trailing separator"),
TestCase("example..com", "two separators in a row"),
TestCase("\u3002", "single separator U+3002"),
TestCase("\uFF0E", "single separator U+FF0E"),
TestCase("\uFF61", "single separator U+FF61"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class JsonSchemaTest : FunSpec() {
"http://example.com/other.json",
"http://example.com/other.json#",
"http://example.com/root.json#/definitions/B",
"./other.json",
),
"definition X" to
listOf(
Expand All @@ -136,7 +137,8 @@ class JsonSchemaTest : FunSpec() {
"http://example.com/root.json#/definitions/C",
),
).forEach { (refDestination, possibleRefs) ->
possibleRefs.asSequence()
possibleRefs
.asSequence()
.flatMapIndexed { index, ref ->
val uri = Uri.parse(ref)
val caseNumber = index + 1
Expand Down
Loading