Skip to content

Commit

Permalink
manual customization steps for json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jul 26, 2024
1 parent 8e22d29 commit fa3fb96
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package io.github.smiley4.schemakenerator.jsonschema

import io.github.smiley4.schemakenerator.core.data.BaseTypeData
import io.github.smiley4.schemakenerator.core.data.Bundle
import io.github.smiley4.schemakenerator.core.data.PropertyData
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.data.TitleType
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonNode
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaAnnotationTypeHintStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaAutoTitleStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCompileInlineStep
Expand All @@ -16,6 +18,7 @@ import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotati
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationDescriptionStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationExamplesStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCoreAnnotationTitleStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaCustomizeStep
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaGenerationStep

/**
Expand Down Expand Up @@ -79,3 +82,19 @@ fun Bundle<JsonSchema>.compileReferencing(pathType: RefType = RefType.FULL): Com
fun Bundle<JsonSchema>.compileReferencingRoot(pathType: RefType = RefType.FULL): CompiledJsonSchema {
return JsonSchemaCompileReferenceRootStep(pathType).compile(this)
}


/**
* See [JsonSchemaCustomizeStep.customizeTypes]
*/
fun Bundle<JsonSchema>.customizeTypes(action: (typeData: BaseTypeData, typeSchema: JsonNode) -> Unit): Bundle<JsonSchema> {
return JsonSchemaCustomizeStep().customizeTypes(this, action)
}


/**
* See [JsonSchemaCustomizeStep.customizeProperties]
*/
fun Bundle<JsonSchema>.customizeProperties(action: (propertyData: PropertyData, propertySchema: JsonNode) -> Unit): Bundle<JsonSchema> {
return JsonSchemaCustomizeStep().customizeProperties(this, action)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.PropertyData
import io.github.smiley4.schemakenerator.jsonschema.data.JsonSchema
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonNode
import io.github.smiley4.schemakenerator.jsonschema.steps.JsonSchemaAnnotationUtils.iterateProperties

/**
* Further customization options
*/
class JsonSchemaCustomizeStep {

/**
* Provide a function that is called for each type and json-schema. Can be used to manually manipulate the generated json-schema.
*/
fun customizeTypes(
bundle: Bundle<JsonSchema>,
action: (typeData: BaseTypeData, typeSchema: JsonNode) -> Unit
): Bundle<JsonSchema> {
return bundle.also { schema ->
processTypes(schema.data, action)
schema.supporting.forEach { processTypes(it, action) }
}
}

private fun processTypes(schema: JsonSchema, action: (typeData: BaseTypeData, typeSchema: JsonNode) -> Unit) {
action(schema.typeData, schema.json)
}


/**
* Provide a function that is called for each property. Can be used to manually manipulate the generated json-schema.
*/
fun customizeProperties(
bundle: Bundle<JsonSchema>,
action: (propertyData: PropertyData, propertySchema: JsonNode) -> Unit
): Bundle<JsonSchema> {
return bundle.also { schema ->
processProperties(schema.data, action)
schema.supporting.forEach { processProperties(it, action) }
}
}

private fun processProperties(schema: JsonSchema, action: (typeData: PropertyData, typeSchema: JsonNode) -> Unit) {
iterateProperties(schema) { prop, data ->
action(data, prop)
}
}

}

0 comments on commit fa3fb96

Please sign in to comment.