-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #821 from zalando/714-migrate-limit-number-of-sub-…
…resources-rule feat(server): migrate limit number of subresources rule to context object to support openapi 3
- Loading branch information
Showing
10 changed files
with
75 additions
and
329 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
server/src/main/java/de/zalando/zally/rule/zalando/LimitNumberOfSubResourcesRule.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import com.typesafe.config.Config | ||
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 | ||
import org.springframework.beans.factory.annotation.Autowired | ||
|
||
@Rule( | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "147", | ||
severity = Severity.SHOULD, | ||
title = "Limit number of Sub-resources level" | ||
) | ||
class LimitNumberOfSubResourcesRule(@Autowired rulesConfig: Config) { | ||
private val subResourcesLimit = rulesConfig.getConfig(javaClass.simpleName).getInt("subresources_limit") | ||
private val description = "Number of sub-resources should not exceed $subResourcesLimit" | ||
|
||
@Check(severity = Severity.SHOULD) | ||
fun checkNumberOfSubResources(context: Context): List<Violation> = | ||
context.api.paths.orEmpty().entries | ||
.map { (path, pathObj) -> Pair(path.split("/").filter { it.isNotEmpty() && !PatternUtil.isPathVariable(it) }.size - 1, pathObj) } | ||
.filter { (numberOfSubResources, _) -> numberOfSubResources > subResourcesLimit } | ||
.map { (_, pathObj) -> context.violation(description, pathObj) } | ||
} |
32 changes: 0 additions & 32 deletions
32
server/src/main/java/de/zalando/zally/rule/zalando/LimitNumberOfSubresourcesRule.kt
This file was deleted.
Oops, something went wrong.
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
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
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
44 changes: 44 additions & 0 deletions
44
server/src/test/java/de/zalando/zally/rule/zalando/LimitNumberOfSubResourcesRuleTest.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import de.zalando.zally.getOpenApiContextFromContent | ||
import de.zalando.zally.testConfig | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
|
||
class LimitNumberOfSubResourcesRuleTest { | ||
|
||
private val rule = LimitNumberOfSubResourcesRule(testConfig) | ||
|
||
@Test | ||
fun `checkNumberOfSubResources should return violation if number of sub resources exceeds the limit`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: "3.0.0" | ||
paths: | ||
/worlds/{world-id}/countries/{country-id}/states/{state-id}/cities/{city-id}/streets/{street-id}: {} | ||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
val violations = rule.checkNumberOfSubResources(context) | ||
|
||
assertThat(violations).isNotEmpty | ||
assertThat(violations[0].description).containsPattern("Number of sub-resources should not exceed") | ||
assertThat(violations[0].pointer.toString()).isEqualTo("/paths/~1worlds~1{world-id}~1countries~1{country-id}~1states~1{state-id}~1cities~1{city-id}~1streets~1{street-id}") | ||
} | ||
|
||
@Test | ||
fun `checkNumberOfSubResources should return no violation if number of sub resources doesn not exceed the limit`() { | ||
@Language("YAML") | ||
val spec = """ | ||
openapi: 3.0.1 | ||
paths: | ||
/articles: {} | ||
""".trimIndent() | ||
val context = getOpenApiContextFromContent(spec) | ||
|
||
val violations = rule.checkNumberOfSubResources(context) | ||
|
||
assertThat(violations).isEmpty() | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
server/src/test/java/de/zalando/zally/rule/zalando/LimitNumberOfSubresourcesRuleTest.kt
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
server/src/test/resources/fixtures/limitNumberOfSubresourcesInvalid.json
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
server/src/test/resources/fixtures/limitNumberOfSubresourcesInvalidButIgnored.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.