From 8e2e4190a2bfabe2cb5c3a0a12b1f40355eb9a22 Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Mon, 16 Sep 2024 22:36:08 +0100 Subject: [PATCH 1/4] Fix default array issue. We can only support basic defaults for field types, and should ignore any more complicated defaults for now. Closes #312 --- .../kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt | 5 +++-- src/test/resources/examples/arrays/api.yaml | 5 +++++ .../examples/arrays/models/ContainsArrayOfArrays.kt | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) 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/arrays/api.yaml b/src/test/resources/examples/arrays/api.yaml index 0a1670cc..d619e3d6 100644 --- a/src/test/resources/examples/arrays/api.yaml +++ b/src/test/resources/examples/arrays/api.yaml @@ -16,6 +16,11 @@ components: properties: some-array-prop: type: string + array_with_empty_default: + type: array + default: [] + items: + type: string a-nullable-array: type: array nullable: true diff --git a/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt b/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt index 3264a982..ad722921 100644 --- a/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt +++ b/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt @@ -14,6 +14,9 @@ public data class ContainsArrayOfArrays( @get:JsonProperty("absent-object-type-in-array") @get:Valid public val absentObjectTypeInArray: List? = null, + @param:JsonProperty("array_with_empty_default") + @get:JsonProperty("array_with_empty_default") + public val arrayWithEmptyDefault: List? = null, @param:JsonProperty("a-nullable-array") @get:JsonProperty("a-nullable-array") public val aNullableArray: List? = null, From 948d67688506736b1dbf4312aa6ccfa29cc851dd Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Mon, 16 Sep 2024 22:44:51 +0100 Subject: [PATCH 2/4] move test to better location --- src/test/resources/examples/arrays/api.yaml | 5 ----- .../examples/arrays/models/ContainsArrayOfArrays.kt | 3 --- src/test/resources/examples/defaultValues/api.yaml | 5 +++++ .../examples/defaultValues/models/PersonWithDefaults.kt | 4 ++++ 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/test/resources/examples/arrays/api.yaml b/src/test/resources/examples/arrays/api.yaml index d619e3d6..0a1670cc 100644 --- a/src/test/resources/examples/arrays/api.yaml +++ b/src/test/resources/examples/arrays/api.yaml @@ -16,11 +16,6 @@ components: properties: some-array-prop: type: string - array_with_empty_default: - type: array - default: [] - items: - type: string a-nullable-array: type: array nullable: true diff --git a/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt b/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt index ad722921..3264a982 100644 --- a/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt +++ b/src/test/resources/examples/arrays/models/ContainsArrayOfArrays.kt @@ -14,9 +14,6 @@ public data class ContainsArrayOfArrays( @get:JsonProperty("absent-object-type-in-array") @get:Valid public val absentObjectTypeInArray: List? = null, - @param:JsonProperty("array_with_empty_default") - @get:JsonProperty("array_with_empty_default") - public val arrayWithEmptyDefault: List? = null, @param:JsonProperty("a-nullable-array") @get:JsonProperty("a-nullable-array") public val aNullableArray: List? = null, diff --git a/src/test/resources/examples/defaultValues/api.yaml b/src/test/resources/examples/defaultValues/api.yaml index bf0c8361..68093eb3 100644 --- a/src/test/resources/examples/defaultValues/api.yaml +++ b/src/test/resources/examples/defaultValues/api.yaml @@ -46,3 +46,8 @@ components: type: string format: byte default: U3dhZ2dlciByb2Nrcw== + array_default: + type: array + default: [] + items: + type: string diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt index e9150a90..faa904a0 100644 --- a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt @@ -9,6 +9,7 @@ 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 +49,7 @@ public data class PersonWithDefaults( @get:JsonProperty("byte_type") @get:NotNull public val byteType: ByteArray = Base64.getDecoder().decode("U3dhZ2dlciByb2Nrcw=="), + @param:JsonProperty("array_default") + @get:JsonProperty("array_default") + public val arrayDefault: List? = null, ) From 9aa27b54846fd21bacc6b4dacc6e3d3c303e6199 Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Mon, 16 Sep 2024 22:50:16 +0100 Subject: [PATCH 3/4] move test to better location --- src/test/resources/examples/defaultValues/api.yaml | 13 ++++++++++++- .../defaultValues/models/PersonWithDefaults.kt | 11 ++++++++--- .../models/PersonWithDefaultsIgnoredObject.kt | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt diff --git a/src/test/resources/examples/defaultValues/api.yaml b/src/test/resources/examples/defaultValues/api.yaml index 68093eb3..d8659560 100644 --- a/src/test/resources/examples/defaultValues/api.yaml +++ b/src/test/resources/examples/defaultValues/api.yaml @@ -46,8 +46,19 @@ components: type: string format: byte default: U3dhZ2dlciByb2Nrcw== - array_default: + ignored_array_default: type: array default: [] items: type: string + ignored_object: + 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 faa904a0..bb2567b6 100644 --- a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt @@ -4,6 +4,7 @@ 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 @@ -49,7 +50,11 @@ public data class PersonWithDefaults( @get:JsonProperty("byte_type") @get:NotNull public val byteType: ByteArray = Base64.getDecoder().decode("U3dhZ2dlciByb2Nrcw=="), - @param:JsonProperty("array_default") - @get:JsonProperty("array_default") - public val arrayDefault: List? = null, + @param:JsonProperty("ignored_array_default") + @get:JsonProperty("ignored_array_default") + public val ignoredArrayDefault: List? = null, + @param:JsonProperty("ignored_object") + @get:JsonProperty("ignored_object") + @get:Valid + public val ignoredObject: PersonWithDefaultsIgnoredObject? = null, ) diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt new file mode 100644 index 00000000..7db05b07 --- /dev/null +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt @@ -0,0 +1,14 @@ +package examples.defaultValues.models + +import com.fasterxml.jackson.`annotation`.JsonProperty +import kotlin.Int +import kotlin.String + +public data class PersonWithDefaultsIgnoredObject( + @param:JsonProperty("name") + @get:JsonProperty("name") + public val name: String? = null, + @param:JsonProperty("age") + @get:JsonProperty("age") + public val age: Int? = null, +) From 5ad522153ed5e4e8f1d14cf5d78f2e2c01611862 Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Mon, 16 Sep 2024 22:52:47 +0100 Subject: [PATCH 4/4] move test to better location --- src/test/resources/examples/defaultValues/api.yaml | 2 +- .../examples/defaultValues/models/PersonWithDefaults.kt | 6 +++--- ...dObject.kt => PersonWithDefaultsIgnoredObjectDefault.kt} | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/test/resources/examples/defaultValues/models/{PersonWithDefaultsIgnoredObject.kt => PersonWithDefaultsIgnoredObjectDefault.kt} (84%) diff --git a/src/test/resources/examples/defaultValues/api.yaml b/src/test/resources/examples/defaultValues/api.yaml index d8659560..69ef49ea 100644 --- a/src/test/resources/examples/defaultValues/api.yaml +++ b/src/test/resources/examples/defaultValues/api.yaml @@ -51,7 +51,7 @@ components: default: [] items: type: string - ignored_object: + ignored_object_default: type: object properties: name: diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt index bb2567b6..31343297 100644 --- a/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaults.kt @@ -53,8 +53,8 @@ public data class PersonWithDefaults( @param:JsonProperty("ignored_array_default") @get:JsonProperty("ignored_array_default") public val ignoredArrayDefault: List? = null, - @param:JsonProperty("ignored_object") - @get:JsonProperty("ignored_object") + @param:JsonProperty("ignored_object_default") + @get:JsonProperty("ignored_object_default") @get:Valid - public val ignoredObject: PersonWithDefaultsIgnoredObject? = null, + public val ignoredObjectDefault: PersonWithDefaultsIgnoredObjectDefault? = null, ) diff --git a/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt similarity index 84% rename from src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt rename to src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt index 7db05b07..3b23ebfe 100644 --- a/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObject.kt +++ b/src/test/resources/examples/defaultValues/models/PersonWithDefaultsIgnoredObjectDefault.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.`annotation`.JsonProperty import kotlin.Int import kotlin.String -public data class PersonWithDefaultsIgnoredObject( +public data class PersonWithDefaultsIgnoredObjectDefault( @param:JsonProperty("name") @get:JsonProperty("name") public val name: String? = null,