Skip to content

Commit

Permalink
Add support for nullable keyword. (#265)
Browse files Browse the repository at this point in the history
* Add support for nullable keyword.
If a required property has its schema flagged as nullable, it is no longer flagged as a non null.

* fix openapi 3.1 nullable example
  • Loading branch information
cjbooms authored Feb 7, 2024
1 parent 467eb6c commit a8a6c85
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ object PropertyUtils {
}

fun PropertyInfo.isNullable() = when (this) {
is PropertyInfo.Field -> !isRequired && schema.default == null
is PropertyInfo.Field -> !isRequired && schema.default == null || schema.isNullable
else -> !isRequired
}

Expand Down
7 changes: 2 additions & 5 deletions src/test/resources/examples/jsonMergePatch/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,21 @@ public data class NullabilityCheck(
public val notNullWithDefaultNotRequired: JsonNullable<String> = JsonNullable.of(""),
@param:JsonProperty("nullable-with-default-not-required")
@get:JsonProperty("nullable-with-default-not-required")
@get:NotNull
public val nullableWithDefaultNotRequired: JsonNullable<String?> = JsonNullable.of(""),
@param:JsonProperty("not-null-no-default-required")
@get:JsonProperty("not-null-no-default-required")
@get:NotNull
public val notNullNoDefaultRequired: String,
@param:JsonProperty("nullable-no-default-required")
@get:JsonProperty("nullable-no-default-required")
@get:NotNull
public val nullableNoDefaultRequired: String,
public val nullableNoDefaultRequired: String?,
@param:JsonProperty("not-null-with-default-required")
@get:JsonProperty("not-null-with-default-required")
@get:NotNull
public val notNullWithDefaultRequired: String,
@param:JsonProperty("nullable-with-default-required")
@get:JsonProperty("nullable-with-default-required")
@get:NotNull
public val nullableWithDefaultRequired: String,
public val nullableWithDefaultRequired: String?,
)

public data class TopLevelLevelMergePatchInline(
Expand Down
4 changes: 1 addition & 3 deletions src/test/resources/examples/openapi310/models/Models.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package examples.openapi310.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import javax.validation.constraints.NotNull
import kotlin.String

public data class NewNullableFormat(
@param:JsonProperty("version")
@get:JsonProperty("version")
@get:NotNull
public val version: String,
public val version: String?,
)
6 changes: 5 additions & 1 deletion src/test/resources/examples/optionalVsRequired/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ components:
type: object
required:
- name
- requiredNullable
properties:
name:
type: string
gender:
type: string
format: uuid
format: uuid
requiredNullable:
type: string
nullable: true
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public data class OptionalVsRequired(
@param:JsonProperty("gender")
@get:JsonProperty("gender")
public val gender: UUID? = null,
@param:JsonProperty("requiredNullable")
@get:JsonProperty("requiredNullable")
public val requiredNullable: String?,
)

0 comments on commit a8a6c85

Please sign in to comment.