Skip to content

Commit

Permalink
Use new Context.validateOperations(..) convenience method zalando#725
Browse files Browse the repository at this point in the history
  • Loading branch information
roxspring authored and Robert James Oxspring committed Jun 19, 2018
1 parent b27adf8 commit b4437ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
20 changes: 20 additions & 0 deletions server/src/main/java/de/zalando/zally/rule/Context.kt
Expand Up @@ -8,6 +8,7 @@ import de.zalando.zally.util.ast.ReverseAst
import io.swagger.models.Swagger
import io.swagger.parser.SwaggerParser
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.parser.OpenAPIV3Parser
import io.swagger.v3.parser.converter.SwaggerConverter
Expand All @@ -22,6 +23,25 @@ class Context(openApi: OpenAPI, swagger: Swagger? = null) {

val api = recorder.proxy

/**
* Convenience method for filtering and iterating over the operations in order to create Violations.
* @param pathFilter a filter selecting the paths to validate
* @param operationFilter a filter selecting the operations to validate
* @param action the action to perform on filtered items
* @return a list of Violations and/or nulls where no violations are necessary
*/
fun validateOperations(
pathFilter: (Map.Entry<String, PathItem>) -> Boolean = { true },
operationFilter: (Map.Entry<PathItem.HttpMethod, Operation>) -> Boolean = { true },
action: (Map.Entry<PathItem.HttpMethod, Operation>) -> List<Violation?>
): List<Violation> = validatePaths(pathFilter = pathFilter) { (_, path) ->
path.readOperationsMap()
.orEmpty()
.filter(operationFilter)
.flatMap(action)
.filterNotNull()
}

/**
* Convenience method for filtering and iterating over the paths in order to create Violations.
* @param pathFilter a filter selecting the paths to validate
Expand Down
Expand Up @@ -27,19 +27,14 @@ class UseStandardHttpStatusCodes(@Autowired rulesConfig: Config) {
.toMap()

@Check(severity = Severity.MUST)
fun allowOnlyStandardStatusCodes(context: Context): List<Violation> {
return context.api.paths.orEmpty().flatMap { (_, pathItem) ->
pathItem.readOperationsMap().orEmpty().flatMap { (method, operation) ->
operation.responses.filterNot { (statusCode, _) ->
isAllowed(method, statusCode)
}.map { (_, response) ->
response
}
}.map {
context.violation("Operations should use standard HTTP status codes", it)
fun allowOnlyStandardStatusCodes(context: Context): List<Violation> =
context.validateOperations { (method, operation) ->
operation.responses.orEmpty().filterNot { (status, _) ->
isAllowed(method, status)
}.map { (_, response) ->
context.violation("Operations should use standard HTTP status codes", response)
}
}
}

private fun isAllowed(method: PathItem.HttpMethod, statusCode: String): Boolean {
val allowedMethods = allowed[statusCode.toLowerCase()].orEmpty()
Expand Down

0 comments on commit b4437ca

Please sign in to comment.