-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,673 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../kotlin/io/github/smiley4/schemakenerator/jsonschema/steps/JsonSchemaCompileInlineStep.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.github.smiley4.schemakenerator.jsonschema.steps | ||
|
||
import io.github.smiley4.schemakenerator.core.data.Bundle | ||
import io.github.smiley4.schemakenerator.core.data.TypeId | ||
import io.github.smiley4.schemakenerator.core.data.flatten | ||
import io.github.smiley4.schemakenerator.jsonschema.data.CompiledJsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.data.JsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonObject | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonTextValue | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.resolveReferences | ||
|
||
/** | ||
* Resolves references in prepared json-schemas by inlining them. | ||
*/ | ||
class JsonSchemaCompileInlineStep { | ||
|
||
/** | ||
* Inline all referenced schema | ||
*/ | ||
fun compile(bundle: Bundle<JsonSchema>): CompiledJsonSchema { | ||
val schemaList = bundle.flatten() | ||
val root = resolveReferences(bundle.data.json) { refObj -> | ||
val referencedId = TypeId.parse((refObj.properties["\$ref"] as JsonTextValue).value) | ||
val referencedSchema = schemaList.find(referencedId) | ||
if (referencedSchema != null) { | ||
referencedSchema.json.also { | ||
if (it is JsonObject) { | ||
it.properties.putAll(buildMap { | ||
this.putAll(refObj.properties) | ||
this.remove("\$ref") | ||
}) | ||
} | ||
} | ||
} else { | ||
refObj | ||
} | ||
} | ||
return CompiledJsonSchema( | ||
json = root, | ||
typeData = bundle.data.typeData, | ||
definitions = emptyMap() | ||
) | ||
} | ||
|
||
private fun Collection<JsonSchema>.find(id: TypeId): JsonSchema? { | ||
return this.find { it.typeData.id == id } | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
.../io/github/smiley4/schemakenerator/jsonschema/steps/JsonSchemaCompileReferenceRootStep.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.github.smiley4.schemakenerator.jsonschema.steps | ||
|
||
import io.github.smiley4.schemakenerator.core.data.Bundle | ||
import io.github.smiley4.schemakenerator.jsonschema.data.CompiledJsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.data.JsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.data.RefType | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.getRefPath | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.shouldReference | ||
|
||
/** | ||
* Resolves references in prepared json-schemas by collecting them in the definitions-section and referencing them. | ||
* @param pathType how to reference the type, i.e. which name to use | ||
*/ | ||
class JsonSchemaCompileReferenceRootStep(private val pathType: RefType = RefType.FULL) { | ||
|
||
private val schemaUtils = JsonSchemaUtils() | ||
|
||
|
||
/** | ||
* Put referenced schemas into definitions and reference them | ||
*/ | ||
fun compile(bundle: Bundle<JsonSchema>): CompiledJsonSchema { | ||
val result = JsonSchemaCompileReferenceStep(pathType).compile(bundle) | ||
if (shouldReference(result.json)) { | ||
val refPath = getRefPath(pathType, result.typeData, bundle.buildTypeDataMap()) | ||
return CompiledJsonSchema( | ||
typeData = result.typeData, | ||
json = schemaUtils.referenceSchema(refPath, true), | ||
definitions = buildMap { | ||
this.putAll(result.definitions) | ||
this[refPath] = result.json | ||
} | ||
) | ||
} else { | ||
return CompiledJsonSchema( | ||
typeData = result.typeData, | ||
json = result.json, | ||
definitions = result.definitions | ||
) | ||
} | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...tlin/io/github/smiley4/schemakenerator/jsonschema/steps/JsonSchemaCompileReferenceStep.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.github.smiley4.schemakenerator.jsonschema.steps | ||
|
||
import io.github.smiley4.schemakenerator.core.data.BaseTypeData | ||
import io.github.smiley4.schemakenerator.core.data.Bundle | ||
import io.github.smiley4.schemakenerator.core.data.TypeId | ||
import io.github.smiley4.schemakenerator.core.data.flatten | ||
import io.github.smiley4.schemakenerator.jsonschema.data.CompiledJsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.data.JsonSchema | ||
import io.github.smiley4.schemakenerator.jsonschema.data.RefType | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonNode | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonObject | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonTextValue | ||
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.obj | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.getRefPath | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.resolveReferences | ||
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileUtils.shouldReference | ||
|
||
/** | ||
* Resolves references in prepared json-schemas by collecting them in the definitions-section and referencing them. | ||
* @param pathType how to reference the type, i.e. which name to use | ||
*/ | ||
class JsonSchemaCompileReferenceStep(private val pathType: RefType = RefType.FULL) { | ||
|
||
private val schemaUtils = JsonSchemaUtils() | ||
|
||
/** | ||
* Put referenced schemas into definitions and reference them | ||
*/ | ||
fun compile(bundle: Bundle<JsonSchema>): CompiledJsonSchema { | ||
val schemaList = bundle.flatten() | ||
val typeDataMap = bundle.buildTypeDataMap() | ||
val definitions = mutableMapOf<String, JsonNode>() | ||
|
||
val root = resolveReferences(bundle.data.json) { refObj -> | ||
resolve(refObj, schemaList, typeDataMap, definitions) | ||
} | ||
|
||
return CompiledJsonSchema( | ||
typeData = bundle.data.typeData, | ||
json = root, | ||
definitions = definitions | ||
) | ||
} | ||
|
||
private fun resolve( | ||
refObj: JsonObject, | ||
schemaList: List<JsonSchema>, | ||
typeDataMap: Map<TypeId, BaseTypeData>, | ||
definitions: MutableMap<String, JsonNode> | ||
): JsonNode { | ||
val referencedId = TypeId.parse((refObj.properties["\$ref"] as JsonTextValue).value) | ||
val referencedSchema = schemaList.find(referencedId) | ||
return if (referencedSchema != null) { | ||
if (shouldReference(referencedSchema.json)) { | ||
val refPath = getRefPath(pathType, referencedSchema.typeData, typeDataMap) | ||
if(!definitions.containsKey(refPath)) { | ||
definitions[refPath] = placeholder() // break out of infinite loops | ||
definitions[refPath] = resolveReferences(referencedSchema.json) { resolve(it, schemaList, typeDataMap, definitions)} | ||
} | ||
schemaUtils.referenceSchema(refPath, true) | ||
} else { | ||
referencedSchema.json.also { | ||
if (it is JsonObject) { | ||
it.properties.putAll(buildMap { | ||
this.putAll(refObj.properties) | ||
this.remove("\$ref") | ||
}) | ||
} | ||
} | ||
} | ||
} else { | ||
refObj | ||
} | ||
} | ||
|
||
private fun placeholder() = obj { } | ||
|
||
private fun Collection<JsonSchema>.find(id: TypeId): JsonSchema? { | ||
return this.find { it.typeData.id == id } | ||
} | ||
|
||
} |
Oops, something went wrong.