Feature/non required params (#35)

This commit is contained in:
dpnolte
2021-04-23 17:53:11 +02:00
committed by GitHub
parent 01917b2ade
commit 30f02f88df
5 changed files with 99 additions and 2 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## [0.6.2] - April 23rd, 2021
### Added
- Request params are not required when property is nullable
## [0.6.1] - April 23rd, 2021
### Added

View File

@ -1,5 +1,5 @@
# Kompendium
project.version=0.6.1
project.version=0.6.2
# Kotlin
kotlin.code.style=official
# Gradle

View File

@ -182,7 +182,8 @@ object Kompendium {
is HeaderParam -> anny.description.ifBlank { null }
is CookieParam -> anny.description.ifBlank { null }
else -> error("should not be reachable")
}
},
required = !prop.returnType.isMarkedNullable
)
}
}

View File

@ -23,6 +23,7 @@ import org.leafygreens.kompendium.Kompendium.notarizedDelete
import org.leafygreens.kompendium.Kompendium.notarizedGet
import org.leafygreens.kompendium.Kompendium.notarizedPost
import org.leafygreens.kompendium.Kompendium.notarizedPut
import org.leafygreens.kompendium.annotations.QueryParam
import org.leafygreens.kompendium.models.meta.MethodInfo
import org.leafygreens.kompendium.models.meta.RequestInfo
import org.leafygreens.kompendium.models.meta.ResponseInfo
@ -340,6 +341,22 @@ internal class KompendiumTest {
}
}
@Test
fun `Can notarize route with non-required params`() {
withTestApplication({
configModule()
docs()
nonRequiredParamsGet()
}) {
// do
val json = handleRequest(HttpMethod.Get, "/openapi.json").response.content
// expect
val expected = TestData.getFileSnapshot("non_required_params.json").trim()
assertEquals(expected, json, "The received json spec should match the expected content")
}
}
@Test
fun `Generates the expected redoc`() {
withTestApplication({
@ -513,6 +530,17 @@ internal class KompendiumTest {
}
}
data class OptionalParams(@QueryParam val required: String, @QueryParam val notRequired: String?)
private fun Application.nonRequiredParamsGet() {
routing {
route("/test/optional") {
notarizedGet<OptionalParams, Unit>(emptyTestGetInfo) {
call.respond(HttpStatusCode.OK)
}
}
}
}
private val oas = Kompendium.openApiSpec.copy(
info = OpenApiSpecInfo(
title = "Test API",

View File

@ -0,0 +1,62 @@
{
"openapi" : "3.0.3",
"info" : {
"title" : "Test API",
"version" : "1.33.7",
"description" : "An amazing, fully-ish \uD83D\uDE09 generated API spec",
"termsOfService" : "https://example.com",
"contact" : {
"name" : "Homer Simpson",
"url" : "https://gph.is/1NPUDiM",
"email" : "chunkylover53@aol.com"
},
"license" : {
"name" : "MIT",
"url" : "https://github.com/lg-backbone/kompendium/blob/main/LICENSE"
}
},
"servers" : [ {
"url" : "https://myawesomeapi.com",
"description" : "Production instance of my API"
}, {
"url" : "https://staging.myawesomeapi.com",
"description" : "Where the fun stuff happens"
} ],
"paths" : {
"/test/optional" : {
"get" : {
"tags" : [ ],
"summary" : "No request params and response body",
"description" : "testing more",
"parameters" : [ {
"name" : "notRequired",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/String"
},
"required" : false,
"deprecated" : false
}, {
"name" : "required",
"in" : "query",
"schema" : {
"$ref" : "#/components/schemas/String"
},
"required" : true,
"deprecated" : false
} ],
"deprecated" : false
}
}
},
"components" : {
"schemas" : {
"String" : {
"type" : "string"
}
},
"securitySchemes" : { }
},
"security" : [ ],
"tags" : [ ]
}