Skip to content

Commit

Permalink
Check for correct api version and add extra test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Nov 4, 2023
1 parent ef0ebf8 commit d429699
Show file tree
Hide file tree
Showing 6 changed files with 10,473 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "community.flock.kotlinx.openapi.bindings"
version = "0.0.17"
version = "0.0.18"


val dokkaOutputDir = "$buildDir/dokka"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ sealed class OpenAPI(

fun decodeFromString(string: String): SwaggerObject {
return json
.decodeFromString<JsonElement>(string)
.decodeFromString<JsonObject>(string)
.validate()
.traverse({ path, obj -> obj.encodeExtensions(path) })
.let { json.decodeFromJsonElement(it) }
}
Expand All @@ -47,6 +48,12 @@ sealed class OpenAPI(
companion object Default : OpenAPI()
}

private fun JsonObject.validate() = apply {
if (!containsKey("swagger")){
error("No valid openapi v2 element 'swagger' is missing")
}
}

private fun JsonObject.encodeExtensions(path: String): JsonObject {
val (known, unknown) = this.toList()
.partition { !regex.hasMatchedRegex("$path${it.first}|") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ sealed class OpenAPI(

fun decodeFromString(string: String): OpenAPIObject {
return json
.decodeFromString<JsonElement>(string)
.decodeFromString<JsonObject>(string)
.validate()
.traverse({ path, obj -> obj.encodeExtensions(path) })
.let { json.decodeFromJsonElement(it) }
}
Expand All @@ -47,6 +48,12 @@ sealed class OpenAPI(
companion object Default : OpenAPI()
}

private fun JsonObject.validate() = apply {
if (!containsKey("openapi")){
error("No valid openapi v3 element 'openapi' is missing")
}
}

private fun JsonObject.encodeExtensions(path: String): JsonObject {
val (known, unknown) = this.toList()
.partition { !regex.hasMatchedRegex("$path${it.first}|") }
Expand Down
Loading

0 comments on commit d429699

Please sign in to comment.