Skip to content

Commit 6318961

Browse files
authored
Merge pull request #53 from bherbst/model_description
Support model property descriptions
2 parents e36e8e7 + 7dc9db2 commit 6318961

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.papsign.ktor.openapigen.annotations.properties.description
2+
3+
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation
4+
5+
/**
6+
* Property annotation for providing a description of a schema model property
7+
*/
8+
@Target(AnnotationTarget.PROPERTY)
9+
@SchemaProcessorAnnotation(DescriptionProcessor::class)
10+
annotation class Description(val value: String)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.papsign.ktor.openapigen.annotations.properties.description
2+
3+
import com.papsign.ktor.openapigen.model.schema.SchemaModel
4+
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessor
5+
import kotlin.reflect.KType
6+
7+
object DescriptionProcessor: SchemaProcessor<Description> {
8+
override fun process(model: SchemaModel<*>, type: KType, annotation: Description): SchemaModel<*> {
9+
model.description = annotation.value
10+
return model
11+
}
12+
}

src/main/kotlin/com/papsign/ktor/openapigen/model/schema/SchemaModel.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ sealed class SchemaModel<T>: DataModel {
77

88
abstract var example: T?
99
abstract var examples: List<T>?
10+
abstract var description: String?
1011

1112
data class SchemaModelObj<T>(
1213
var properties: Map<String, SchemaModel<*>>,
1314
var required: List<String>,
1415
var nullable: Boolean = false,
1516
override var example: T? = null,
1617
override var examples: List<T>? = null,
17-
var type: DataType = DataType.`object`
18+
var type: DataType = DataType.`object`,
19+
override var description: String? = null
1820
) : SchemaModel<T>()
1921

2022
data class SchemaModelMap<T : Map<String, U>, U>(
2123
var additionalProperties: SchemaModel<U>,
2224
var nullable: Boolean = false,
2325
override var example: T? = null,
2426
override var examples: List<T>? = null,
25-
var type: DataType = DataType.`object`
27+
var type: DataType = DataType.`object`,
28+
override var description: String? = null
2629
) : SchemaModel<T>()
2730

2831
data class SchemaModelEnum<T>(
2932
var enum: List<String>,
3033
var nullable: Boolean = false,
3134
override var example: T? = null,
3235
override var examples: List<T>? = null,
33-
var type: DataType = DataType.string
36+
var type: DataType = DataType.string,
37+
override var description: String? = null
3438
) : SchemaModel<T>()
3539

3640
data class SchemaModelArr<T>(
@@ -41,7 +45,8 @@ sealed class SchemaModel<T>: DataModel {
4145
var uniqueItems: Boolean? = null,
4246
var minItems: Int? = null,
4347
var maxItems: Int? = null,
44-
var type: DataType = DataType.array
48+
var type: DataType = DataType.array,
49+
override var description: String? = null
4550
) : SchemaModel<T>()
4651

4752
data class SchemaModelLitteral<T>(
@@ -51,16 +56,19 @@ sealed class SchemaModel<T>: DataModel {
5156
var minimum: T? = null,
5257
var maximum: T? = null,
5358
override var example: T? = null,
54-
override var examples: List<T>? = null
59+
override var examples: List<T>? = null,
60+
override var description: String? = null
5561
) : SchemaModel<T>()
5662

5763
data class SchemaModelRef<T>(override val `$ref`: String) : SchemaModel<T>(), RefModel<SchemaModel<T>> {
5864
override var example: T? = null
5965
override var examples: List<T>? = null
66+
override var description: String? = null
6067
}
6168

6269
data class OneSchemaModelOf<T>(val oneOf: List<SchemaModel<out T>>) : SchemaModel<T>() {
6370
override var example: T? = null
6471
override var examples: List<T>? = null
72+
override var description: String? = null
6573
}
6674
}

src/test/kotlin/TestServer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.papsign.ktor.openapigen.annotations.Response
1515
import com.papsign.ktor.openapigen.annotations.mapping.OpenAPIName
1616
import com.papsign.ktor.openapigen.annotations.parameters.HeaderParam
1717
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
18+
import com.papsign.ktor.openapigen.annotations.properties.description.Description
1819
import com.papsign.ktor.openapigen.annotations.type.`object`.example.ExampleProvider
1920
import com.papsign.ktor.openapigen.annotations.type.`object`.example.WithExample
2021
import com.papsign.ktor.openapigen.interop.withAPI
@@ -222,7 +223,7 @@ object TestServer {
222223
data class StringParam(@PathParam("A simple String Param") val a: String)
223224

224225
@Response("A String Response")
225-
data class StringResponse(val str: String)
226+
data class StringResponse(@Description("The string value") val str: String)
226227

227228
data class NameParam(@HeaderParam("A simple Header Param") @OpenAPIName("X-NAME") val name: String)
228229

0 commit comments

Comments
 (0)