Skip to content

Commit

Permalink
generation synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaztahir committed Apr 17, 2023
1 parent 88ec130 commit 332dbc4
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=1.1.0
version=1.1.1

jackson.version=2.9.8
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/
package com.reprezen.jsonoverlay

import com.reprezen.jsonoverlay.JsonOverlay.Companion._jsonObject
import kotlinx.serialization.json.JsonObject

class Builder<V>(private val factory: OverlayFactory<V>, private val modelMember: JsonOverlay<*>) {
fun build(): JsonOverlay<V> {
val refMgr = modelMember.refMgr
return factory.create(_jsonObject(mapOf()), null, refMgr)
return factory.create(JsonObject(mapOf()), null, refMgr)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ abstract class JsonOverlay<V> : IJsonOverlay<V> {
}

fun findByPath(path: String): JsonOverlay<*>? {
return findByPointer(JsonPointer(path))
return _findByPointer(JsonPointer(path))
}

open fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
open fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
return null
}

Expand Down Expand Up @@ -241,7 +241,7 @@ abstract class JsonOverlay<V> : IJsonOverlay<V> {
fun _toJson(options: SerializationOptions = SerializationOptions()): JsonElement {
return if (_isReference()) {
if (!options.isFollowRefs || refOverlay!!._getReference().isInvalid) {
_jsonObject(mapOf("\$ref" to JsonPrimitive(refOverlay!!._getReference().refString)))
JsonObject(mapOf("\$ref" to JsonPrimitive(refOverlay!!._getReference().refString)))
} else {
refOverlay!!.overlay!!._toJson(options)
}
Expand Down Expand Up @@ -284,17 +284,4 @@ abstract class JsonOverlay<V> : IJsonOverlay<V> {
return (json != null || other.json != null) && json == other.json
}

companion object {

@JvmStatic
fun _jsonObject(content: Map<String, JsonElement>): JsonObject {
return JsonObject(content)
}

@JvmStatic
protected fun _jsonArray(content: List<JsonElement>): JsonArray {
return JsonArray(content)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ interface KeyValueOverlay {
return _getValueOverlayByPath(index.toString())
}

fun findByPointer(path: JsonPointer): JsonOverlay<*>?
fun _findByPointer(path: JsonPointer): JsonOverlay<*>?

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ListOverlay<V> private constructor(

override fun _toJsonInternal(options: SerializationOptions): JsonElement {
if (overlays.isEmpty() && !options.isKeepThisEmpty) return JsonNull
return _jsonArray(overlays.map { it._toJson(options.plus(SerializationOptions.Option.KEEP_ONE_EMPTY)) })
return JsonArray(overlays.map { it._toJson(options.plus(SerializationOptions.Option.KEEP_ONE_EMPTY)) })
}

fun _elaborate() {
Expand Down Expand Up @@ -95,12 +95,12 @@ class ListOverlay<V> private constructor(
return name.toIntOrNull()?.let { overlays[it] }
}

override fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
val index = path.segments.firstOrNull()?.toIntOrNull()
if (index != null) {
_getValueOverlayByIndex(index)?.let {
if (path.segments.size == 1) return it
return it.findByPointer(JsonPointer(path.segments.drop(1)))
return it._findByPointer(JsonPointer(path.segments.drop(1)))
}
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MapOverlay<V> private constructor(
return overlays[name]
}

override fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
// val debug = path == JsonPointer("/2.0/users/{username}")
// if (debug) println("FINDING $path IN MapOverlay")
overlays[path.toString()]?.let { return it }
Expand All @@ -55,7 +55,7 @@ class MapOverlay<V> private constructor(
(overlays[key])?.let { ov ->
// if(debug) println("FOUND $key , FINDING ${JsonPointer(path.segments.drop(1))} IN ${ov::class.qualifiedName}")
if (path.segments.size == 1) return ov
return ov.findByPointer(JsonPointer(path.segments.drop(1)))
return ov._findByPointer(JsonPointer(path.segments.drop(1)))
}
// if (debug) println("FINDING F-SLASH")
// if (key.getOrNull(0) == '/') {
Expand All @@ -81,7 +81,7 @@ class MapOverlay<V> private constructor(

override fun _toJsonInternal(options: SerializationOptions): JsonElement {
if (overlays.isEmpty() && !options.isKeepThisEmpty) return JsonNull
return _jsonObject(overlays.mapValues { it.value._toJson(options.minus(SerializationOptions.Option.KEEP_ONE_EMPTY)) })
return JsonObject(overlays.mapValues { it.value._toJson(options.minus(SerializationOptions.Option.KEEP_ONE_EMPTY)) })
}

fun _elaborate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class ObjectOverlay : ScalarOverlay<Any>, KeyValueOverlay {
return null
}

override fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
path.segments.firstOrNull()?.let { valueOfKey(it) }?.let { ov ->
if (path.segments.size == 1) return ov
return ov.findByPointer(JsonPointer(path.segments.drop(1)))
return ov._findByPointer(JsonPointer(path.segments.drop(1)))
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract class PropertiesOverlay<V> : JsonOverlay<V>, KeyValueOverlay {
return elem?.let { createOverlay(it, factoryMap, factory) }
}

override fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
// val debug = path == JsonPointer("/2.0/users/{username}")
// if (debug) println("NAVIGATING $path")
for (entry in factoryMap) {
Expand All @@ -160,7 +160,7 @@ abstract class PropertiesOverlay<V> : JsonOverlay<V>, KeyValueOverlay {
return it
} else if (it is KeyValueOverlay) {
// if (debug) println("FINDING $remaining IN ${it::class.qualifiedName}")
it.findByPointer(remaining)?.let { next -> return next }
it._findByPointer(remaining)?.let { next -> return next }
// if (debug) println("FINDING /")
// if (remaining.segments.firstOrNull()?.startsWith("/") == true) {
// println("FINDING WITHOUT F-SLASH $remaining")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OpenApi3Impl : PropertiesOverlay<OpenApi3> ,OpenApi3 {
json = JsonObject(mapOf())
}
if (json is JsonObject && !json.containsKey("paths")) {
json = JsonObject(json.toMutableMap().apply { put("paths", _jsonObject(mapOf())) })
json = JsonObject(json.toMutableMap().apply { put("paths", JsonObject(mapOf())) })
}
return json
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ package com.reprezen.kaizen.oasparser.ovl3
import com.reprezen.kaizen.oasparser.model3.*
import com.reprezen.jsonoverlay.MapOverlay
import com.reprezen.jsonoverlay.StringOverlay
import com.reprezen.jsonoverlay.ListOverlay
import com.reprezen.jsonoverlay.OverlayFactory
import com.reprezen.jsonoverlay.Builder
import com.reprezen.jsonoverlay.JsonPointer
import com.reprezen.kaizen.oasparser.ovl3.XmlImpl
import com.reprezen.jsonoverlay.ReferenceManager
import com.reprezen.jsonoverlay.IntegerOverlay
import com.reprezen.kaizen.oasparser.ovl3.SchemaImpl
import kotlin.collections.List
import com.reprezen.jsonoverlay.JsonOverlay
import com.reprezen.jsonoverlay.Overlay
import com.reprezen.kaizen.oasparser.ovl3.DiscriminatorImpl
import com.reprezen.jsonoverlay.parser.Generated
import java.util.Optional
import com.reprezen.jsonoverlay.IJsonOverlay
import com.reprezen.jsonoverlay.PropertiesOverlay
import com.reprezen.jsonoverlay.NumberOverlay
import kotlin.reflect.KClass
import kotlinx.serialization.json.JsonElement
import com.reprezen.jsonoverlay.ObjectOverlay
import kotlin.collections.Map
import com.reprezen.jsonoverlay.BooleanOverlay
import com.reprezen.kaizen.oasparser.ovl3.ExternalDocsImpl

class SchemaImpl : PropertiesOverlay<Schema> ,Schema {

Expand All @@ -26,16 +35,16 @@ class SchemaImpl : PropertiesOverlay<Schema> ,Schema {
return if(overlay.parent is MapOverlay<*>) overlay.pathInParent else null
}

override fun findByPointer(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
return if(path.segments.firstOrNull() == "additionalProperties"){
val tail = JsonPointer(path.segments.drop(1))
if(json != null && tail.navigate(json!!) != null){
_getOverlay<Boolean>("additionalProperties") as BooleanOverlay
} else {
_get<SchemaImpl>("additionalPropertiesSchema")?.findByPointer(tail)
_get<SchemaImpl>("additionalPropertiesSchema")?._findByPointer(tail)
}
} else {
super.findByPointer(path)
super._findByPointer(path)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/kotlin/class/openapi3.kate
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
json = JsonObject(mapOf())
}
if (json is JsonObject && !json.containsKey("paths")) {
json = JsonObject(json.toMutableMap().apply { put("paths", _jsonObject(mapOf())) })
json = JsonObject(json.toMutableMap().apply { put("paths", JsonObject(mapOf())) })
}
return json
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/kotlin/class/schema.kate
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
return if(overlay.parent is MapOverlay<*>) overlay.pathInParent else null
}

override fun _findByPath(path: JsonPointer): JsonOverlay<*>? {
override fun _findByPointer(path: JsonPointer): JsonOverlay<*>? {
return if(path.segments.firstOrNull() == "additionalProperties"){
val tail = JsonPointer(path.segments.drop(1))
if(json != null && tail.navigate(json!!) != null){
_getOverlay<Boolean>("additionalProperties") as BooleanOverlay
} else {
_get<SchemaImpl>("additionalPropertiesSchema")?.findByPointer(tail)
_get<SchemaImpl>("additionalPropertiesSchema")?._findByPointer(tail)
}
} else {
super._findByPath(path)
super._findByPointer(path)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BigParseTest(private val modelUrl: URL) : Assert() {
val model = OpenApiParser().parse(modelUrl) as OpenApi3Impl
val walker = object : Walker {
override fun walk(node: JsonElement, parent: JsonElement?, pointer: com.reprezen.jsonoverlay.JsonPointer) {
val overlay = model.findByPointer(pointer)
val overlay = model._findByPointer(pointer)
assertNotNull("No overlay object found for path: $pointer", overlay)
val value = Overlay[overlay!!]
val fromJson = node.toValue()
Expand Down

0 comments on commit 332dbc4

Please sign in to comment.