feat: support no request body for post, put and patch (#427)

This commit is contained in:
Ryan Brink
2023-03-15 08:39:35 -04:00
committed by GitHub
parent 8cc229667e
commit 3d27d30a31
10 changed files with 126 additions and 24 deletions

View File

@ -12,6 +12,12 @@
## Released ## Released
## [3.13.0] - March 15th, 2023
### Changed
- Post, Put, and Patch now support not providing request info
## [3.12.0] - March 14th, 2023 ## [3.12.0] - March 14th, 2023
### Added ### Added

View File

@ -1,7 +1,7 @@
package io.bkbn.kompendium.core.metadata package io.bkbn.kompendium.core.metadata
sealed interface MethodInfoWithRequest : MethodInfo { sealed interface MethodInfoWithRequest : MethodInfo {
val request: RequestInfo val request: RequestInfo?
abstract class Builder<T : MethodInfoWithRequest> : MethodInfo.Builder<T>() { abstract class Builder<T : MethodInfoWithRequest> : MethodInfo.Builder<T>() {
internal var request: RequestInfo? = null internal var request: RequestInfo? = null

View File

@ -4,7 +4,7 @@ import io.bkbn.kompendium.oas.common.ExternalDocumentation
import io.bkbn.kompendium.oas.payload.Parameter import io.bkbn.kompendium.oas.payload.Parameter
class PatchInfo private constructor( class PatchInfo private constructor(
override val request: RequestInfo, override val request: RequestInfo?,
override val errors: MutableList<ResponseInfo>, override val errors: MutableList<ResponseInfo>,
override val response: ResponseInfo, override val response: ResponseInfo,
override val tags: Set<String>, override val tags: Set<String>,
@ -26,7 +26,7 @@ class PatchInfo private constructor(
class Builder : MethodInfoWithRequest.Builder<PatchInfo>() { class Builder : MethodInfoWithRequest.Builder<PatchInfo>() {
override fun build() = PatchInfo( override fun build() = PatchInfo(
request = request ?: error("request info must be present"), request = request,
errors = errors, errors = errors,
response = response ?: error("response info must be present"), response = response ?: error("response info must be present"),
tags = tags, tags = tags,

View File

@ -4,7 +4,7 @@ import io.bkbn.kompendium.oas.common.ExternalDocumentation
import io.bkbn.kompendium.oas.payload.Parameter import io.bkbn.kompendium.oas.payload.Parameter
class PostInfo private constructor( class PostInfo private constructor(
override val request: RequestInfo, override val request: RequestInfo?,
override val errors: MutableList<ResponseInfo>, override val errors: MutableList<ResponseInfo>,
override val response: ResponseInfo, override val response: ResponseInfo,
override val tags: Set<String>, override val tags: Set<String>,
@ -26,7 +26,7 @@ class PostInfo private constructor(
class Builder : MethodInfoWithRequest.Builder<PostInfo>() { class Builder : MethodInfoWithRequest.Builder<PostInfo>() {
override fun build() = PostInfo( override fun build() = PostInfo(
request = request ?: error("request info must be present"), request = request,
errors = errors, errors = errors,
response = response ?: error("response info must be present"), response = response ?: error("response info must be present"),
tags = tags, tags = tags,

View File

@ -4,7 +4,7 @@ import io.bkbn.kompendium.oas.common.ExternalDocumentation
import io.bkbn.kompendium.oas.payload.Parameter import io.bkbn.kompendium.oas.payload.Parameter
class PutInfo private constructor( class PutInfo private constructor(
override val request: RequestInfo, override val request: RequestInfo?,
override val errors: MutableList<ResponseInfo>, override val errors: MutableList<ResponseInfo>,
override val response: ResponseInfo, override val response: ResponseInfo,
override val tags: Set<String>, override val tags: Set<String>,
@ -26,7 +26,7 @@ class PutInfo private constructor(
class Builder : MethodInfoWithRequest.Builder<PutInfo>() { class Builder : MethodInfoWithRequest.Builder<PutInfo>() {
override fun build() = PutInfo( override fun build() = PutInfo(
request = request ?: error("request info must be present"), request = request,
errors = errors, errors = errors,
response = response ?: error("response info must be present"), response = response ?: error("response info must be present"),
tags = tags, tags = tags,

View File

@ -72,13 +72,15 @@ object Helpers {
when (this) { when (this) {
is MethodInfoWithRequest -> { is MethodInfoWithRequest -> {
SchemaGenerator.fromTypeOrUnit( this.request?.let { reqInfo ->
type = this.request.requestType, SchemaGenerator.fromTypeOrUnit(
cache = spec.components.schemas, type = reqInfo.requestType,
schemaConfigurator = schemaConfigurator, cache = spec.components.schemas,
enrichment = this.request.typeEnrichment, schemaConfigurator = schemaConfigurator,
)?.let { schema -> enrichment = reqInfo.typeEnrichment,
spec.components.schemas[this.request.requestType.getSlug(this.request.typeEnrichment)] = schema )?.let { schema ->
spec.components.schemas[reqInfo.requestType.getSlug(reqInfo.typeEnrichment)] = schema
}
} }
} }
@ -121,15 +123,17 @@ object Helpers {
?.map { listOf(it).toMap() } ?.map { listOf(it).toMap() }
?.toMutableList(), ?.toMutableList(),
requestBody = when (this) { requestBody = when (this) {
is MethodInfoWithRequest -> Request( is MethodInfoWithRequest -> this.request?.let { reqInfo ->
description = this.request.description, Request(
content = this.request.requestType.toReferenceContent( description = reqInfo.description,
examples = this.request.examples, content = reqInfo.requestType.toReferenceContent(
mediaTypes = this.request.mediaTypes, examples = reqInfo.examples,
enrichment = this.request.typeEnrichment mediaTypes = reqInfo.mediaTypes,
), enrichment = reqInfo.typeEnrichment
required = true ),
) required = true
)
}
else -> null else -> null
}, },

View File

@ -48,6 +48,7 @@ import io.bkbn.kompendium.core.util.polymorphicCollectionResponse
import io.bkbn.kompendium.core.util.polymorphicException import io.bkbn.kompendium.core.util.polymorphicException
import io.bkbn.kompendium.core.util.polymorphicMapResponse import io.bkbn.kompendium.core.util.polymorphicMapResponse
import io.bkbn.kompendium.core.util.polymorphicResponse import io.bkbn.kompendium.core.util.polymorphicResponse
import io.bkbn.kompendium.core.util.postNoReqBody
import io.bkbn.kompendium.core.util.primitives import io.bkbn.kompendium.core.util.primitives
import io.bkbn.kompendium.core.util.reqRespExamples import io.bkbn.kompendium.core.util.reqRespExamples
import io.bkbn.kompendium.core.util.requiredParams import io.bkbn.kompendium.core.util.requiredParams
@ -129,6 +130,9 @@ class KompendiumTest : DescribeSpec({
it("Can override media types") { it("Can override media types") {
openApiTestAllSerializers("T0052__override_media_types.json") { overrideMediaTypes() } openApiTestAllSerializers("T0052__override_media_types.json") { overrideMediaTypes() }
} }
it("Can support a post request with no request body") {
openApiTestAllSerializers("T0065__post_no_req_body.json") { postNoReqBody() }
}
} }
describe("Route Parsing") { describe("Route Parsing") {
it("Can parse a simple path and store it under the expected route") { it("Can parse a simple path and store it under the expected route") {

View File

@ -299,3 +299,19 @@ fun Routing.overrideMediaTypes() {
} }
} }
} }
fun Routing.postNoReqBody() {
route("/no_req_body") {
install(NotarizedRoute()) {
post = PostInfo.builder {
summary(defaultPathSummary)
description(defaultPathDescription)
response {
responseType<TestResponse>()
description("Cool response")
responseCode(HttpStatusCode.Created)
}
}
}
}
}

View File

@ -0,0 +1,72 @@
{
"openapi": "3.1.0",
"jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema",
"info": {
"title": "Test API",
"version": "1.33.7",
"description": "An amazing, fully-ish 😉 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/bkbnio/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": {
"/no_req_body": {
"post": {
"tags": [],
"summary": "Great Summary!",
"description": "testing more",
"parameters": [],
"responses": {
"201": {
"description": "Cool response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestResponse"
}
}
}
}
},
"deprecated": false
},
"parameters": []
}
},
"webhooks": {},
"components": {
"schemas": {
"TestResponse": {
"type": "object",
"properties": {
"c": {
"type": "string"
}
},
"required": [
"c"
]
}
},
"securitySchemes": {}
},
"security": [],
"tags": []
}

View File

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