Skip to content

Commit

Permalink
Merge pull request #1210 from tharakadesilva/master
Browse files Browse the repository at this point in the history
Fixes for #1209
  • Loading branch information
tkrop authored Apr 8, 2021
2 parents 75c32f3 + e72e613 commit 61836f7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.zalando.zally.core.util

class OpenApiSections {
companion object {
const val PATHS = "/paths"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.zalando.zally.core.ast

import com.fasterxml.jackson.core.JsonPointer
import org.zalando.zally.core.ObjectTreeReader
import org.zalando.zally.core.toJsonPointer
import io.swagger.parser.OpenAPIParser
import io.swagger.parser.SwaggerParser
import io.swagger.util.Json
Expand All @@ -12,6 +10,9 @@ import org.apache.commons.io.IOUtils
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.Test
import org.zalando.zally.core.ObjectTreeReader
import org.zalando.zally.core.toJsonPointer
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS

@Suppress("UndocumentedPublicClass", "UndocumentedPublicFunction", "StringLiteralDuplication")
class ReverseAstTest {
Expand Down Expand Up @@ -77,7 +78,7 @@ class ReverseAstTest {
assertThat(ast.isIgnored(pointer, "*")).isTrue()
assertThat(ast.getIgnoreValues(pointer)).hasSize(1).contains("*")

pointer = "/paths".toJsonPointer()
pointer = PATHS.toJsonPointer()
assertThat(ast.isIgnored(pointer, "*")).isFalse()
assertThat(ast.getIgnoreValues(pointer)).isEmpty()

Expand Down Expand Up @@ -141,7 +142,7 @@ class ReverseAstTest {
assertThat(ast.isIgnored(pointer, "*")).isTrue()
assertThat(ast.getIgnoreValues(pointer)).hasSize(1).contains("*")

pointer = "/paths".toJsonPointer()
pointer = PATHS.toJsonPointer()
assertThat(ast.isIgnored(pointer, "*")).isFalse()
assertThat(ast.getIgnoreValues(pointer)).isEmpty()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.zalando.zally.ruleset.zalando
import org.zalando.zally.core.plus
import org.zalando.zally.core.toEscapedJsonPointer
import org.zalando.zally.core.toJsonPointer
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS
import org.zalando.zally.rule.api.Check
import org.zalando.zally.rule.api.Context
import org.zalando.zally.rule.api.Rule
Expand Down Expand Up @@ -31,6 +32,6 @@ class AvoidTrailingSlashesRule {
}
}
) { (path, _) ->
context.violations(description, "/paths".toJsonPointer() + path.toEscapedJsonPointer())
context.violations(description, PATHS.toJsonPointer() + path.toEscapedJsonPointer())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.zalando.zally.ruleset.zalando
import org.zalando.zally.core.plus
import org.zalando.zally.core.toEscapedJsonPointer
import org.zalando.zally.core.toJsonPointer
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS
import org.zalando.zally.rule.api.Check
import org.zalando.zally.rule.api.Context
import org.zalando.zally.rule.api.Rule
Expand All @@ -24,17 +25,27 @@ class IdentifyResourcesViaPathSegments {
@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()) })
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()) })
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) },
action = { context.violations(pathParameterContainsPrefixOrSuffix, "/paths".toJsonPointer() + it.key.toEscapedJsonPointer()) })
action = {
context.violations(
pathParameterContainsPrefixOrSuffix,
PATHS.toJsonPointer() + it.key.toEscapedJsonPointer()
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.typesafe.config.Config
import org.zalando.zally.core.plus
import org.zalando.zally.core.toEscapedJsonPointer
import org.zalando.zally.core.toJsonPointer
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS
import org.zalando.zally.core.util.PatternUtil
import org.zalando.zally.rule.api.Check
import org.zalando.zally.rule.api.Context
Expand Down Expand Up @@ -38,7 +39,7 @@ class PluralizeResourceNamesRule(rulesConfig: Config) {
return context.validatePaths { (path, _) ->
pathSegments(sanitizedPath(path, whitelist))
.filter { isNonViolating(it) }
.map { violation(context, it, "/paths".toJsonPointer() + path.toEscapedJsonPointer()) }
.map { violation(context, it, PATHS.toJsonPointer() + path.toEscapedJsonPointer()) }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.zalando.zally.ruleset.zalando

import com.typesafe.config.ConfigFactory
import org.zalando.zally.core.DefaultContextFactory
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.Test
import org.zalando.zally.core.DefaultContextFactory
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS

class LimitNumberOfResourcesRuleTest {

Expand Down Expand Up @@ -43,7 +44,7 @@ class LimitNumberOfResourcesRuleTest {

assertThat(violation).isNotNull
assertThat(violation!!.description).containsPattern(".*greater than recommended limit of.*")
assertThat(violation.pointer.toString()).isEqualTo("/paths")
assertThat(violation.pointer.toString()).isEqualTo(PATHS)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.zally.ruleset.zally

import org.zalando.zally.core.toJsonPointer
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS
import org.zalando.zally.rule.api.Check
import org.zalando.zally.rule.api.Context
import org.zalando.zally.rule.api.Rule
Expand Down Expand Up @@ -32,7 +33,7 @@ class ExtractBasePathRule {
private fun violations(prefix: String, target: String) = listOf(
Violation(
"All paths start with prefix '$prefix' which could be part of $target.",
"/paths".toJsonPointer()
PATHS.toJsonPointer()
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.zalando.zally.ruleset.zally

import org.zalando.zally.core.DefaultContextFactory
import org.zalando.zally.test.ZallyAssertions
import org.intellij.lang.annotations.Language
import org.junit.Test
import org.zalando.zally.core.DefaultContextFactory
import org.zalando.zally.core.util.OpenApiSections.Companion.PATHS
import org.zalando.zally.test.ZallyAssertions

class ExtractBasePathRuleTest {

Expand Down Expand Up @@ -73,7 +74,7 @@ class ExtractBasePathRuleTest {
ZallyAssertions
.assertThat(rule.validate(context))
.descriptionsEqualTo("All paths start with prefix '/shipment' which could be part of basePath.")
.pointersEqualTo("/paths")
.pointersEqualTo(PATHS)
}

@Test
Expand All @@ -93,7 +94,7 @@ class ExtractBasePathRuleTest {
ZallyAssertions
.assertThat(rule.validate(context))
.descriptionsEqualTo("All paths start with prefix '/queue/models' which could be part of basePath.")
.pointersEqualTo("/paths")
.pointersEqualTo(PATHS)
}

@Test
Expand Down Expand Up @@ -131,6 +132,6 @@ class ExtractBasePathRuleTest {
ZallyAssertions
.assertThat(rule.validate(context))
.descriptionsEqualTo("All paths start with prefix '/shipment' which could be part of servers' urls.")
.pointersEqualTo("/paths")
.pointersEqualTo(PATHS)
}
}

0 comments on commit 61836f7

Please sign in to comment.