-
Notifications
You must be signed in to change notification settings - Fork 141
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
feat(server): migrate nested paths may be root paths rule to use context object to support OpenAPI 3 #829
Merged
maxim-tschumak
merged 4 commits into
master
from
714-migrate-nested-paths-may-be-root-paths-rule
Sep 10, 2018
Merged
feat(server): migrate nested paths may be root paths rule to use context object to support OpenAPI 3 #829
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3748257
feat(server): migrate nested paths may be root paths rule to use cont…
maxim-tschumak a6cf2cf
Merge branch 'master' into 714-migrate-nested-paths-may-be-root-paths…
tkrop d354c6a
Merge branch 'master' into 714-migrate-nested-paths-may-be-root-paths…
tkrop 053c3c6
feat(server): minor test imrovements (#714)
maxim-tschumak File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
25 changes: 11 additions & 14 deletions
25
server/src/main/java/de/zalando/zally/rule/zalando/NestedPathsMayBeRootPathsRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import de.zalando.zally.rule.api.Check | ||
import de.zalando.zally.rule.api.Context | ||
import de.zalando.zally.rule.api.Rule | ||
import de.zalando.zally.rule.api.Severity | ||
import de.zalando.zally.rule.api.Violation | ||
import de.zalando.zally.util.PatternUtil.isPathVariable | ||
import io.swagger.models.Swagger | ||
|
||
@Rule( | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "145", | ||
severity = Severity.MAY, | ||
title = "Consider Using (Non-) Nested URLs" | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "145", | ||
severity = Severity.MAY, | ||
title = "Consider Using (Non-) Nested URLs" | ||
) | ||
class NestedPathsMayBeRootPathsRule { | ||
private val description = "Nested paths / URLs may be top-level resource" | ||
private val description = "Nested paths may be top-level resource" | ||
|
||
@Check(severity = Severity.MAY) | ||
fun validate(swagger: Swagger): Violation? { | ||
val paths = swagger.paths.orEmpty().keys.filter { | ||
val pathSegments = it.split("/".toRegex()) | ||
// we are only interested in paths that have sub-resource followed by a param: /path1/{param1}/path2/{param2} | ||
pathSegments.size > 4 && isPathVariable(pathSegments.last()) | ||
} | ||
return if (paths.isNotEmpty()) Violation(description, paths) else null | ||
} | ||
fun checkNestedPaths(context: Context): List<Violation> = | ||
context.api.paths.orEmpty().entries | ||
.map { (path, pathEntry) -> Pair(pathEntry, path.split("/").filter { isPathVariable(it) }.count()) } | ||
.filter { (_, numberOfPathParameters) -> numberOfPathParameters > 1 } | ||
.map { (path, _) -> context.violation(description, path) } | ||
} |
37 changes: 29 additions & 8 deletions
37
server/src/test/java/de/zalando/zally/rule/zalando/NestedPathsMayBeRootPathsRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,44 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import de.zalando.zally.getFixture | ||
import de.zalando.zally.getOpenApiContextFromContent | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
|
||
class NestedPathsMayBeRootPathsRuleTest { | ||
|
||
private val rule = NestedPathsMayBeRootPathsRule() | ||
|
||
@Test | ||
fun avoidLinkHeadersValidJson() { | ||
val swagger = getFixture("api_spp.json") | ||
val result = rule.validate(swagger)!! | ||
assertThat(result.paths).hasSameElementsAs(listOf("/products/{product_id}/updates/{update_id}")) | ||
fun `checkNestedPaths should return violations for paths containing nested sub resources`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: 3.0.1 | ||
paths: | ||
"/countries/{country-id}/cities/{city-id}": {} | ||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
val violations = rule.checkNestedPaths(context) | ||
|
||
assertThat(violations).isNotEmpty | ||
assertThat(violations[0].description).contains("may be top-level resource") | ||
assertThat(violations[0].pointer.toString()) | ||
.isEqualTo("/paths/~1countries~1{country-id}~1cities~1{city-id}") | ||
} | ||
|
||
@Test | ||
fun avoidLinkHeadersValidYaml() { | ||
val swagger = getFixture("api_spa.yaml") | ||
assertThat(rule.validate(swagger)).isNull() | ||
fun `checkNestedPaths should return no violations if there are no paths containing nested sub resources`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: 3.0.1 | ||
paths: | ||
pets/: {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing check for |
||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
val violations = rule.checkNestedPaths(context) | ||
|
||
assertThat(violations).isEmpty() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also check for something like
/countries/{country-id}/populated/cities/{city-id}
.