Skip to content

Commit cf9082e

Browse files
KuechAdevcrocod
authored andcommitted
Fix bugs in regexes
1 parent 8dbb8c6 commit cf9082e

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

  • kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/Feature.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ public class UriTemplateFeatureKey(override val key: String) :
7979
// - Query parameters are ignored
8080
val uriWithoutQueryParameters = this.key.replace(Regex("\\{\\?[^}]+}"), "")
8181

82-
val newRegex = Regex("\\{(<groupName>[^}*]*)(<wildcard>[*]?)}")
82+
var newRegex = Regex("\\{(?<groupName>[^}*]*)(?<wildcard>\\*?)}")
8383
.replace(uriWithoutQueryParameters) { matchResult ->
8484
val groupName = matchResult.groups["groupName"]?.value
8585
checkNotNull(groupName) { "Invalid URI template: $this" }
8686
groups.add(groupName)
87-
if (matchResult.groups["wildcard"]?.value != null) {
87+
if (matchResult.groups["wildcard"]?.value?.isNotEmpty() == true) {
8888
"(?<$groupName>.+)"
8989
} else {
90-
"(?<$groupName>[^/]*)"
90+
"(?<$groupName>[^/]+)"
9191
}
9292
}
93+
newRegex =
94+
if (newRegex.endsWith('/')) {
95+
"^$newRegex?$"
96+
} else {
97+
"^$newRegex/?$"
98+
}
9399
this.value = Regex(newRegex)
94100
}
95101

@@ -99,7 +105,9 @@ public class UriTemplateFeatureKey(override val key: String) :
99105
val matchGroups = value.matchEntire(input)?.groups
100106
return groups.mapNotNull { groupName ->
101107
val groupValue = matchGroups?.get(groupName)?.value
102-
if (groupValue != null) {
108+
if (groupValue?.endsWith('/') == true) {
109+
groupName to groupValue.removeSuffix("/")
110+
} else if (groupValue != null) {
103111
groupName to groupValue
104112
} else {
105113
null

0 commit comments

Comments
 (0)