Skip to content

Commit 87f7bdf

Browse files
authored
GH-280 Support x-enum-descriptions for enums (#281)
1 parent b2f760c commit 87f7bdf

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎openapi-annotation-processor/src/main/kotlin/io/javalin/openapi/experimental/processor/generators/TypeSchemaGenerator.kt‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,41 @@ class TypeSchemaGenerator(val context: AnnotationProcessorContext) {
4242

4343
val namingStrategy = source.getAnnotation(OpenApiNaming::class.java)?.value
4444
val values = createArrayNode()
45+
val descriptions = createArrayNode()
4546

4647
source.enclosedElements
4748
.filterIsInstance<VariableElement>()
4849
.filter { it.modifiers.contains(Modifier.STATIC) }
4950
.filter { context.isAssignable(it.asType(), type.mirror) }
5051
.map { element ->
5152
val customName = element.getAnnotation(OpenApiName::class.java)
52-
when {
53+
val description = element.getAnnotation(OpenApiDescription::class.java)
54+
val name = when {
5355
customName != null -> customName.value
5456
namingStrategy != null -> translatePropertyName(namingStrategy, element.toSimpleName())
5557
else -> element.toSimpleName()
5658
}
59+
60+
Pair(name, description?.value ?: "")
5761
}
58-
.forEach { name ->
62+
.forEach { (name, description) ->
5963
if (enumType != null && enumType.type != "string") {
6064
values.add(jsonMapper.readTree(name))
6165
} else {
6266
values.add(name)
6367
}
68+
69+
descriptions.add(description)
6470
}
6571

6672
schema.put("type", enumType?.type ?: "string")
6773
enumType?.format?.also { schema.put("format", it) }
6874
schema.set<JsonNode>("enum", values)
6975

76+
if (descriptions.find({ description -> description.isTextual && description.asText().isNotEmpty()}) != null) {
77+
schema.set<JsonNode>("x-enum-descriptions", descriptions)
78+
}
79+
7080
val extra = source.findExtra(context)
7181
schema.addExtra(extra)
7282
}

‎openapi-annotation-processor/src/test/kotlin/io/javalin/openapi/processor/TypeMappersTest.kt‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,39 @@ internal class TypeMappersTest : OpenApiAnnotationProcessorSpecification() {
418418
))
419419
}
420420

421+
private enum class XEnumDescriptionsEnum {
422+
@OpenApiName("1")
423+
@OpenApiDescription("Description of 1")
424+
REQUEST_TYPE_1,
425+
@OpenApiName("2")
426+
@OpenApiDescription("Description of 2")
427+
REQUEST_TYPE_2
428+
}
429+
430+
@OpenApi(
431+
path = "x-enum-descriptions-enum",
432+
versions = ["should_support_x_enum_descriptions_enum"],
433+
responses = [OpenApiResponse(status = "200", content = [OpenApiContent(from = XEnumDescriptionsEnum::class)])]
434+
)
435+
@Test
436+
fun should_support_x_enum_descriptions_enum() = withOpenApi("should_support_x_enum_descriptions_enum") {
437+
println(it)
438+
439+
assertThatJson(it)
440+
.inPath("$.components.schemas.XEnumDescriptionsEnum")
441+
.isObject
442+
.isEqualTo(json(
443+
// language=json
444+
"""
445+
{
446+
"type": "string",
447+
"enum": ["1", "2"],
448+
"x-enum-descriptions": ["Description of 1", "Description of 2"]
449+
}
450+
"""
451+
))
452+
}
453+
421454
@OpenApiDescription("A regular string enum with description")
422455
private enum class DescribedStringEnum {
423456
ALPHA,

0 commit comments

Comments
 (0)