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

de.lancom.openapi.tools.OpenApiUtils.kt Maven / Gradle / Ivy

Go to download

This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes

There is a newer version: 2.1.1
Show newest version
package de.lancom.openapi.tools

import de.lancom.openapi.entity.*
import de.lancom.openapi.refs.Instance
import de.lancom.openapi.refs.ReferenceOrInstance

fun Response.addJsonContent(schema: Schema): Response {
    return addJsonContent(Instance(schema))
}

fun Response.addJsonContent(schema: ReferenceOrInstance): Response {
    return addJsonContent(MediaType().addSchema(schema))
}

fun Response.addJsonContent(mediaType: MediaType): Response {
    return addContent(ContentType.applicationJson.contentType to mediaType)
}

fun RequestBody.addJsonContent(schema: Schema): RequestBody {
    return addJsonContent(Instance(schema))
}

fun RequestBody.addJsonContent(schema: ReferenceOrInstance): RequestBody {
    return addJsonContent(MediaType().addSchema(schema))
}

fun RequestBody.addJsonContent(mediaType: MediaType): RequestBody {
    return addContent(ContentType.applicationJson.contentType to mediaType)
}

@JvmName("addResponsesInstance")
fun Operation.addResponses(responses: Map): Operation {
    val mapped = responses.mapValues { (_, value) ->
        Instance(value)
    }
    return addResponses(mapped)
}

@JvmName("addResponsesInstance")
fun Operation.addResponses(vararg response: Pair): Operation {
    return addResponses(response.toMap())
}

@JvmName("addResponsesInstance")
fun Operation.addResponse(response: Pair): Operation {
    return addResponses(response)
}

fun Operation.addResponses(responses: Map?>): Operation {
    return mergeResponses(Responses().setResponses(responses))
}

fun Operation.addResponses(vararg response: Pair?>): Operation {
    return addResponses(response.toMap())
}

fun Operation.addResponse(response: Pair?>): Operation {
    return addResponses(response)
}

fun Components.addSchemas(schemas: Map): Components {
    return addSchemas(schemas.mapValues { (_, schema) ->
        Instance(schema)
    })
}

fun PathItem?.updateOperations(updater: (Operation?) -> Operation?): PathItem? {
    return this
        ?.updateGet(updater)
        ?.updatePut(updater)
        ?.updatePost(updater)
        ?.updateDelete(updater)
        ?.updateOptions(updater)
        ?.updateHead(updater)
        ?.updatePatch(updater)
        ?.updateTrace(updater)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy