com.ancientlightstudios.quarkus.kotlin.openapi.parser.OpenApiReader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kotlin-openapi-maven-plugin Show documentation
Show all versions of quarkus-kotlin-openapi-maven-plugin Show documentation
A Maven plugin to use the OpenAPI generator.
package com.ancientlightstudios.quarkus.kotlin.openapi.parser
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.flipkart.zjsonpatch.JsonPatch
import java.io.InputStream
fun read(inputStream: InputStream):JsonNode = ObjectMapper(YAMLFactory()).readTree(inputStream)
fun ObjectNode.merge(updateNode: ObjectNode): ObjectNode {
val fieldNames = updateNode.fieldNames()
while (fieldNames.hasNext()) {
val updatedFieldName = fieldNames.next()
val valueToBeUpdated = this[updatedFieldName]
val updatedValue = updateNode[updatedFieldName]
when {
// If the node is an ArrayNode
valueToBeUpdated != null && updatedValue.isArray -> {
// running a loop for all elements of the updated ArrayNode
for (i in 0 until updatedValue.size()) {
val updatedChildNode = updatedValue[i]
// Create a new Node in the node that should be updated, if there was no corresponding node in it
// Use-case - where the updateNode will have a new element in its Array
(valueToBeUpdated as ArrayNode).add(updatedChildNode)
}
}
// if the Node is an ObjectNode
valueToBeUpdated != null && valueToBeUpdated.isObject -> {
(valueToBeUpdated as ObjectNode).merge(updatedValue as ObjectNode)
}
else -> {
replace(updatedFieldName, updatedValue)
}
}
}
return this
}
fun ObjectNode.patch(patchNode:JsonNode) : ObjectNode = JsonPatch.apply(patchNode, this) as ObjectNode