Default param values (#47)

This commit is contained in:
Ryan Brink
2021-05-05 21:33:17 -04:00
committed by GitHub
parent 938a604285
commit f23016e878
42 changed files with 679 additions and 490 deletions

View File

@ -19,69 +19,27 @@ import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.webjars.Webjars
import java.net.URI
import org.leafygreens.kompendium.Kompendium
import org.leafygreens.kompendium.Notarized.notarizedDelete
import org.leafygreens.kompendium.Notarized.notarizedException
import org.leafygreens.kompendium.Notarized.notarizedGet
import org.leafygreens.kompendium.Notarized.notarizedPost
import org.leafygreens.kompendium.Notarized.notarizedPut
import org.leafygreens.kompendium.annotations.KompendiumField
import org.leafygreens.kompendium.annotations.PathParam
import org.leafygreens.kompendium.annotations.QueryParam
import org.leafygreens.kompendium.auth.KompendiumAuth.notarizedBasic
import org.leafygreens.kompendium.models.meta.MethodInfo.GetInfo
import org.leafygreens.kompendium.models.meta.MethodInfo.PostInfo
import org.leafygreens.kompendium.models.meta.MethodInfo.PutInfo
import org.leafygreens.kompendium.models.meta.MethodInfo.DeleteInfo
import org.leafygreens.kompendium.models.meta.RequestInfo
import org.leafygreens.kompendium.models.meta.ResponseInfo
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfo
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfoContact
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfoLicense
import org.leafygreens.kompendium.models.oas.OpenApiSpecServer
import org.leafygreens.kompendium.playground.KompendiumTOC.testAuthenticatedSingleGetInfo
import org.leafygreens.kompendium.playground.KompendiumTOC.testGetWithExamples
import org.leafygreens.kompendium.playground.KompendiumTOC.testIdGetInfo
import org.leafygreens.kompendium.playground.KompendiumTOC.testPostWithExamples
import org.leafygreens.kompendium.playground.KompendiumTOC.testSingleDeleteInfo
import org.leafygreens.kompendium.playground.KompendiumTOC.testSingleGetInfo
import org.leafygreens.kompendium.playground.KompendiumTOC.testSingleGetInfoWithThrowable
import org.leafygreens.kompendium.playground.KompendiumTOC.testSinglePostInfo
import org.leafygreens.kompendium.playground.KompendiumTOC.testSinglePutInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testAuthenticatedSingleGetInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testGetWithExamples
import org.leafygreens.kompendium.playground.PlaygroundToC.testIdGetInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testPostWithExamples
import org.leafygreens.kompendium.playground.PlaygroundToC.testSingleDeleteInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testSingleGetInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testSingleGetInfoWithThrowable
import org.leafygreens.kompendium.playground.PlaygroundToC.testSinglePostInfo
import org.leafygreens.kompendium.playground.PlaygroundToC.testSinglePutInfo
import org.leafygreens.kompendium.routes.openApi
import org.leafygreens.kompendium.routes.redoc
import org.leafygreens.kompendium.swagger.swaggerUI
import org.leafygreens.kompendium.util.KompendiumHttpCodes
private val oas = Kompendium.openApiSpec.copy(
info = OpenApiSpecInfo(
title = "Test API",
version = "1.33.7",
description = "An amazing, fully-ish 😉 generated API spec",
termsOfService = URI("https://example.com"),
contact = OpenApiSpecInfoContact(
name = "Homer Simpson",
email = "chunkylover53@aol.com",
url = URI("https://gph.is/1NPUDiM")
),
license = OpenApiSpecInfoLicense(
name = "MIT",
url = URI("https://github.com/lg-backbone/kompendium/blob/main/LICENSE")
)
),
servers = mutableListOf(
OpenApiSpecServer(
url = URI("https://myawesomeapi.com"),
description = "Production instance of my API"
),
OpenApiSpecServer(
url = URI("https://staging.myawesomeapi.com"),
description = "Where the fun stuff happens"
)
)
)
fun main() {
embeddedServer(
Netty,
@ -171,131 +129,9 @@ fun Application.mainModule() {
}
}
route("/error") {
notarizedGet<Unit, ExampleResponse>(testSingleGetInfoWithThrowable) {
notarizedGet(testSingleGetInfoWithThrowable) {
error("bad things just happened")
}
}
}
}
data class ExampleParams(
@PathParam val id: Int,
@QueryParam val name: String
)
data class JustQuery(
@QueryParam val potato: Boolean,
@QueryParam val tomato: String
)
data class ExampleNested(val nesty: String)
data class ExampleRequest(
@KompendiumField(name = "field_name")
val fieldName: ExampleNested,
val b: Double,
val aaa: List<Long>
)
data class ExampleResponse(val c: String)
data class ExceptionResponse(val message: String)
data class ExampleCreatedResponse(val id: Int, val c: String)
object KompendiumTOC {
val testGetWithExamples = GetInfo<Unit, ExampleResponse>(
summary = "Example Parameters",
description = "A test for setting parameter examples",
responseInfo = ResponseInfo(
status = 200,
description = "nice",
examples = mapOf("test" to ExampleResponse(c = "spud"))
),
canThrow = setOf(Exception::class)
)
@Suppress("MagicNumber")
val testPostWithExamples = PostInfo<ExampleParams, ExampleRequest, ExampleResponse>(
summary = "Full Example",
description = "Throws just about all Kompendium has to offer into one endpoint",
requestInfo = RequestInfo(
description = "Necessary deetz",
examples = mapOf(
"Send This" to ExampleRequest(ExampleNested("potato"), 13.37, listOf(12341))
)
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "Congratz you hit da endpoint",
examples = mapOf(
"Expect This" to ExampleResponse(c = "Hi"),
"Or This" to ExampleResponse(c = "Hey")
)
),
canThrow = setOf(Exception::class)
)
val testIdGetInfo = GetInfo<ExampleParams, ExampleResponse>(
summary = "Get Test",
description = "Test for the getting",
tags = setOf("test", "sample", "get"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns sample info"
)
)
val testSingleGetInfo = GetInfo<Unit, ExampleResponse>(
summary = "Another get test",
description = "testing more",
tags = setOf("anotherTest", "sample"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns a different sample"
)
)
val testSingleGetInfoWithThrowable = testSingleGetInfo.copy(
summary = "Show me the error baby 🙏",
canThrow = setOf(Exception::class)
)
val testSinglePostInfo = PostInfo<Unit, ExampleRequest, ExampleCreatedResponse>(
summary = "Test post endpoint",
description = "Post your tests here!",
requestInfo = RequestInfo(
description = "Simple request body"
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "Worlds most complex response"
)
)
val testSinglePutInfo = PutInfo<JustQuery, ExampleRequest, ExampleCreatedResponse>(
summary = "Test put endpoint",
description = "Put your tests here!",
requestInfo = RequestInfo(
description = "Info needed to perform this put request"
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "What we give you when u do the puts"
)
)
val testSingleDeleteInfo = DeleteInfo<Unit, Unit>(
summary = "Test delete endpoint",
description = "testing my deletes",
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.NO_CONTENT,
description = "Signifies that your item was deleted successfully",
mediaTypes = emptyList()
)
)
val testAuthenticatedSingleGetInfo = GetInfo<Unit, Unit>(
summary = "Another get test",
description = "testing more",
tags = setOf("anotherTest", "sample"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns a different sample"
),
securitySchemes = setOf("basic")
)
}

View File

@ -0,0 +1,30 @@
package org.leafygreens.kompendium.playground
import org.leafygreens.kompendium.annotations.KompendiumField
import org.leafygreens.kompendium.annotations.KompendiumParam
import org.leafygreens.kompendium.annotations.ParamType
data class ExampleParams(
@KompendiumParam(ParamType.PATH) val id: Int,
@KompendiumParam(ParamType.QUERY) val name: String
)
data class JustQuery(
@KompendiumParam(ParamType.QUERY) val potato: Boolean,
@KompendiumParam(ParamType.QUERY) val tomato: String
)
data class ExampleNested(val nesty: String)
data class ExampleRequest(
@KompendiumField(name = "field_name")
val fieldName: ExampleNested,
val b: Double,
val aaa: List<Long>
)
data class ExampleResponse(val c: String)
data class ExceptionResponse(val message: String)
data class ExampleCreatedResponse(val id: Int, val c: String)

View File

@ -0,0 +1,103 @@
package org.leafygreens.kompendium.playground
import org.leafygreens.kompendium.models.meta.MethodInfo
import org.leafygreens.kompendium.models.meta.RequestInfo
import org.leafygreens.kompendium.models.meta.ResponseInfo
import org.leafygreens.kompendium.util.KompendiumHttpCodes
object PlaygroundToC {
val testGetWithExamples = MethodInfo.GetInfo<Unit, ExampleResponse>(
summary = "Example Parameters",
description = "A test for setting parameter examples",
responseInfo = ResponseInfo(
status = 200,
description = "nice",
examples = mapOf("test" to ExampleResponse(c = "spud"))
),
canThrow = setOf(Exception::class)
)
@Suppress("MagicNumber")
val testPostWithExamples = MethodInfo.PostInfo<ExampleParams, ExampleRequest, ExampleResponse>(
summary = "Full Example",
description = "Throws just about all Kompendium has to offer into one endpoint",
requestInfo = RequestInfo(
description = "Necessary deetz",
examples = mapOf(
"Send This" to ExampleRequest(ExampleNested("potato"), 13.37, listOf(12341))
)
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "Congratz you hit da endpoint",
examples = mapOf(
"Expect This" to ExampleResponse(c = "Hi"),
"Or This" to ExampleResponse(c = "Hey")
)
),
canThrow = setOf(Exception::class)
)
val testIdGetInfo = MethodInfo.GetInfo<ExampleParams, ExampleResponse>(
summary = "Get Test",
description = "Test for the getting",
tags = setOf("test", "sample", "get"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns sample info"
)
)
val testSingleGetInfo = MethodInfo.GetInfo<Unit, ExampleResponse>(
summary = "Another get test",
description = "testing more",
tags = setOf("anotherTest", "sample"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns a different sample"
)
)
val testSingleGetInfoWithThrowable = testSingleGetInfo.copy(
summary = "Show me the error baby 🙏",
canThrow = setOf(Exception::class)
)
val testSinglePostInfo = MethodInfo.PostInfo<Unit, ExampleRequest, ExampleCreatedResponse>(
summary = "Test post endpoint",
description = "Post your tests here!",
requestInfo = RequestInfo(
description = "Simple request body"
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "Worlds most complex response"
)
)
val testSinglePutInfo = MethodInfo.PutInfo<JustQuery, ExampleRequest, ExampleCreatedResponse>(
summary = "Test put endpoint",
description = "Put your tests here!",
requestInfo = RequestInfo(
description = "Info needed to perform this put request"
),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.CREATED,
description = "What we give you when u do the puts"
)
)
val testSingleDeleteInfo = MethodInfo.DeleteInfo<Unit, Unit>(
summary = "Test delete endpoint",
description = "testing my deletes",
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.NO_CONTENT,
description = "Signifies that your item was deleted successfully",
mediaTypes = emptyList()
)
)
val testAuthenticatedSingleGetInfo = MethodInfo.GetInfo<Unit, Unit>(
summary = "Another get test",
description = "testing more",
tags = setOf("anotherTest", "sample"),
responseInfo = ResponseInfo(
status = KompendiumHttpCodes.OK,
description = "Returns a different sample"
),
securitySchemes = setOf("basic")
)
}

View File

@ -0,0 +1,36 @@
package org.leafygreens.kompendium.playground
import java.net.URI
import org.leafygreens.kompendium.Kompendium
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfo
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfoContact
import org.leafygreens.kompendium.models.oas.OpenApiSpecInfoLicense
import org.leafygreens.kompendium.models.oas.OpenApiSpecServer
val oas = Kompendium.openApiSpec.copy(
info = OpenApiSpecInfo(
title = "Test API",
version = "1.33.7",
description = "An amazing, fully-ish 😉 generated API spec",
termsOfService = URI("https://example.com"),
contact = OpenApiSpecInfoContact(
name = "Homer Simpson",
email = "chunkylover53@aol.com",
url = URI("https://gph.is/1NPUDiM")
),
license = OpenApiSpecInfoLicense(
name = "MIT",
url = URI("https://github.com/lg-backbone/kompendium/blob/main/LICENSE")
)
),
servers = mutableListOf(
OpenApiSpecServer(
url = URI("https://myawesomeapi.com"),
description = "Production instance of my API"
),
OpenApiSpecServer(
url = URI("https://staging.myawesomeapi.com"),
description = "Where the fun stuff happens"
)
)
)