Skip to content

Example Encoding

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.

Pre-Defined Example Encoders

Explicitly use the internal swagger example encoder.

install(OpenApi) {
    examples {
        encoder = ExampleEncoder.internal()
    }
}
More Information

API Reference

Use kotlinx.serialization to encode example values.

install(OpenApi) {
    examples {
        encoder = ExampleEncoder.kotlinx()
    }
}
More Information

API Reference

Custom Example Encoder

The example encoder can be completely replaced by an own implementation.

install(OpenApi) {
    examples {
        encoder { type, example -> //(1)!
            TOOD() //(2)!
        }
    }
}
  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.
Custom "toString()" Example Encoder
install(OpenApi) {
    examples {
        encoder { type, example ->
            example.toString() //(1)!
        }
    }
}
  1. Always encode and embed the example value as a raw string.