-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
manual customization steps for json schema
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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
52 changes: 52 additions & 0 deletions
52
...main/kotlin/io/github/smiley4/schemakenerator/jsonschema/steps/JsonSchemaCustomizeStep.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,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) | ||
} | ||
} | ||
|
||
} |