All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.epages.restdocs.apispec.openapi3.ApiSpecificationWriter.kt Maven / Gradle / Ivy

There is a newer version: 0.19.4
Show newest version
package com.epages.restdocs.apispec.openapi3

import io.swagger.v3.core.util.Json
import io.swagger.v3.core.util.Yaml
import io.swagger.v3.oas.models.OpenAPI

internal object ApiSpecificationWriter {

    private val yamlFormats = setOf("yaml", "yml")
    private val jsonFormats = setOf("json")

    fun serialize(format: String, openApi: OpenAPI): String {
        validateFormat(format)
        return if (yamlFormats.contains(format)) {
            Yaml.pretty().writeValueAsString(openApi)
        } else {
            Json.pretty().writeValueAsString(openApi)
        }
    }

    fun supportedFormats() = yamlFormats + jsonFormats

    fun validateFormat(format: String) {
        if (!supportedFormats().contains(format)) throw IllegalArgumentException("Format '$format' is invalid - supported formats are '${supportedFormats()}'")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy