У меня есть классы kotlin с наследованием:
abstract class Classification(
val accuracy: Double
)
@Document
class OrderForm(
@Id
val id: String,
accuracy: Double,
val customerNumber: String,
val orders: List<Order>
) : Classification(accuracy)
И я реализовал тест сериализации:
internal class OrderFormTest : Logging {
private val objectMapper = ObjectMapper().registerKotlinModule()
@Test
fun canBeSerialisedToJsonAndBack() {
val expected = OrderForm(
"123_ID",
94.77,
"123_CUSTOMER",
listOf(
Order(
0.0,
OrderNumber(
10.0,
123456789L),
OrderSize(
99.99,
"38/40"),
OrderAmount(
100.0,
7),
OrderPrice(
50.0,
999.99),
OrderPage(
23.29343,
100)
),
Order(
0.0,
OrderNumber(
10.0,
123456789L),
OrderSize(
99.99,
"38/40"),
OrderAmount(
100.0,
7),
OrderPrice(
50.0,
999.99),
OrderPage(
23.29343,
100)
)
)
)
logger.info("expected: $expected")
val json = objectMapper.writeValueAsString(expected)
logger.info("json: $json")
val result: OrderForm = objectMapper.readValue(json)
logger.info("result: $result")
val json2 = objectMapper.writeValueAsString(result);
logger.info("json: $json2")
Assertions.assertThat(result).isEqualTo(expected)
}
}
Вывод журнала:
14:57:37.031 [main] OrderFormTest - expected: de.fronetic.witt.ml.backend.model.OrderForm@4b34fff9
14:57:37.623 [main] OrderFormTest - json: {"id":"123_ID","accuracy":94.77,"customerNumber":"123_CUSTOMER","orders":[{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}},{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}}]}
14:57:37.673 [main] OrderFormTest - result: de.fronetic.witt.ml.backend.model.OrderForm@38b972d7
14:57:37.674 [main] OrderFormTest - json: {"id":"123_ID","accuracy":94.77,"customerNumber":"123_CUSTOMER","orders":[{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}},{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}}]}
Таким образом, содержимое объектов точно такое же, но сами объекты не равны!
Если я не использую наследование, этого не происходит:
@Document
data class OrderForm(
@Id
val id: String,
val accuracy: Double,
val customerNumber: String,
val orders: List<Order>
)
Вывод журнала:
14:59:03.889 [main] OrderFormTest - expected: OrderForm(id=123_ID, accuracy=94.77, customerNumber=123_CUSTOMER, orders=[Order(accuracy=0.0, orderNumber=OrderNumber(accuracy=10.0, number=123456789), orderSize=OrderSize(accuracy=99.99, size=38/40), orderAmount=OrderAmount(accuracy=100.0, amount=7), orderPrice=OrderPrice(accuracy=50.0, price=999.99), orderPage=OrderPage(accuracy=23.29343, page=100)), Order(accuracy=0.0, orderNumber=OrderNumber(accuracy=10.0, number=123456789), orderSize=OrderSize(accuracy=99.99, size=38/40), orderAmount=OrderAmount(accuracy=100.0, amount=7), orderPrice=OrderPrice(accuracy=50.0, price=999.99), orderPage=OrderPage(accuracy=23.29343, page=100))])
14:59:04.833 [main] OrderFormTest - json: {"id":"123_ID","accuracy":94.77,"customerNumber":"123_CUSTOMER","orders":[{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}},{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}}]}
14:59:04.913 [main] OrderFormTest - result: OrderForm(id=123_ID, accuracy=94.77, customerNumber=123_CUSTOMER, orders=[Order(accuracy=0.0, orderNumber=OrderNumber(accuracy=10.0, number=123456789), orderSize=OrderSize(accuracy=99.99, size=38/40), orderAmount=OrderAmount(accuracy=100.0, amount=7), orderPrice=OrderPrice(accuracy=50.0, price=999.99), orderPage=OrderPage(accuracy=23.29343, page=100)), Order(accuracy=0.0, orderNumber=OrderNumber(accuracy=10.0, number=123456789), orderSize=OrderSize(accuracy=99.99, size=38/40), orderAmount=OrderAmount(accuracy=100.0, amount=7), orderPrice=OrderPrice(accuracy=50.0, price=999.99), orderPage=OrderPage(accuracy=23.29343, page=100))])
14:59:04.914 [main] OrderFormTest - json: {"id":"123_ID","accuracy":94.77,"customerNumber":"123_CUSTOMER","orders":[{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}},{"accuracy":0.0,"orderNumber":{"accuracy":10.0,"number":123456789},"orderSize":{"accuracy":99.99,"size":"38/40"},"orderAmount":{"accuracy":100.0,"amount":7},"orderPrice":{"accuracy":50.0,"price":999.99},"orderPage":{"accuracy":23.29343,"page":100}}]}
Почему это происходит?