Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class KotlinNullablePropertyCustomizer(
/**
* Marks a non-$ref property as nullable.
* - OAS 3.0: `nullable: true`
* - OAS 3.1: adds `"null"` to the `types` set
* - OAS 3.1: adds `"null"` to the `types` set, or to `oneOf` for a composed schema
*
* A schema representing `Any?` carries no `type`/`types` constraint, i.e. it is an
* "any" schema which already permits any value including `null`. Such a schema is left
Expand All @@ -138,9 +138,15 @@ class KotlinNullablePropertyCustomizer(
if (isAnySchema(property)) return

if (specVersion == SpecVersion.V31) {
val currentTypes = property.types ?: property.type?.let { setOf(it) } ?: emptySet()
if ("null" !in currentTypes) {
property.types = currentTypes + "null"
if (!property.oneOf.isNullOrEmpty()) {
if (property.oneOf.none { it.type == "null" || "null" in (it.types ?: emptySet()) }) {
property.oneOf = property.oneOf + Schema<Any>().apply { addType("null") }
}
} else {
val currentTypes = property.types ?: property.type?.let { setOf(it) } ?: emptySet()
if ("null" !in currentTypes) {
property.types = currentTypes + "null"
}
}
} else {
property.nullable = true
Expand All @@ -155,7 +161,8 @@ class KotlinNullablePropertyCustomizer(
private fun isAnySchema(property: Schema<*>): Boolean =
property.`$ref` == null &&
property.type == null &&
property.types.isNullOrEmpty()
property.types.isNullOrEmpty() &&
property.oneOf.isNullOrEmpty()

/**
* Wraps a $ref property in a nullable composite schema. A fresh wrapper schema is returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ data class NullableFieldsResponse(
val nullableAny: Any? = null,
@field:Schema(description = "The nested object")
val nullableNested: NestedObject? = null,
val nullableComposed: ComposedObject? = null,
)

data class NestedObject(
val name: String,
val description: String? = null,
)

sealed class ComposedObject {
data class First(val first: String) : ComposedObject()

data class Second(val second: String) : ComposedObject()
}

@RestController
class NullableController {
@GetMapping("/nullable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ data class NullableFieldsResponse(
val nullableAny: Any? = null,
@field:Schema(description = "The nested object")
val nullableNested: NestedObject? = null,
val nullableComposed: ComposedObject? = null,
)

data class NestedObject(
val name: String,
val description: String? = null,
)

sealed class ComposedObject {
data class First(val first: String) : ComposedObject()

data class Second(val second: String) : ComposedObject()
}

@RestController
class NullableController {
@GetMapping("/nullable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@
},
"components": {
"schemas": {
"ComposedObject": {
"type": "object"
},
"First": {
"required": [
"first"
],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/ComposedObject"
},
{
"type": "object",
"properties": {
"first": {
"type": "string"
}
}
}
]
},
"NestedObject": {
"required": [
"name"
Expand Down Expand Up @@ -79,8 +101,38 @@
"$ref": "#/components/schemas/NestedObject"
}
]
},
"nullableComposed": {
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/First"
},
{
"$ref": "#/components/schemas/Second"
}
]
}
}
},
"Second": {
"required": [
"second"
],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/ComposedObject"
},
{
"type": "object",
"properties": {
"second": {
"type": "string"
}
}
}
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@
},
"components": {
"schemas": {
"ComposedObject": { },
"First": {
"allOf": [
{
"$ref": "#/components/schemas/ComposedObject"
},
{
"type": "object",
"properties": {
"first": {
"type": "string"
}
}
}
],
"required": [
"first"
]
},
"NestedObject": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -81,11 +100,42 @@
"type": "null"
}
]
},
"nullableComposed": {
"oneOf": [
{
"$ref": "#/components/schemas/First"
},
{
"$ref": "#/components/schemas/Second"
},
{
"type": "null"
}
]
}
},
"required": [
"requiredField"
]
},
"Second": {
"allOf": [
{
"$ref": "#/components/schemas/ComposedObject"
},
{
"type": "object",
"properties": {
"second": {
"type": "string"
}
}
}
],
"required": [
"second"
]
}
}
}
Expand Down