-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from papsign/experimental-parameters
Experimental parameters
- Loading branch information
Showing
81 changed files
with
1,694 additions
and
118 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#Mon Mar 02 14:17:20 CET 2020 | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME |
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
8 changes: 7 additions & 1 deletion
8
src/main/kotlin/com/papsign/ktor/openapigen/annotations/parameters/PathParam.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,8 +1,14 @@ | ||
package com.papsign.ktor.openapigen.annotations.parameters | ||
|
||
import com.papsign.ktor.openapigen.openapi.ParameterLocation | ||
import com.papsign.ktor.openapigen.parameters.PathParamStyle | ||
|
||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@APIParam(ParameterLocation.path) | ||
annotation class PathParam(val description: String, val required: Boolean = true, val deprecated: Boolean = false) | ||
annotation class PathParam( | ||
val description: String, | ||
val style: PathParamStyle = PathParamStyle.simple, | ||
val explode: Boolean = false, | ||
val deprecated: Boolean = false | ||
) |
9 changes: 8 additions & 1 deletion
9
src/main/kotlin/com/papsign/ktor/openapigen/annotations/parameters/QueryParam.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,8 +1,15 @@ | ||
package com.papsign.ktor.openapigen.annotations.parameters | ||
|
||
import com.papsign.ktor.openapigen.openapi.ParameterLocation | ||
import com.papsign.ktor.openapigen.parameters.QueryParamStyle | ||
|
||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@APIParam(ParameterLocation.query) | ||
annotation class QueryParam(val description: String, val allowEmptyValues: Boolean = false, val required: Boolean = true, val deprecated: Boolean = false) | ||
annotation class QueryParam( | ||
val description: String, | ||
val style: QueryParamStyle = QueryParamStyle.form, | ||
val explode: Boolean = true, | ||
val allowEmptyValues: Boolean = false, | ||
val deprecated: Boolean = false | ||
) |
55 changes: 0 additions & 55 deletions
55
src/main/kotlin/com/papsign/ktor/openapigen/generator/ParamBuilder.kt
This file was deleted.
Oops, something went wrong.
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
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
7 changes: 4 additions & 3 deletions
7
src/main/kotlin/com/papsign/ktor/openapigen/modules/providers/ParameterProvider.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,9 +1,10 @@ | ||
package com.papsign.ktor.openapigen.modules.providers | ||
|
||
import com.papsign.ktor.openapigen.generator.ParamBuilder | ||
import com.papsign.ktor.openapigen.OpenAPIGen | ||
import com.papsign.ktor.openapigen.modules.ModuleProvider | ||
import com.papsign.ktor.openapigen.modules.OpenAPIModule | ||
import com.papsign.ktor.openapigen.openapi.Parameter | ||
|
||
interface ParameterProvider: OpenAPIModule { | ||
fun getParameters(builder: ParamBuilder): List<Parameter<*>> | ||
} | ||
fun getParameters(apiGen: OpenAPIGen, provider: ModuleProvider<*>): List<Parameter<*>> | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/papsign/ktor/openapigen/parameters/ParameterStyle.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.papsign.ktor.openapigen.parameters | ||
|
||
import com.papsign.ktor.openapigen.parameters.parsers.builders.Builder | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.BuilderFactory | ||
|
||
interface ParameterStyle<S> where S: ParameterStyle<S>, S: Enum<S> { | ||
val name: String | ||
val factory: BuilderFactory<Builder<S>, S> | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/papsign/ktor/openapigen/parameters/PathParamStyle.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.papsign.ktor.openapigen.parameters | ||
|
||
import com.papsign.ktor.openapigen.parameters.parsers.builders.Builder | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.BuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.path.matrix.MatrixBuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.path.label.LabelBuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.path.simple.SimpleBuilderFactory | ||
|
||
|
||
enum class PathParamStyle(override val factory: BuilderFactory<Builder<PathParamStyle>, PathParamStyle>): ParameterStyle<PathParamStyle> { | ||
simple(SimpleBuilderFactory), label(LabelBuilderFactory), matrix(MatrixBuilderFactory) | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/papsign/ktor/openapigen/parameters/QueryParamStyle.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.papsign.ktor.openapigen.parameters | ||
|
||
import com.papsign.ktor.openapigen.parameters.parsers.builders.Builder | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.BuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.query.deepobject.DeepBuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.query.delimited.PipeDelimitedBuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.query.delimited.SpaceDelimitedBuilderFactory | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.query.form.FormBuilderFactory | ||
|
||
enum class QueryParamStyle(override val factory: BuilderFactory<Builder<QueryParamStyle>, QueryParamStyle>): ParameterStyle<QueryParamStyle> { | ||
form(FormBuilderFactory), spaceDelimited(SpaceDelimitedBuilderFactory), pipeDelimited(PipeDelimitedBuilderFactory), deepObject(DeepBuilderFactory) | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ModularParameterHander.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.papsign.ktor.openapigen.parameters.handlers | ||
|
||
import com.papsign.ktor.openapigen.OpenAPIGen | ||
import com.papsign.ktor.openapigen.annotations.parameters.PathParam | ||
import com.papsign.ktor.openapigen.annotations.parameters.QueryParam | ||
import com.papsign.ktor.openapigen.annotations.parameters.apiParam | ||
import com.papsign.ktor.openapigen.modules.ModuleProvider | ||
import com.papsign.ktor.openapigen.openapi.Parameter | ||
import com.papsign.ktor.openapigen.openapi.ParameterLocation | ||
import com.papsign.ktor.openapigen.openapi.Schema | ||
import com.papsign.ktor.openapigen.parameters.parsers.builders.Builder | ||
import io.ktor.http.Parameters | ||
import io.ktor.util.toMap | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.full.findAnnotation | ||
|
||
class ModularParameterHander<T>(val parsers: Map<KParameter, Builder<*>>, val constructor: KFunction<T>) : | ||
ParameterHandler<T> { | ||
|
||
override fun parse(parameters: Parameters): T { | ||
return constructor.callBy(parsers.mapValues { it.value.build(it.key.name!!, parameters.toMap()) }) | ||
} | ||
|
||
override fun getParameters(apiGen: OpenAPIGen, provider: ModuleProvider<*>): List<Parameter<*>> { | ||
|
||
fun createParam(param: KParameter, `in`: ParameterLocation, config: (Parameter<*>) -> Unit): Parameter<*> { | ||
return Parameter<Any>( | ||
param.name.toString(), | ||
`in`, | ||
!param.type.isMarkedNullable | ||
).also { | ||
it.schema = apiGen.schemaRegistrar[param.type].schema as Schema<Any> | ||
config(it) | ||
} | ||
} | ||
|
||
fun QueryParam.createParam(param: KParameter): Parameter<*> { | ||
val parser = parsers[param]!! | ||
return createParam(param, apiParam.`in`) { | ||
it.description = description | ||
it.allowEmptyValue = allowEmptyValues | ||
it.deprecated = deprecated | ||
it.style = parser.style | ||
it.explode = parser.explode | ||
} | ||
} | ||
|
||
fun PathParam.createParam(param: KParameter): Parameter<*> { | ||
val parser = parsers[param]!! | ||
return createParam(param, apiParam.`in`) { | ||
it.description = description | ||
it.deprecated = deprecated | ||
it.style = parser.style | ||
it.explode = parser.explode | ||
} | ||
} | ||
|
||
return constructor.parameters.map { | ||
it.findAnnotation<PathParam>()?.createParam(it) ?: | ||
it.findAnnotation<QueryParam>()?.createParam(it) ?: | ||
error("API routes with ${constructor.returnType} must have parameters annotated with one of ${paramAnnotationClasses.map { it.simpleName }}") | ||
} | ||
} | ||
|
||
companion object { | ||
private val paramAnnotationClasses = hashSetOf(PathParam::class, QueryParam::class) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/ParameterHandler.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.papsign.ktor.openapigen.parameters.handlers | ||
|
||
import com.papsign.ktor.openapigen.modules.providers.ParameterProvider | ||
import io.ktor.http.Parameters | ||
|
||
interface ParameterHandler<T>: ParameterProvider { | ||
fun parse(parameters: Parameters): T | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/papsign/ktor/openapigen/parameters/handlers/UnitParameterHandler.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.papsign.ktor.openapigen.parameters.handlers | ||
|
||
import com.papsign.ktor.openapigen.OpenAPIGen | ||
import com.papsign.ktor.openapigen.modules.ModuleProvider | ||
import com.papsign.ktor.openapigen.openapi.Parameter | ||
import com.papsign.ktor.openapigen.parameters.handlers.ParameterHandler | ||
import io.ktor.http.Parameters | ||
|
||
object UnitParameterHandler : | ||
ParameterHandler<Unit> { | ||
override fun parse(parameters: Parameters) = Unit | ||
override fun getParameters(apiGen: OpenAPIGen, provider: ModuleProvider<*>): List<Parameter<*>> { | ||
return listOf() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/papsign/ktor/openapigen/parameters/parsers/builders/Builder.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.papsign.ktor.openapigen.parameters.parsers.builders | ||
|
||
import com.papsign.ktor.openapigen.parameters.ParameterStyle | ||
|
||
interface Builder<S> where S: ParameterStyle<S>, S: Enum<S> { | ||
val style: S | ||
val explode: Boolean | ||
fun build(key: String, parameters: Map<String, List<String>>): Any? | ||
} |
Oops, something went wrong.