-
Notifications
You must be signed in to change notification settings - Fork 43
Create a proper Example system #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Oh, I'm still waiting... I hope you'll make it as soon as you can... |
Hi @Wicpar, I implemented an Let me know if you prefer me to open a different issue for this, I thought the subjects were somewhat connected. The rationale behind the change is that I'm using Rapidoc instead of Swagger UI, and Rapidoc ignores the examples entirely and uses only the "example" attribute each field. The annotation is used like this: data class Result(
@Description("Hexadecimal UID of the card")
@Example("04D3AC7A124A80")
var cardUID: String = "",
@Description("Card type: A, B o M")
@Example("M")
var cardType: String = "",
@Example(intValue = 200)
var someIntField: Int = 0
) This is the implementation: @Target(AnnotationTarget.TYPE, AnnotationTarget.PROPERTY)
@SchemaProcessorAnnotation(ExampleValueProcessor::class)
annotation class Example(
val stringValue: String = "",
val intValue: Int = 0,
val longValue: Long = 0,
val doubleValue: Double = 0.0,
val boolValue: Boolean = false,
val isNull: Boolean = false
) The processor is the only thing that's a little ugly, but probably can be improved: object ExampleValueProcessor: SchemaProcessor<Example> {
@Suppress("UNCHECKED_CAST")
override fun process(model: SchemaModel<*>, type: KType, annotation: Example): SchemaModel<*> {
when (model) {
is SchemaModel.SchemaModelLitteral<*> -> {
(model as SchemaModel.SchemaModelLitteral<Any?>).apply {
when(model.type) {
DataType.integer, DataType.number -> {
example = when {
annotation.intValue != 0 -> annotation.intValue
annotation.longValue != 0L -> annotation.longValue
annotation.doubleValue != 0.0 -> annotation.doubleValue
annotation.stringValue.isNotBlank() -> BigDecimal(annotation.stringValue)
else -> 0
}
}
DataType.boolean -> {
example = annotation.boolValue
}
DataType.`object`, DataType.array -> {
throw Exception("Type ${model.type} not supported for examples")
}
else -> { /* string */
example = annotation.stringValue
}
}
}
}
is SchemaModel.SchemaModelEnum<*> -> {
(model as SchemaModel.SchemaModelEnum<Any?>).apply {
example = annotation.stringValue
}
}
else -> {
throw Exception("${annotation::class} can't be applied to $model")
}
}
return model
}
} |
The approach I would have taken is to just take a string and convert it based on |
@JavierPAYTEF What do you think about that, but a purely string based annotation that parses the content as json (or other based on a secondary optional property) ? |
@Wicpar Hi, sorry, I've had a lot of work this past few weeks, I couldn't work on this. |
There is no way to handle multiple types of primitives in annotations in a cleaner way than you did. |
Do you maybe have an example or somewhere I can read some documentation? I tried researching on my own but my knowledge of annotations is not the best, so I will follow your lead since you probably know more than me in that regard. |
I don't know if an exhaustive guide about annotations exist, i just learned how to use them by fiddling around with them, and looking at annotation-heavy projects like spring. |
A system needs to be created that allows to provide one or more examples with their metadata without adding bulk to the minimal configuration.
The text was updated successfully, but these errors were encountered: