V2 Json Schema Generation - SMILEY4/ktor-openapi-tools GitHub Wiki
Json-Schema-Generation is the conversion from a type (kotlin.reflect.KType
) to a json-schema-string. This behavior can be customized, overwritten or completely replaced in the plugin-config encoding
-section.
This project uses the victools/jsonschema-generator as the default generator. Its configuration can be customized freely via the "EncodingData".
EncodingData.DEFAULT_SCHEMA_GENERATOR = SchemaGenerator(
schemaGeneratorConfigBuilder()
.with(Option.GETTER_METHODS) // include getter-methods in schema
.without(Option.EXTRA_OPEN_API_FORMAT_VALUES) // don't add openapi specific formats
.build()
)
The default schema-generator can be completely replaced by another one in the plugin-config encoding
-section.
install(SwaggerUI) {
encoding {
// replace the default schema-generator
schemaEncoder { type ->
// convert 'type' to a json-schema and return it as a string
}
// if schemas contain subschemas, they are contained in the field with the following name
schemaDefinitionsField = "$defs"
}
}
-
schemaEncoder
- converts the given type (kotlin.reflect.KType
) to a valid json-schema-string -
schemaDefinitionsField
- the name of the field containing possible subschemas
It is also possible to only replace the generator for specific types. In this case, the "schemaDefinitionsField" must be valid for the default and custom generator.
schemaEncoder { type ->
when (type) {
getSchemaType<String>() -> """{"type": "string"}""" // custom "generator" for strings
else -> EncodingData.encodeSchema(type) // use default generator for everything else
}
}
An example replacing victools/jsonschema-generator with a kotlinx-compatible generator can be found here.