-
Notifications
You must be signed in to change notification settings - Fork 32
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
30 changed files
with
562 additions
and
476 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
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
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/CustomSchemas.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,60 @@ | ||
package io.github.smiley4.ktorswaggerui.dsl | ||
|
||
import io.swagger.v3.oas.models.media.Schema | ||
import java.lang.reflect.Type | ||
|
||
@OpenApiDslMarker | ||
class CustomSchemas { | ||
|
||
private var jsonSchemaBuilder: ((type: Type) -> String?)? = null | ||
|
||
|
||
/** | ||
* Custom builder for building json-schemas from a given type. Return null to not use this builder for the given type. | ||
*/ | ||
fun jsonSchemaBuilder(builder: (type: Type) -> String?) { | ||
jsonSchemaBuilder = builder | ||
} | ||
|
||
fun getJsonSchemaBuilder() = jsonSchemaBuilder | ||
|
||
|
||
private val customSchemas = mutableMapOf<String, BaseCustomSchema>() | ||
|
||
|
||
/** | ||
* Define the json-schema for an object/body with the given id | ||
*/ | ||
fun json(id: String, provider: () -> String) { | ||
customSchemas[id] = CustomJsonSchema(provider) | ||
} | ||
|
||
|
||
/** | ||
* Define the [Schema] for an object/body with the given id | ||
*/ | ||
fun openApi(id: String, provider: () -> Schema<Any>) { | ||
customSchemas[id] = CustomOpenApiSchema(provider) | ||
} | ||
|
||
|
||
/** | ||
* Define the external url for an object/body with the given id | ||
*/ | ||
fun remote(id: String, url: String) { | ||
customSchemas[id] = RemoteSchema(url) | ||
} | ||
|
||
fun getSchema(id: String): BaseCustomSchema? = customSchemas[id] | ||
|
||
|
||
} | ||
|
||
|
||
sealed class BaseCustomSchema | ||
|
||
class CustomJsonSchema(val provider: () -> String) : BaseCustomSchema() | ||
|
||
class CustomOpenApiSchema(val provider: () -> Schema<Any>) : BaseCustomSchema() | ||
|
||
class RemoteSchema(val url: String) : BaseCustomSchema() |
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
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
Oops, something went wrong.