Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigDecimal defaults. #307

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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