Skip to content

Commit

Permalink
Fix BigDecimal defaults. (#307)
Browse files Browse the repository at this point in the history
* Fix BigDecimal defaults.

* fix default name
  • Loading branch information
cjbooms authored Aug 6, 2024
1 parent 46487ff commit 97ff3c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/kotlin/com/cjbooms/fabrikt/generators/OasDefault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.cjbooms.fabrikt.util.NormalisedString.toEnumName
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.TypeName
import java.math.BigDecimal
import java.net.URI
import java.util.Base64

Expand All @@ -17,7 +18,12 @@ sealed class OasDefault {
CodeBlock.of("%S", strValue)
}

data class NumericValue(val numericValue: Number) : OasDefault() {
data class BigDecimalValue(val decimalValue: Number) : OasDefault() {
override fun getDefault(): CodeBlock =
CodeBlock.of("%T($decimalValue)", BigDecimal::class)
}

data class NumberValue(val numericValue: Number) : OasDefault() {
override fun getDefault(): CodeBlock =
CodeBlock.of("%L", numericValue)
}
Expand Down Expand Up @@ -69,7 +75,11 @@ sealed class OasDefault {
}
}

is Number -> OasDefault.NumericValue(default)
is Number ->
when (typeInfo) {
is KotlinTypeInfo.Numeric -> OasDefault.BigDecimalValue(default)
else -> OasDefault.NumberValue(default)
}
is Boolean -> OasDefault.BooleanValue(default)
else -> null
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/examples/defaultValues/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ components:
integer_default:
type: integer
default: 18
decimal_default:
type: number
default: 0.1
enum_default:
type: string
enum:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples.defaultValues.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import java.math.BigDecimal
import java.net.URI
import java.util.Base64
import javax.validation.constraints.NotNull
Expand All @@ -18,6 +19,10 @@ public data class PersonWithDefaults(
@get:JsonProperty("integer_default")
@get:NotNull
public val integerDefault: Int = 18,
@param:JsonProperty("decimal_default")
@get:JsonProperty("decimal_default")
@get:NotNull
public val decimalDefault: BigDecimal = BigDecimal(0.1),
@param:JsonProperty("enum_default")
@get:JsonProperty("enum_default")
@get:NotNull
Expand Down

0 comments on commit 97ff3c6

Please sign in to comment.