diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt index 79ccd197..68360777 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt @@ -205,9 +205,10 @@ object PropertyUtils { } fun PropertyInfo.isNullable() = when (this) { - is PropertyInfo.Field, is PropertyInfo.ListField, is PropertyInfo.MapField, + is PropertyInfo.Field -> !isRequired && schema.default == null || schema.isNullable + is PropertyInfo.ListField, is PropertyInfo.MapField, is PropertyInfo.ObjectRefField, is PropertyInfo.ObjectInlinedField -> - !isRequired && schema.default == null || schema.isNullable + !isRequired || schema.isNullable else -> !isRequired } diff --git a/src/test/resources/examples/defaultValues/api.yaml b/src/test/resources/examples/defaultValues/api.yaml index bf0c8361..69ef49ea 100644 --- a/src/test/resources/examples/defaultValues/api.yaml +++ b/src/test/resources/examples/defaultValues/api.yaml @@ -46,3 +46,19 @@ components: type: string format: byte default: U3dhZ2dlciByb2Nrcw== + ignored_array_default: + type: array + default: [] + items: + type: string + ignored_object_default: + type: object + properties: + name: + type: string + age: + type: integer + default: + name: "John Doe" + age: 30 + diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt index e9150a90..31343297 100644 --- a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt @@ -4,11 +4,13 @@ import com.fasterxml.jackson.`annotation`.JsonProperty import java.math.BigDecimal import java.net.URI import java.util.Base64 +import javax.validation.Valid import javax.validation.constraints.NotNull import kotlin.Boolean import kotlin.ByteArray import kotlin.Int import kotlin.String +import kotlin.collections.List public data class PersonWithDefaults( @param:JsonProperty("required_so_default_ignored") @@ -48,4 +50,11 @@ public data class PersonWithDefaults( @get:JsonProperty("byte_type") @get:NotNull public val byteType: ByteArray = Base64.getDecoder().decode("U3dhZ2dlciByb2Nrcw=="), + @param:JsonProperty("ignored_array_default") + @get:JsonProperty("ignored_array_default") + public val ignoredArrayDefault: List? = null, + @param:JsonProperty("ignored_object_default") + @get:JsonProperty("ignored_object_default") + @get:Valid + public val ignoredObjectDefault: PersonWithDefaultsIgnoredObjectDefault? = null, ) diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt new file mode 100644 index 00000000..3b23ebfe --- /dev/null +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt @@ -0,0 +1,14 @@ +package examples.defaultValues.models + +import com.fasterxml.jackson.`annotation`.JsonProperty +import kotlin.Int +import kotlin.String + +public data class PersonWithDefaultsIgnoredObjectDefault( + @param:JsonProperty("name") + @get:JsonProperty("name") + public val name: String? = null, + @param:JsonProperty("age") + @get:JsonProperty("age") + public val age: Int? = null, +)