Skip to content

Commit 00a33c9

Browse files
committed
GH-220 Generate "nullable: true" for types explicitly marked as nullable (Resolve #220)
1 parent 634e9b7 commit 00a33c9

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

examples/javalin-gradle-kotlin/src/main/kotlin/io/javalin/openapi/plugin/test/KotlinEntity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ data class KotlinEntity(
1313
val name: String,
1414
val primitive: Int,
1515
val custom: Elements,
16-
val oneOfResult: Result
16+
val oneOfResult: Result,
17+
val nullable: Any?,
1718
)
1819

1920
@JsonSchema(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ internal class ComponentAnnotationsTest : OpenApiAnnotationProcessorSpecificatio
6767
"properties": {
6868
"testProperty": {
6969
"type": "number",
70+
"nullable": true,
7071
"format": "double"
7172
}
7273
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,25 @@ internal fun ClassDefinition.findAllProperties(requireNonNulls: Boolean): Collec
278278
else -> requireNonNulls && isNotNull
279279
}
280280

281+
val extra = mutableMapOf<String, Any?>()
282+
283+
val isExplicitlyNullable = when {
284+
customType?.nullability == Nullability.NULLABLE -> true
285+
property.hasAnnotation("Nullable") -> true
286+
else -> false
287+
}
288+
289+
if (isExplicitlyNullable) {
290+
extra["nullable"] = true
291+
}
292+
281293
properties.add(
282294
Property(
283295
name = name,
284296
type = propertyType.toClassDefinition(),
285297
composition = findCompositionInElement(context, property),
286298
required = required,
287-
extra = property.findExtra(context)
299+
extra = extra + property.findExtra(context)
288300
)
289301
)
290302
}

0 commit comments

Comments
 (0)