Skip to content

lexa-diky/ktor-openapi-validator

Repository files navigation

Ktor OpenApi (Swagger) Validator

Github Actions Build Stars License Maven Central Version

A Ktor plugin for validating HTTP requests and responses against an OpenAPI specification.

Installation

dependencies {
    testImplementation("io.github.lexa-diky:ktor-openapi-validator:0.5.0")
}

Usage

This plugin is intended for test only, please avoid using it in production code.

Install OpenApiValidator client plugin and specify specificationUrl and it will do the rest automatically.

val client = HttpClient {
    // ... rest of your configuration

    install(OpenApiValidator) {
        specificationUrl = "openapi.yaml"
    }
}

Whitelisting

You can whitelist specific requests or responses by providing a list of paths and methods that should be ignored during validation.

install(OpenApiValidator) {
    specificationUrl = "openapi.yaml"

    // Whitelist any response with error status code
    whitelist("Allow any error") {
        response.code?.isSuccess() == false
    }
}

Custom reporters

You can provide a custom reporter to handle validation errors. The default reporter will use Junit5 assertions to report errors.

install(OpenApiValidator) {
    specificationUrl = "openapi.yaml"
    reporter = TextReporter { messages ->
        messages.forEach { message ->
            println("OpenAPI validation error: $message")
        }
    }
}

Integrations and dependencies

Junit5

Library integrates with Junit5 via compile time dependency. Please provide appropriate implementation on classpath if you are using default reporter.

dependencies {
    testImplementation("io.github.lexa-diky:ktor-openapi-validator:<latest-version>")
    testImplementation("org.junit.jupiter:junit-jupiter-api:<your-junit-version>")
}

Ktor

Library integrates with Ktor Client via compile time dependency. It is compiled explicitly against Ktor 2.x, but works fine with Ktor 3.x as well. Please provide appropriate implementation on classpath.

dependencies {
    testImplementation("io.github.lexa-diky:ktor-openapi-validator:<latest-version>")
    testImplementation("io.ktor:ktor-client-core:<your-ktor-version>")
}

About

Openapi spec validation for ktor client calls

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages