-
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
15 changed files
with
587 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ hide: | |
- title | ||
- navigation | ||
- toc | ||
- footer | ||
--- | ||
|
||
# Ktor OpenAPI Tools | ||
|
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 |
---|---|---|
@@ -1,53 +1,69 @@ | ||
# Example Encoding | ||
|
||
## Customizing Example Encoding | ||
The encoding of example values can be customized with the encoder config. | ||
Example objects are automatically serialized (as json) and added to the OpenAPI specification. | ||
The encoding of example values can be customized in the plugin configuration. | ||
If no encoder is specified, the example value will be encoded by swagger. | ||
|
||
```kotlin | ||
install(OpenApi) { | ||
examples { | ||
encoder { type, example -> | ||
// always encoding example as a string | ||
example.toString() | ||
## Pre-Defined Example Encoders | ||
|
||
=== "Swagger" | ||
Explicitly use the internal swagger example encoder. | ||
|
||
```kotlin | ||
install(OpenApi) { | ||
examples { | ||
encoder = ExampleEncoder.internal() | ||
} | ||
} | ||
} | ||
``` | ||
``` | ||
|
||
??? info "More Information" | ||
|
||
[:octicons-arrow-right-24: API Reference](../dokka/ktor-openapi/ktor-openapi/io.github.smiley4.ktoropenapi.config/-example-encoder/internal.html) | ||
|
||
## Shared Examples | ||
=== "Kotlinx.Serialization" | ||
Use [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) to encode example values. | ||
|
||
```kotlin | ||
install(OpenApi) { | ||
examples { | ||
encoder = ExampleEncoder.kotlinx() | ||
} | ||
} | ||
``` | ||
|
||
Global or shared examples can be defined in the examples section of the plugin config and then referenced by route documentation. | ||
Shared examples are placed in the components/examples section of the final OpenAPI spec. | ||
??? info "More Information" | ||
|
||
### Defining Shared Examples | ||
[:octicons-arrow-right-24: API Reference](../dokka/ktor-openapi/ktor-openapi/io.github.smiley4.ktoropenapi.config/-example-encoder/kotlinx.html) | ||
|
||
|
||
## Custom Example Encoder | ||
|
||
The example encoder can be completely replaced by an own implementation. | ||
|
||
```kotlin | ||
install(OpenApi) { | ||
examples { | ||
example("Shared A") { | ||
description = "first shared example" | ||
value = MyExampleClass( | ||
someValue = "shared a" | ||
) | ||
} | ||
|
||
example("Shared B") { | ||
description = "second shared example" | ||
value = MyExampleClass( | ||
someValue = "shared b" | ||
) | ||
encoder { type, example -> //(1)! | ||
TOOD() //(2)! | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Referencing Shared Examples in Routes | ||
1. Input of the example encoding function is a `io.github.smiley4.ktoropenapi.config.TypeDescriptor` with information about the type/schema and the actual value of the example. | ||
2. Encode/Transform the example value and return the result. | ||
|
||
```kotlin | ||
body<MyExampleClass> { | ||
// reference two shared examples specified in the plugin configuration | ||
exampleRef("Example 1", "Shared A") | ||
exampleRef("Example 2", "Shared B") | ||
} | ||
``` | ||
???+ example "Custom "toString()" Example Encoder" | ||
|
||
```kotlin | ||
install(OpenApi) { | ||
examples { | ||
encoder { type, example -> | ||
example.toString() //(1)! | ||
} | ||
} | ||
} | ||
``` | ||
|
||
1. Always encode and embed the example value as a raw 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
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.