-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): migrate extensible enum rule to context object to suppo…
…rt openapi 3 (#714)
- Loading branch information
1 parent
71bdd29
commit ae81305
Showing
5 changed files
with
88 additions
and
1,195 deletions.
There are no files selected for viewing
82 changes: 12 additions & 70 deletions
82
server/src/main/java/de/zalando/zally/rule/zalando/ExtensibleEnumRule.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,83 +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.Severity | ||
import de.zalando.zally.rule.api.Violation | ||
import de.zalando.zally.rule.api.Rule | ||
import io.swagger.models.Operation | ||
import io.swagger.models.Swagger | ||
import io.swagger.models.parameters.Parameter | ||
import io.swagger.models.parameters.SerializableParameter | ||
import io.swagger.models.properties.BinaryProperty | ||
import io.swagger.models.properties.BooleanProperty | ||
import io.swagger.models.properties.DateProperty | ||
import io.swagger.models.properties.DateTimeProperty | ||
import io.swagger.models.properties.DoubleProperty | ||
import io.swagger.models.properties.EmailProperty | ||
import io.swagger.models.properties.FloatProperty | ||
import io.swagger.models.properties.IntegerProperty | ||
import io.swagger.models.properties.LongProperty | ||
import io.swagger.models.properties.PasswordProperty | ||
import io.swagger.models.properties.Property | ||
import io.swagger.models.properties.StringProperty | ||
import de.zalando.zally.util.getAllTransitiveSchemas | ||
|
||
@Rule( | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "107", | ||
severity = Severity.SHOULD, | ||
title = "Prefer Compatible Extensions" | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "107", | ||
severity = Severity.SHOULD, | ||
title = "Prefer Compatible Extensions" | ||
) | ||
class ExtensibleEnumRule { | ||
|
||
@Check(severity = Severity.SHOULD) | ||
fun validate(swagger: Swagger): Violation? { | ||
val properties = enumProperties(swagger) | ||
val parameters = enumParameters(swagger) | ||
|
||
val enumNames = (properties.keys + parameters.keys).distinct() | ||
val enumPaths = (properties.values + parameters.values).distinct() | ||
return if (enumNames.isNotEmpty()) Violation( | ||
"Properties/Parameters $enumNames are not extensible enums", enumPaths) | ||
else null | ||
} | ||
|
||
private fun enumProperties(swagger: Swagger): Map<String, String> = | ||
swagger.definitions.orEmpty().flatMap { (defName, model) -> | ||
val enumProps = model.properties.orEmpty().filter { (_, prop) -> prop.isEnum() } | ||
enumProps.map { (propName, _) -> propName to "/definitions/$defName/properties/$propName" } | ||
}.toMap() | ||
|
||
private fun enumParameters(swagger: Swagger): Map<String, String> { | ||
val pathsOperationsAndEnums = swagger.paths.orEmpty().map { (pathName, path) -> | ||
pathName to path.operationMap.orEmpty().map { (opName, op) -> opName to op.getEnumParameters() }.toMap() | ||
}.toMap() | ||
|
||
return pathsOperationsAndEnums | ||
.filter { (_, opAndEnums) -> opAndEnums.isNotEmpty() } | ||
.flatMap { (pathName, opAndEnums) -> opAndEnums.map { (op, enums) -> "/paths$pathName/$op" to enums } } | ||
.flatMap { (operationPath, enums) -> enums.map { it to "$operationPath/parameters/$it" } }.toMap() | ||
} | ||
val description = "Property is not an extensible enum (use `x-extensible-enum` instead)" | ||
|
||
private fun Property.isEnum(): Boolean = when (this) { | ||
is StringProperty -> this.enum.hasValues() | ||
is BinaryProperty -> this.enum.hasValues() | ||
is DateProperty -> this.enum.hasValues() | ||
is DateTimeProperty -> this.enum.hasValues() | ||
is BooleanProperty -> this.enum.hasValues() | ||
is DoubleProperty -> this.enum.hasValues() | ||
is EmailProperty -> this.enum.hasValues() | ||
is FloatProperty -> this.enum.hasValues() | ||
is IntegerProperty -> this.enum.hasValues() | ||
is LongProperty -> this.enum.hasValues() | ||
is PasswordProperty -> this.enum.hasValues() | ||
else -> false | ||
} | ||
|
||
private fun <T> List<T>?.hasValues(): Boolean { | ||
return this.orEmpty().isNotEmpty() | ||
} | ||
|
||
private fun Operation?.getEnumParameters() = this?.parameters.orEmpty().filter { it.isEnum() }.map { it.name } | ||
|
||
private fun Parameter?.isEnum() = (this as? SerializableParameter)?.enum?.orEmpty()?.isNotEmpty() ?: false | ||
@Check(severity = Severity.SHOULD) | ||
fun checkForEnums(context: Context): List<Violation> = | ||
context.api.getAllTransitiveSchemas() | ||
.filter { it.enum != null && it.enum.isNotEmpty() } | ||
.map { context.violation(description, it) } | ||
} |
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
Oops, something went wrong.