-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
3 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
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/Example.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 io.github.smiley4.ktorswaggerui.dsl | ||
|
||
/** | ||
* Annotation to add an example value to the field of an object. | ||
*/ | ||
@Target(AnnotationTarget.FIELD) | ||
annotation class Example( | ||
val value: String | ||
) |
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
119 changes: 119 additions & 0 deletions
119
src/test/kotlin/io/github/smiley4/ktorswaggerui/examples/ExampleAnnotationExample.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,119 @@ | ||
package io.github.smiley4.ktorswaggerui.examples | ||
|
||
import io.github.smiley4.ktorswaggerui.SwaggerUI | ||
import io.github.smiley4.ktorswaggerui.dsl.Example | ||
import io.github.smiley4.ktorswaggerui.dsl.get | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.call | ||
import io.ktor.server.application.install | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.netty.Netty | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.routing | ||
import io.swagger.v3.oas.annotations.media.ArraySchema | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
|
||
/** | ||
* An example showcasing examples with the [Schema] and [io.github.smiley4.ktorswaggerui.dsl.Example]-Annotation | ||
*/ | ||
fun main() { | ||
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true) | ||
} | ||
|
||
private fun Application.myModule() { | ||
install(SwaggerUI) | ||
routing { | ||
get("person/example", { | ||
request { | ||
body<ExamplePerson>() | ||
} | ||
response { | ||
HttpStatusCode.OK to { | ||
body<ExamplePerson>() | ||
} | ||
} | ||
}) { | ||
call.respondText("...") | ||
} | ||
get("person/schema", { | ||
request { | ||
body<SchemaPerson>() | ||
} | ||
response { | ||
HttpStatusCode.OK to { | ||
body<SchemaPerson>() | ||
} | ||
} | ||
}) { | ||
call.respondText("...") | ||
} | ||
} | ||
} | ||
|
||
data class ExamplePerson( | ||
|
||
@Example("red") | ||
val favColor: String, | ||
|
||
@Example("Steve") | ||
val name: String, | ||
|
||
@Example("42") | ||
val age: Int, | ||
|
||
@Example("172") | ||
val size: Float, | ||
|
||
@Example("false") | ||
val robot: Boolean, | ||
|
||
val address: ExampleAddress, | ||
|
||
val secondaryAddresses: List<ExampleAddress> | ||
) | ||
|
||
data class ExampleAddress( | ||
|
||
@Example("New City") | ||
val city: String, | ||
|
||
@Example("12345") | ||
val code: Int | ||
|
||
) | ||
|
||
@Schema(description = "Schema of some person", title = "Person") | ||
data class SchemaPerson( | ||
|
||
@field:Schema(example = "red") | ||
val favColor: String, | ||
|
||
@field:Schema(example = "Steve", minLength = 1, maxLength = 32) | ||
val name: String, | ||
|
||
@field:Schema(example = "42", minimum = "18", maximum = "99") | ||
val age: Int, | ||
|
||
@field:Schema(example = "172", format = "int32") | ||
val size: Float, | ||
|
||
@field:Schema(example = "false") | ||
val robot: Boolean, | ||
|
||
val address: SchemaAddress, | ||
|
||
@field:ArraySchema(minItems = 1, maxItems = 32, uniqueItems = true) | ||
val secondaryAddresses: List<SchemaAddress> | ||
) | ||
|
||
@Schema(description = "Schema of some address", title = "Address") | ||
data class SchemaAddress( | ||
|
||
@field:Schema(example = "New City") | ||
val city: String, | ||
|
||
@field:Schema(example = "12345") | ||
val code: Int | ||
|
||
) |
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,18 @@ | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern> | ||
%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) | %highlight(%-5.5level{5}) | %gray(%-16.16thread{16}) | %magenta(%-25.25logger{25}) | %m%n | ||
</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="io.netty" level="INFO"/> | ||
<logger name="com.arangodb" level="INFO"/> | ||
|
||
</configuration> |