A Kotlin Multiplatform library that provides bindings for OpenAPI JSON files. The library can parse and serialize OpenAPI specifications to Kotlin data classes, handling union types and x-properties (extensions).
- Support for both OpenAPI v3 and v2 (Swagger) specifications
- Kotlin Multiplatform support (JVM, JS, Native)
- Serialization and deserialization of OpenAPI JSON
- Proper handling of union types
- Support for x-properties (extensions)
- Type-safe Kotlin data classes for OpenAPI models
- JVM
- JavaScript (NodeJS)
- Native
- macOS (x64, arm64)
- Linux (x64)
- Windows (x64)
Add the dependency to your build.gradle.kts or build.gradle file:
repositories {
mavenCentral()
}
dependencies {
implementation("community.flock.kotlinx.openapi.bindings:kotlin-openapi-bindings:0.1.1")
}// Using community.flock.kotlinx.openapi.bindings.OpenAPIV3
// and community.flock.kotlinx.openapi.bindings.OpenAPIV3Model
val json = """
{
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
"""
// Parse OpenAPI JSON to Kotlin object
val obj: OpenAPIV3Model = OpenAPIV3.decodeFromString(json)
// Serialize a Kotlin object back to JSON
val str: String = OpenAPIV3.encodeToString(obj)// Using community.flock.kotlinx.openapi.bindings.OpenAPIV2
// and community.flock.kotlinx.openapi.bindings.OpenAPIV2Model
val json = """
{
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
"""
// Parse Swagger JSON to Kotlin object
val obj: OpenAPIV2Model = OpenAPIV2.decodeFromString(json)
// Serialize a Kotlin object back to JSON
val str: String = OpenAPIV2.encodeToString(obj)The library automatically handles x-properties (extensions) in OpenAPI specifications. These properties are stored in an xProperties field during parsing and are restored when serializing back to JSON.
For detailed API documentation, please refer to the Dokka documentation.
Contributions to kotlin-openapi-bindings are welcome! To contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
This project is licensed under the MIT License - see the LICENSE file for details.