Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

package com.fincore.ledger.api.dto.response

import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fincore.ledger.api.serialization.MoneyAmountSerializer
import io.swagger.v3.oas.annotations.media.Schema
import java.math.BigDecimal
import java.time.Instant

data class BalanceResponse(
val accountId: String,
val currency: String,
@JsonSerialize(using = MoneyAmountSerializer::class)
@Schema(type = "string", format = "decimal", example = "100.00")
val amount: BigDecimal,
val lastPostedAt: Instant?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2026 FinCore Engine Authors

package com.fincore.ledger.api.serialization

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider
import java.math.BigDecimal

class MoneyAmountSerializer : JsonSerializer<BigDecimal>() {
override fun serialize(
value: BigDecimal,
gen: JsonGenerator,
serializers: SerializerProvider,
) {
gen.writeString(value.toPlainString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AccountControllerTest(
.andExpect(status().isOk)
.andExpect(jsonPath("$.accountId").value(id.toString()))
.andExpect(jsonPath("$.currency").value("EUR"))
.andExpect(jsonPath("$.amount").value(100.00))
.andExpect(jsonPath("$.amount").value("100.00"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2026 FinCore Engine Authors

package com.fincore.ledger.api.serialization

import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import java.math.BigDecimal

class MoneyAmountSerializerTest {
private data class Holder(
@JsonSerialize(using = MoneyAmountSerializer::class) val amount: BigDecimal,
)

private val mapper = jacksonObjectMapper()

@Test
fun `should serialize amount as a json string preserving trailing zeros`() {
mapper.writeValueAsString(Holder(BigDecimal("100.00"))) shouldBe """{"amount":"100.00"}"""
}

@Test
fun `should serialize a high scale amount losslessly without scientific notation`() {
val value = "12345678901234567890.123456789012345678"
mapper.writeValueAsString(Holder(BigDecimal(value))) shouldBe """{"amount":"$value"}"""
}
}
Loading