Refine conversion directions between boolean and byte using MySQL/MariaDB.

ByteToBooleanConverter is now marked as reading converter and we've added a writing converter with BooleanToByteConverter to ensure proper conversion. Previously, all byte values were converted to boolean resulting in data precision loss.

Closes #589
This commit is contained in:
Mark Paluch
2021-05-19 10:37:10 +02:00
parent f1ac5bae56
commit c8122a9b41
2 changed files with 140 additions and 2 deletions

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.convert;
import static org.assertj.core.api.Assertions.*;
import io.r2dbc.spi.test.MockColumnMetadata;
import io.r2dbc.spi.test.MockRow;
import io.r2dbc.spi.test.MockRowMetadata;
import lombok.Value;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.r2dbc.dialect.MySqlDialect;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.testing.OutboundRowAssert;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
/**
* MySQL-specific unit tests for {@link MappingR2dbcConverter}.
*
* @author Mark Paluch
*/
class MySqlMappingR2dbcConverterUnitTests {
private RelationalMappingContext mappingContext = new R2dbcMappingContext();
private MappingR2dbcConverter converter = new MappingR2dbcConverter(mappingContext);
@BeforeEach
void before() {
List<Object> converters = new ArrayList<>(MySqlDialect.INSTANCE.getConverters());
converters.addAll(R2dbcCustomConversions.STORE_CONVERTERS);
CustomConversions.StoreConversions storeConversions = CustomConversions.StoreConversions
.of(MySqlDialect.INSTANCE.getSimpleTypeHolder(), converters);
R2dbcCustomConversions customConversions = new R2dbcCustomConversions(storeConversions, Collections.emptyList());
mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
converter = new MappingR2dbcConverter(mappingContext, customConversions);
}
@Test // gh-589
void shouldWriteBooleanToByte() {
BooleanMapping object = new BooleanMapping(0, true, false);
OutboundRow row = new OutboundRow();
converter.write(object, row);
OutboundRowAssert.assertThat(row).containsColumnWithValue("flag1", (byte) 1).containsColumnWithValue("flag2",
(byte) 0);
}
@Test // gh-589
void shouldReadByteToBoolean() {
MockRow row = MockRow.builder().identified("flag1", Object.class, (byte) 1)
.identified("flag2", Object.class, (byte) 0).build();
MockRowMetadata metadata = MockRowMetadata.builder()
.columnMetadata(MockColumnMetadata.builder().name("flag1").build())
.columnMetadata(MockColumnMetadata.builder().name("flag2").build()).build();
BooleanMapping mapped = converter.read(BooleanMapping.class, row, metadata);
assertThat(mapped.flag1).isTrue();
assertThat(mapped.flag2).isFalse();
}
@Test // gh-589
void shouldPreserveByteValue() {
WithByte object = new WithByte(0, (byte) 3);
OutboundRow row = new OutboundRow();
converter.write(object, row);
OutboundRowAssert.assertThat(row).containsColumnWithValue("state", (byte) 3);
}
@Value
private static class BooleanMapping {
Integer id;
boolean flag1;
boolean flag2;
}
@Value
private static class WithByte {
Integer id;
byte state;
}
}