Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove violation in case of successive parameters for Rule 143 #1260

Merged
merged 1 commit into from
Jun 24, 2021
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 @@ -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