/openapi-matcher

OpenApi matcher for kotlin, allows to compare 2 specifications against each other.

Primary LanguageKotlinMIT LicenseMIT

OpenAPI Matcher

Build CodeFactor ktlint

Maven Central Kotlin GitHub

Warning
From version 0.7.0 all package names have been renamed to match new artifact group id.

OpenAPI matcher for Kotlin allows comparing 2 specifications against each other.

Warning
This library in Work In Progress. It doesn’t support all elements of OpenAPI specification yet. The support scope can be checked individually for every OpenAPI specification object in the korrit.kotlin.openapi.model package.

Reading OpenAPI

This library includes an OpenAPI reader that allows parsing your OpenAPI specification in YAML format into an in-memory object that can be used in a matcher.

val doc: OpenAPI = OpenAPIReader().load({}.javaClass.getResourceAsStream("/openapi.yaml"))
Note
Unsupported elements are ignored.

Validating OpenAPI

Once you have 2 OpenAPI objects you can match them checking for any significant differences.

A significant difference is understood as a divergence that leads to or may lead to a compatibility break.

The general idea is to validate your OpenAPI specification against your code instead of spec/code generation.

val source: OpenAPI = ... // Analyze your code

LOG.info("Reading OpenAPI spec...")
val doc: OpenAPI = OpenAPIReader().load({}.javaClass.getResourceAsStream("/openapi.yaml"))

LOG.info("Validating spec...")
val errors = OpenAPIMatcher().match(doc, source)

if (errors.isNotEmpty()) {
    LOG.info("Result of code analysis:\n{}", source)

    errors.forEach {
        LOG.error(it)
    }

    fail("There are ${errors.size} validation errors!")
} else {
    LOG.info("OK!")
}