Skip to content

Commit

Permalink
moved annotations, added @WithExample
Browse files Browse the repository at this point in the history
  • Loading branch information
Wicpar committed Mar 17, 2020
1 parent 5469c44 commit 26f9dd1
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ interface SchemaBuilder {

/**
* @throws error when type is unexpected
* MAKE SURE TO CALL FINALIZE ON THE PRODUCED OBJECT, AN NOT ON THE EVENTUAL RETURNED REF
*/
fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*>
fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*>

fun checkType(type: KType) {
if (!type.isSubtypeOf(superType)) error("${this::class} cannot build type $type, only subtypes of $superType are supported")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ object DefaultCollectionSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenM

private data class Builder(override val superType: KType, private val getter: (KType) -> KType) :
SchemaBuilder {
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
checkType(type)
return SchemaModel.SchemaModelArr<Any?>(builder.build(getter(type)), type.isMarkedNullable)
return finalize(SchemaModel.SchemaModelArr<Any?>(builder.build(getter(type)), type.isMarkedNullable))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ object DefaultEnumSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleE

private object Builder: SchemaBuilder {
override val superType: KType = getKType<Enum<*>>()
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {

override fun build(
type: KType,
builder: FinalSchemaBuilder,
finalize: (SchemaModel<*>) -> SchemaModel<*>
): SchemaModel<*> {
checkType(type)
return SchemaModel.SchemaModelEnum<Any?>(
return finalize(SchemaModel.SchemaModelEnum<Any?>(
type.jvmErasure.java.enumConstants.map { it.toString() },
type.isMarkedNullable
)
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ object DefaultMapSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleEx

private object Builder : SchemaBuilder {
override val superType: KType = getKType<Map<*, *>?>()
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
checkType(type)
if (type.arguments[0].type != getKType<String>()) error("bad type $type: Only maps with string keys are supported")
val valueType = type.arguments[1].type ?: error("bad type $type: star projected types are not supported")
@Suppress("UNCHECKED_CAST")
return SchemaModel.SchemaModelMap(
return finalize(SchemaModel.SchemaModelMap(
builder.build(valueType) as SchemaModel<Any?>,
type.isMarkedNullable
)
))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object DefaultObjectSchemaProvider : SchemaBuilderProviderModule, OpenAPIGenModu

private val refs = HashMap<KType, SchemaModel.SchemaModelRef<*>>()

override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
checkType(type)
val nonNullType = type.withNullability(false)
return refs[nonNullType] ?: {
Expand All @@ -54,9 +54,10 @@ object DefaultObjectSchemaProvider : SchemaBuilderProviderModule, OpenAPIGenModu
}.map { it.name }
)
}
val final = finalize(new)
val existing = apiGen.api.components.schemas[name]
if (existing != null && existing != new) log.error("Schema with name $name already exists, and is not the same as the new one, replacing...")
apiGen.api.components.schemas[name] = new
if (existing != null && existing != final) log.error("Schema with name $name already exists, and is not the same as the new one, replacing...")
apiGen.api.components.schemas[name] = final
ref
}()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import kotlin.reflect.KType

object DefaultPrimitiveSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleExtension {

private data class Builder(override val superType: KType, private val model: SchemaModel.SchemaModelLitteral<*>) :
SchemaBuilder {
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
private data class Builder(override val superType: KType, private val model: SchemaModel.SchemaModelLitteral<*>) : SchemaBuilder {
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
checkType(type)
return model.copy(nullable = type.isMarkedNullable)
return finalize(model.copy(nullable = type.isMarkedNullable))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object FinalSchemaBuilderProvider: FinalSchemaBuilderProviderModule, OpenAPIGenM
return map.getOrPut(type) {
map.entries.firstOrNull { type.isSubtypeOf(it.key) }?.value
?: error("Schema builder could not find declared builder for type $type, make sure it has a provider registered on the route")
}.build(type, this).applyAnnotations(type, type.jvmErasure.annotations).applyAnnotations(type, type.annotations).applyAnnotations(type, annotations)
}.build(type, this) { it.applyAnnotations(type, type.jvmErasure.annotations).applyAnnotations(type, type.annotations).applyAnnotations(type, annotations) }
}
}
}
12 changes: 11 additions & 1 deletion src/test/kotlin/TestServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.papsign.ktor.openapigen.annotations.Path
import com.papsign.ktor.openapigen.annotations.Request
import com.papsign.ktor.openapigen.annotations.Response
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
import com.papsign.ktor.openapigen.annotations.type.`object`.example.ExampleProvider
import com.papsign.ktor.openapigen.annotations.type.`object`.example.WithExample
import com.papsign.ktor.openapigen.interop.withAPI
import com.papsign.ktor.openapigen.model.Described
import com.papsign.ktor.openapigen.model.server.ServerModel
Expand Down Expand Up @@ -229,9 +231,17 @@ object TestServer {
JsonSubTypes.Type(Base.C::class, name = "c")
)
sealed class Base {

class A(val str: String) : Base()

class B(val i: @Min(0) @Max(2) Int) : Base()
class C(val l: @Clamp(0, 10) Long) : Base()

@WithExample
class C(val l: @Clamp(0, 10) Long) : Base() {
companion object: ExampleProvider<C> {
override val example: C? = C(5)
}
}
}


Expand Down

0 comments on commit 26f9dd1

Please sign in to comment.