-
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.
Merge branch 'feature/detect-optional-fields' into develop
- Loading branch information
Showing
16 changed files
with
690 additions
and
22 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...rator-core/src/main/kotlin/io/github/smiley4/schemakenerator/core/annotations/Optional.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,13 @@ | ||
package io.github.smiley4.schemakenerator.core.annotations | ||
|
||
|
||
/** | ||
* Specifies that the annotated object is optional, i.e. not required. | ||
*/ | ||
@Target( | ||
AnnotationTarget.PROPERTY, | ||
AnnotationTarget.FIELD, | ||
AnnotationTarget.FUNCTION | ||
) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class Optional |
13 changes: 13 additions & 0 deletions
13
...rator-core/src/main/kotlin/io/github/smiley4/schemakenerator/core/annotations/Required.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,13 @@ | ||
package io.github.smiley4.schemakenerator.core.annotations | ||
|
||
|
||
/** | ||
* Specifies that the annotated object is required. | ||
*/ | ||
@Target( | ||
AnnotationTarget.PROPERTY, | ||
AnnotationTarget.FIELD, | ||
AnnotationTarget.FUNCTION | ||
) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class Required |
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
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
71 changes: 71 additions & 0 deletions
71
...iley4/schemakenerator/jsonschema/steps/JsonSchemaCoreAnnotationOptionalAndRequiredStep.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,71 @@ | ||
package io.github.smiley4.schemakenerator.jsonschema.steps | ||
|
||
import io.github.smiley4.schemakenerator.core.annotations.Optional | ||
import io.github.smiley4.schemakenerator.core.annotations.Required | ||
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.JsonArray | ||
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.steps.JsonSchemaAnnotationUtils.iterateProperties | ||
|
||
/** | ||
* Sets properties as optional/required from core [Optional] and [Required]-annotation. | ||
*/ | ||
class JsonSchemaCoreAnnotationOptionalAndRequiredStep { | ||
|
||
fun process(bundle: Bundle<JsonSchema>): Bundle<JsonSchema> { | ||
return bundle.also { schema -> | ||
process(schema.data) | ||
schema.supporting.forEach { process(it) } | ||
} | ||
} | ||
|
||
private fun process(schema: JsonSchema) { | ||
iterateProperties(schema) { prop, data -> | ||
determineRequired(data)?.also { required -> | ||
if (required) { | ||
addRequired(schema, data.name) | ||
} else { | ||
removeRequired(schema, data.name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun determineRequired(typeData: PropertyData): Boolean? { | ||
if (typeData.annotations.any { it.name == Required::class.qualifiedName }) { | ||
return true | ||
} | ||
if (typeData.annotations.any { it.name == Optional::class.qualifiedName }) { | ||
return false | ||
} | ||
return null | ||
} | ||
|
||
private fun getRequiredList(schema: JsonSchema): MutableList<JsonNode> { | ||
val json = schema.json | ||
if (json is JsonObject) { | ||
val required = json.properties["required"] | ||
if (required is JsonArray) { | ||
return required.items | ||
} | ||
} | ||
return mutableListOf() | ||
} | ||
|
||
private fun addRequired(schema: JsonSchema, propertyName: String) { | ||
val list = getRequiredList(schema) | ||
if (list.none { (it as JsonTextValue).value == propertyName }) { | ||
list.add(JsonTextValue(propertyName)) | ||
} | ||
} | ||
|
||
private fun removeRequired(schema: JsonSchema, propertyName: String) { | ||
val list = getRequiredList(schema) | ||
list.removeIf { (it as JsonTextValue).value == propertyName } | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.