Skip to content

Commit

Permalink
Remove violation in case of successive parameters for rule 143
Browse files Browse the repository at this point in the history
Rule 241 relaxes requirements and allows to define compound keys successive parameters.
  • Loading branch information
vadeg committed Jun 23, 2021
1 parent 750a31c commit b3d2920
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,17 @@ import org.zalando.zally.rule.api.Violation
)
class IdentifyResourcesViaPathSegments {
private val pathStartsWithParameter = "Path must start with a resource"
private val pathContainsSuccessiveParameters = "Path must not contain successive parameters"
private val pathParameterContainsPrefixOrSuffix = "Path parameter must not contain prefixes and suffixes"

private val pathStartingWithAParameter = """(^/\{[^/]+\}|/)""".toRegex()

@Check(severity = Severity.MUST)
fun pathStartsWithResource(context: Context): List<Violation> = context.validatePaths(
pathFilter = { pathStartingWithAParameter.matches(it.key) },
action = { context.violations(pathStartsWithParameter, PATHS.toJsonPointer() + it.key.toEscapedJsonPointer()) })

private val pathContainingSuccessiveParameters = """.*\}/\{.*""".toRegex()
@Check(severity = Severity.MUST)
fun pathDoesNotContainSuccessiveParameters(context: Context): List<Violation> = context.validatePaths(
pathFilter = { pathContainingSuccessiveParameters.matches(it.key) },
action = {
context.violations(
pathContainsSuccessiveParameters,
PATHS.toJsonPointer() + it.key.toEscapedJsonPointer()
)
})

private val pathContainingPrefixedOrSuffixedParameter = """.*/([^/]+\{[^/]+\}|\{[^/]+\}[^/]+).*""".toRegex()

@Check(severity = Severity.MUST)
fun pathParameterDoesNotContainPrefixAndSuffix(context: Context): List<Violation> = context.validatePaths(
pathFilter = { pathContainingPrefixedOrSuffixedParameter.matches(it.key) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,6 @@ class IdentifyResourcesViaPathSegmentsTest {
assertThat(rule.pathStartsWithResource(withPath("/orders/{order-id}"))).isEmpty()
}

@Test
fun `should return a violation if path contains successive parameters`() {
val violations = rule
.pathDoesNotContainSuccessiveParameters(withPath("/merchants/{merchant-id}/{address-id}"))

assertThat(violations).isNotEmpty
assertThat(violations[0].description).containsPattern(".*must not contain successive parameters*")
assertThat(violations[0].pointer.toString()).isEqualTo("/paths/~1merchants~1{merchant-id}~1{address-id}")
}

@Test
fun `should not return any violations if path doesn't contain successive parameters`() {
assertThat(rule.pathDoesNotContainSuccessiveParameters(withPath("/customers"))).isEmpty()
assertThat(rule.pathDoesNotContainSuccessiveParameters(withPath("/customers/{customer-id}"))).isEmpty()
assertThat(
rule.pathDoesNotContainSuccessiveParameters(
withPath("/customers/{customer-id}/addresses/primary")
)
).isEmpty()
}

@Test
fun `should return a violation if path parameter contains prefix`() {
val violations = rule.pathParameterDoesNotContainPrefixAndSuffix(
Expand Down

0 comments on commit b3d2920

Please sign in to comment.