Skip to content

Commit

Permalink
Merge pull request #1260 from zalando/gh-1182-remove-successive-param…
Browse files Browse the repository at this point in the history
…s-check

Remove violation in case of successive parameters for Rule 143
  • Loading branch information
vadeg committed Jun 24, 2021
2 parents 00e9598 + b3d2920 commit a5cf2fe
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 a5cf2fe

Please sign in to comment.