Skip to content

Commit

Permalink
Fix default array issue. (#315)
Browse files Browse the repository at this point in the history
* Fix default array issue.
We can only support basic defaults for field types, and should ignore any more complicated defaults for now. Closes #312
  • Loading branch information
cjbooms authored Sep 16, 2024
1 parent 6528da2 commit a8e8cbb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/examples/defaultValues/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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<String>? = null,
@param:JsonProperty("ignored_object_default")
@get:JsonProperty("ignored_object_default")
@get:Valid
public val ignoredObjectDefault: PersonWithDefaultsIgnoredObjectDefault? = null,
)
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit a8e8cbb

Please sign in to comment.