-
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.
add optional and required annotation
- Loading branch information
Showing
8 changed files
with
171 additions
and
14 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
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
60 changes: 60 additions & 0 deletions
60
...iley4/schemakenerator/swagger/steps/SwaggerSchemaCoreAnnotationOptionalAndRequiredStep.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,60 @@ | ||
package io.github.smiley4.schemakenerator.swagger.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.swagger.data.SwaggerSchema | ||
import io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaAnnotationUtils.iterateProperties | ||
|
||
/** | ||
* Sets properties as optional/required from core [Optional] and [Required]-annotation. | ||
*/ | ||
class SwaggerSchemaCoreAnnotationOptionalAndRequiredStep { | ||
|
||
fun process(bundle: Bundle<SwaggerSchema>): Bundle<SwaggerSchema> { | ||
return bundle.also { schema -> | ||
process(schema.data) | ||
schema.supporting.forEach { process(it) } | ||
} | ||
} | ||
|
||
private fun process(schema: SwaggerSchema) { | ||
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: SwaggerSchema): MutableList<String> { | ||
return schema.swagger.required ?: mutableListOf() | ||
} | ||
|
||
private fun addRequired(schema: SwaggerSchema, propertyName: String) { | ||
val list = getRequiredList(schema) | ||
if (list.none { it == propertyName }) { | ||
list.add(propertyName) | ||
} | ||
} | ||
|
||
private fun removeRequired(schema: SwaggerSchema, propertyName: String) { | ||
val list = getRequiredList(schema) | ||
list.removeIf { it == 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
8 changes: 4 additions & 4 deletions
8
...n/io/github/smiley4/schemakenerator/test/models/reflection/ClassWithOptionalParameters.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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package io.github.smiley4.schemakenerator.test.models.reflection | ||
|
||
class ClassWithOptionalParameters( | ||
val ctorRequired: String, | ||
val ctorOptional: String = "test", | ||
val ctorRequiredNullable: String?, | ||
val ctorOptionalNullable: String? = null | ||
val someList: List<String>, | ||
val someSet: Set<String>, | ||
val someMap: Map<String, Int>, | ||
val someArray: IntArray, | ||
) |