-
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.
Browse files
Browse the repository at this point in the history
Add validation of required path parameters
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
.../zally-ruleset-zally/src/main/kotlin/org/zalando/zally/ruleset/zally/PathParameterRule.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,32 @@ | ||
package org.zalando.zally.ruleset.zally | ||
|
||
import org.zalando.zally.core.util.getAllParameters | ||
import org.zalando.zally.core.util.isInPath | ||
import org.zalando.zally.rule.api.Check | ||
import org.zalando.zally.rule.api.Context | ||
import org.zalando.zally.rule.api.Rule | ||
import org.zalando.zally.rule.api.Severity | ||
import org.zalando.zally.rule.api.Violation | ||
|
||
@Rule( | ||
ruleSet = ZallyRuleSet::class, | ||
id = "Z001", | ||
severity = Severity.MUST, | ||
title = "Path parameters must have 'required' attribute" | ||
) | ||
class PathParameterRule { | ||
|
||
companion object { | ||
const val ERROR_MESSAGE = "Parameter with location \"path\" must have an attribute \"required=true\" set" | ||
} | ||
|
||
@Check(severity = Severity.MUST) | ||
fun validate(context: Context): List<Violation> = | ||
context.api.getAllParameters().map { entry -> entry.value } | ||
.filter { parameter -> | ||
parameter.isInPath() && !parameter.required | ||
} | ||
.map { | ||
context.violation(ERROR_MESSAGE, it) | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...ly-ruleset-zally/src/test/kotlin/org/zalando/zally/ruleset/zally/PathParameterRuleTest.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,120 @@ | ||
package org.zalando.zally.ruleset.zally | ||
|
||
import org.intellij.lang.annotations.Language | ||
import org.junit.Test | ||
import org.zalando.zally.core.DefaultContextFactory | ||
import org.zalando.zally.test.ZallyAssertions.assertThat | ||
|
||
class PathParameterRuleTest { | ||
|
||
private val rule = PathParameterRule() | ||
|
||
@Test | ||
internal fun `return violation if a path parameter is not marked as "required"`() { | ||
@Language("YAML") | ||
val context = DefaultContextFactory().getOpenApiContext( | ||
""" | ||
openapi: '3.0.0' | ||
info: | ||
title: API 1 | ||
contact: | ||
info: "Team One" | ||
paths: | ||
/items/{item-id}: | ||
get: | ||
parameters: | ||
- name: item-id | ||
in: path | ||
description: The id of the pet to retrieve | ||
schema: | ||
type: string | ||
responses: | ||
default: | ||
description: Response | ||
content: | ||
application/json: | ||
schema: | ||
type: string | ||
""".trimIndent() | ||
) | ||
|
||
val violations = rule.validate(context) | ||
|
||
assertThat(violations) | ||
.descriptionsAllEqualTo(PathParameterRule.ERROR_MESSAGE) | ||
.pointersEqualTo("/paths/~1items~1{item-id}/get/parameters/0") | ||
} | ||
|
||
@Test | ||
fun `return no violations if a path parameter marked as "required"`() { | ||
@Language("YAML") | ||
val context = DefaultContextFactory().getOpenApiContext( | ||
""" | ||
openapi: '3.0.0' | ||
info: | ||
title: API 1 | ||
contact: | ||
info: "Team One" | ||
paths: | ||
/items/{item-id}: | ||
get: | ||
parameters: | ||
- name: item-id | ||
in: path | ||
required: true | ||
description: The id of the pet to retrieve | ||
schema: | ||
type: string | ||
responses: | ||
default: | ||
description: Response | ||
content: | ||
application/json: | ||
schema: | ||
type: string | ||
""".trimIndent() | ||
) | ||
val violations = rule.validate(context) | ||
assertThat(violations).isEmpty() | ||
} | ||
|
||
@Test | ||
internal fun `return violation if a path parameter in components is not marked as "required"`() { | ||
@Language("YAML") | ||
val context = DefaultContextFactory().getOpenApiContext( | ||
""" | ||
openapi: '3.0.0' | ||
info: | ||
title: API 1 | ||
contact: | ||
info: "Team One" | ||
paths: | ||
/items/{item-id}: | ||
get: | ||
parameters: | ||
- $\ref: "#/components/parameters/QueryParameter" | ||
responses: | ||
default: | ||
description: Response | ||
content: | ||
application/json: | ||
schema: | ||
type: string | ||
components: | ||
parameters: | ||
QueryParameter: | ||
name: item-id | ||
in: path | ||
description: The id of the pet to retrieve | ||
schema: | ||
type: string | ||
""".trimIndent() | ||
) | ||
|
||
val violations = rule.validate(context) | ||
|
||
assertThat(violations) | ||
.descriptionsAllEqualTo(PathParameterRule.ERROR_MESSAGE) | ||
.pointersEqualTo("/components/parameters/QueryParameter") | ||
} | ||
} |