Skip to content

Commit 26f9dd1

Browse files
committed
moved annotations, added @WithExample
1 parent 5469c44 commit 26f9dd1

File tree

8 files changed

+34
-18
lines changed

8 files changed

+34
-18
lines changed

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/SchemaBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ interface SchemaBuilder {
1212

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

1819
fun checkType(type: KType) {
1920
if (!type.isSubtypeOf(superType)) error("${this::class} cannot build type $type, only subtypes of $superType are supported")

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultCollectionSchemaProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ object DefaultCollectionSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenM
4040

4141
private data class Builder(override val superType: KType, private val getter: (KType) -> KType) :
4242
SchemaBuilder {
43-
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
43+
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
4444
checkType(type)
45-
return SchemaModel.SchemaModelArr<Any?>(builder.build(getter(type)), type.isMarkedNullable)
45+
return finalize(SchemaModel.SchemaModelArr<Any?>(builder.build(getter(type)), type.isMarkedNullable))
4646
}
4747
}
4848
}

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultEnumSchemaProvider.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ object DefaultEnumSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleE
1414

1515
private object Builder: SchemaBuilder {
1616
override val superType: KType = getKType<Enum<*>>()
17-
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
17+
18+
override fun build(
19+
type: KType,
20+
builder: FinalSchemaBuilder,
21+
finalize: (SchemaModel<*>) -> SchemaModel<*>
22+
): SchemaModel<*> {
1823
checkType(type)
19-
return SchemaModel.SchemaModelEnum<Any?>(
24+
return finalize(SchemaModel.SchemaModelEnum<Any?>(
2025
type.jvmErasure.java.enumConstants.map { it.toString() },
2126
type.isMarkedNullable
22-
)
27+
))
2328
}
2429
}
2530

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultMapSchemaProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ object DefaultMapSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleEx
1717

1818
private object Builder : SchemaBuilder {
1919
override val superType: KType = getKType<Map<*, *>?>()
20-
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
20+
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
2121
checkType(type)
2222
if (type.arguments[0].type != getKType<String>()) error("bad type $type: Only maps with string keys are supported")
2323
val valueType = type.arguments[1].type ?: error("bad type $type: star projected types are not supported")
2424
@Suppress("UNCHECKED_CAST")
25-
return SchemaModel.SchemaModelMap(
25+
return finalize(SchemaModel.SchemaModelMap(
2626
builder.build(valueType) as SchemaModel<Any?>,
2727
type.isMarkedNullable
28-
)
28+
))
2929
}
3030
}
3131
}

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultObjectSchemaProvider.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object DefaultObjectSchemaProvider : SchemaBuilderProviderModule, OpenAPIGenModu
3333

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

36-
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
36+
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
3737
checkType(type)
3838
val nonNullType = type.withNullability(false)
3939
return refs[nonNullType] ?: {
@@ -54,9 +54,10 @@ object DefaultObjectSchemaProvider : SchemaBuilderProviderModule, OpenAPIGenModu
5454
}.map { it.name }
5555
)
5656
}
57+
val final = finalize(new)
5758
val existing = apiGen.api.components.schemas[name]
58-
if (existing != null && existing != new) log.error("Schema with name $name already exists, and is not the same as the new one, replacing...")
59-
apiGen.api.components.schemas[name] = new
59+
if (existing != null && existing != final) log.error("Schema with name $name already exists, and is not the same as the new one, replacing...")
60+
apiGen.api.components.schemas[name] = final
6061
ref
6162
}()
6263
}

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultPrimitiveSchemaProvider.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import kotlin.reflect.KType
1818

1919
object DefaultPrimitiveSchemaProvider: SchemaBuilderProviderModule, OpenAPIGenModuleExtension {
2020

21-
private data class Builder(override val superType: KType, private val model: SchemaModel.SchemaModelLitteral<*>) :
22-
SchemaBuilder {
23-
override fun build(type: KType, builder: FinalSchemaBuilder): SchemaModel<*> {
21+
private data class Builder(override val superType: KType, private val model: SchemaModel.SchemaModelLitteral<*>) : SchemaBuilder {
22+
override fun build(type: KType, builder: FinalSchemaBuilder, finalize: (SchemaModel<*>)->SchemaModel<*>): SchemaModel<*> {
2423
checkType(type)
25-
return model.copy(nullable = type.isMarkedNullable)
24+
return finalize(model.copy(nullable = type.isMarkedNullable))
2625
}
2726

2827
companion object {

src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/FinalSchemaBuilderProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object FinalSchemaBuilderProvider: FinalSchemaBuilderProviderModule, OpenAPIGenM
6767
return map.getOrPut(type) {
6868
map.entries.firstOrNull { type.isSubtypeOf(it.key) }?.value
6969
?: error("Schema builder could not find declared builder for type $type, make sure it has a provider registered on the route")
70-
}.build(type, this).applyAnnotations(type, type.jvmErasure.annotations).applyAnnotations(type, type.annotations).applyAnnotations(type, annotations)
70+
}.build(type, this) { it.applyAnnotations(type, type.jvmErasure.annotations).applyAnnotations(type, type.annotations).applyAnnotations(type, annotations) }
7171
}
7272
}
7373
}

src/test/kotlin/TestServer.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import com.papsign.ktor.openapigen.annotations.Path
1313
import com.papsign.ktor.openapigen.annotations.Request
1414
import com.papsign.ktor.openapigen.annotations.Response
1515
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
16+
import com.papsign.ktor.openapigen.annotations.type.`object`.example.ExampleProvider
17+
import com.papsign.ktor.openapigen.annotations.type.`object`.example.WithExample
1618
import com.papsign.ktor.openapigen.interop.withAPI
1719
import com.papsign.ktor.openapigen.model.Described
1820
import com.papsign.ktor.openapigen.model.server.ServerModel
@@ -229,9 +231,17 @@ object TestServer {
229231
JsonSubTypes.Type(Base.C::class, name = "c")
230232
)
231233
sealed class Base {
234+
232235
class A(val str: String) : Base()
236+
233237
class B(val i: @Min(0) @Max(2) Int) : Base()
234-
class C(val l: @Clamp(0, 10) Long) : Base()
238+
239+
@WithExample
240+
class C(val l: @Clamp(0, 10) Long) : Base() {
241+
companion object: ExampleProvider<C> {
242+
override val example: C? = C(5)
243+
}
244+
}
235245
}
236246

237247

0 commit comments

Comments
 (0)