de.lancom.openapi.tools.OpenApiUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-parser Show documentation
Show all versions of openapi-parser Show documentation
This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes
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)
}