Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jun 21, 2024
2 parents c06f5e7 + 6834551 commit 021dd41
Show file tree
Hide file tree
Showing 25 changed files with 1,673 additions and 461 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kotlin.code.style=official
# project id
projectGroupId=io.github.smiley4
projectArtifactIdBase=schema-kenerator
projectVersion=1.0.0
projectVersion=1.0.1

# publishing information
projectNameBase=Schema-Kenerator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import io.github.smiley4.schemakenerator.jsonschema.data.RefType
import io.github.smiley4.schemakenerator.jsonschema.data.TitleType
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaAnnotationTypeHintStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaAutoTitleStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileInlineStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileReferenceRootStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileReferenceStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationDefaultStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationDeprecatedStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationDescriptionStep
Expand Down Expand Up @@ -56,24 +58,24 @@ fun Bundle<JsonSchema>.handleJsonSchemaAnnotations(): Bundle<JsonSchema> {


/**
* See [JsonSchemaCompileStep]
* See [JsonSchemaCompileInlineStep]
*/
fun Bundle<JsonSchema>.compileInlining(): CompiledJsonSchema {
return JsonSchemaCompileStep().compileInlining(this)
return JsonSchemaCompileInlineStep().compile(this)
}


/**
* See [JsonSchemaCompileStep]
* See [JsonSchemaCompileReferenceStep]
*/
fun Bundle<JsonSchema>.compileReferencing(pathType: RefType = RefType.FULL): CompiledJsonSchema {
return JsonSchemaCompileStep(pathType).compileReferencing(this)
return JsonSchemaCompileReferenceStep(pathType).compile(this)
}


/**
* See [JsonSchemaCompileStep]
* See [JsonSchemaCompileReferenceRootStep]
*/
fun Bundle<JsonSchema>.compileReferencingRoot(pathType: RefType = RefType.FULL): CompiledJsonSchema {
return JsonSchemaCompileStep(pathType).compileReferencingRoot(this)
return JsonSchemaCompileReferenceRootStep(pathType).compile(this)
}
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 }
}

}
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
)
}
}

}
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 }
}

}
Loading

0 comments on commit 021dd41

Please sign in to comment.