-
Couldn't load subscription status.
- Fork 375
Consider PGobject as simple type
#1008
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
...test/java/org/springframework/data/jdbc/core/dialect/PostgresDialectIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| package org.springframework.data.jdbc.core.dialect; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.EnabledIfSystemProperty; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.postgresql.util.PGobject; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.*; | ||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.convert.CustomConversions; | ||
| import org.springframework.data.convert.ReadingConverter; | ||
| import org.springframework.data.convert.WritingConverter; | ||
| import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; | ||
| import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes; | ||
| import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; | ||
| import org.springframework.data.jdbc.testing.TestConfiguration; | ||
| import org.springframework.data.mapping.model.SimpleTypeHolder; | ||
| import org.springframework.data.relational.core.dialect.Dialect; | ||
| import org.springframework.data.relational.core.mapping.Table; | ||
| import org.springframework.data.repository.CrudRepository; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Tests for PostgreSQL Dialect. | ||
| * Start this test with -Dspring.profiles.active=postgres | ||
| * | ||
| * @author Nikita Konev | ||
| */ | ||
| @EnabledIfSystemProperty(named = "spring.profiles.active", matches = "postgres") | ||
| @ContextConfiguration | ||
| @Transactional | ||
| @ExtendWith(SpringExtension.class) | ||
| public class PostgresDialectIntegrationTests { | ||
|
|
||
| private static final ByteArrayOutputStream capturedOutContent = new ByteArrayOutputStream(); | ||
| private static PrintStream previousOutput; | ||
|
|
||
| @Profile("postgres") | ||
| @Configuration | ||
| @Import(TestConfiguration.class) | ||
| @EnableJdbcRepositories(considerNestedRepositories = true, | ||
| includeFilters = @ComponentScan.Filter(value = CustomerRepository.class, type = FilterType.ASSIGNABLE_TYPE)) | ||
| static class Config { | ||
|
|
||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| @Bean | ||
| Class<?> testClass() { | ||
| return PostgresDialectIntegrationTests.class; | ||
| } | ||
|
|
||
| @WritingConverter | ||
| static class PersonDataWritingConverter extends AbstractPostgresJsonWritingConverter<PersonData> { | ||
|
|
||
| public PersonDataWritingConverter(ObjectMapper objectMapper) { | ||
| super(objectMapper, true); | ||
| } | ||
| } | ||
|
|
||
| @ReadingConverter | ||
| static class PersonDataReadingConverter extends AbstractPostgresJsonReadingConverter<PersonData> { | ||
| public PersonDataReadingConverter(ObjectMapper objectMapper) { | ||
| super(objectMapper, PersonData.class); | ||
| } | ||
| } | ||
|
|
||
| @WritingConverter | ||
| static class SessionDataWritingConverter extends AbstractPostgresJsonWritingConverter<SessionData> { | ||
| public SessionDataWritingConverter(ObjectMapper objectMapper) { | ||
| super(objectMapper, true); | ||
| } | ||
| } | ||
|
|
||
| @ReadingConverter | ||
| static class SessionDataReadingConverter extends AbstractPostgresJsonReadingConverter<SessionData> { | ||
| public SessionDataReadingConverter(ObjectMapper objectMapper) { | ||
| super(objectMapper, SessionData.class); | ||
| } | ||
| } | ||
|
|
||
| private List<Object> storeConverters(Dialect dialect) { | ||
|
|
||
| List<Object> converters = new ArrayList<>(); | ||
| converters.addAll(dialect.getConverters()); | ||
| converters.addAll(JdbcCustomConversions.storeConverters()); | ||
| return converters; | ||
| } | ||
|
|
||
| protected List<?> userConverters() { | ||
| final List<Converter> list = new ArrayList<>(); | ||
| list.add(new PersonDataWritingConverter(objectMapper)); | ||
| list.add(new PersonDataReadingConverter(objectMapper)); | ||
| list.add(new SessionDataWritingConverter(objectMapper)); | ||
| list.add(new SessionDataReadingConverter(objectMapper)); | ||
| return list; | ||
| } | ||
|
|
||
| @Primary | ||
| @Bean | ||
| CustomConversions jdbcCustomConversions(Dialect dialect) { | ||
| SimpleTypeHolder simpleTypeHolder = new SimpleTypeHolder(dialect.simpleTypes(), JdbcSimpleTypes.HOLDER); | ||
|
|
||
| return new JdbcCustomConversions(CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(dialect)), | ||
| userConverters()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @BeforeAll | ||
| public static void ba() { | ||
| previousOutput = System.out; | ||
| System.setOut(new PrintStream(capturedOutContent)); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void aa() { | ||
| System.setOut(previousOutput); | ||
| previousOutput = null; | ||
| } | ||
|
|
||
| /** | ||
| * An abstract class for building your own converter for PostgerSQL's JSON[b]. | ||
| */ | ||
| static class AbstractPostgresJsonReadingConverter<T> implements Converter<PGobject, T> { | ||
| private final ObjectMapper objectMapper; | ||
| private final Class<T> valueType; | ||
|
|
||
| public AbstractPostgresJsonReadingConverter(ObjectMapper objectMapper, Class<T> valueType) { | ||
| this.objectMapper = objectMapper; | ||
| this.valueType = valueType; | ||
| } | ||
|
|
||
| @Override | ||
| public T convert(PGobject pgObject) { | ||
| try { | ||
| final String source = pgObject.getValue(); | ||
| return objectMapper.readValue(source, valueType); | ||
| } catch (JsonProcessingException e) { | ||
| throw new RuntimeException("Unable to deserialize to json " + pgObject, e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An abstract class for building your own converter for PostgerSQL's JSON[b]. | ||
| */ | ||
| static class AbstractPostgresJsonWritingConverter<T> implements Converter<T, PGobject> { | ||
| private final ObjectMapper objectMapper; | ||
| private final boolean jsonb; | ||
|
|
||
| public AbstractPostgresJsonWritingConverter(ObjectMapper objectMapper, boolean jsonb) { | ||
| this.objectMapper = objectMapper; | ||
| this.jsonb = jsonb; | ||
| } | ||
|
|
||
| @Override | ||
| public PGobject convert(T source) { | ||
| try { | ||
| final PGobject pGobject = new PGobject(); | ||
| pGobject.setType(jsonb ? "jsonb" : "json"); | ||
| pGobject.setValue(objectMapper.writeValueAsString(source)); | ||
| return pGobject; | ||
| } catch (JsonProcessingException | SQLException e) { | ||
| throw new RuntimeException("Unable to serialize to json " + source, e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| @Table("customers") | ||
| public static class Customer { | ||
|
|
||
| @Id | ||
| private Long id; | ||
| private String name; | ||
| private PersonData personData; | ||
| private SessionData sessionData; | ||
| } | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public static class PersonData { | ||
| private int age; | ||
| private String petName; | ||
| } | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public static class SessionData { | ||
| private String token; | ||
| private Long ttl; | ||
| } | ||
|
|
||
| interface CustomerRepository extends CrudRepository<Customer, Long> { | ||
|
|
||
| } | ||
|
|
||
| @Autowired | ||
| CustomerRepository customerRepository; | ||
|
|
||
| @Test | ||
| void testWarningShouldNotBeShown() { | ||
| final Customer saved = customerRepository.save(new Customer(null, "Adam Smith", new PersonData(30, "Casper"), null)); | ||
| assertThat(saved.getId()).isNotZero(); | ||
| final Optional<Customer> byId = customerRepository.findById(saved.getId()); | ||
| assertThat(byId.isPresent()).isTrue(); | ||
| final Customer foundCustomer = byId.get(); | ||
| assertThat(foundCustomer.getName()).isEqualTo("Adam Smith"); | ||
| assertThat(foundCustomer.getPersonData()).isNotNull(); | ||
| assertThat(foundCustomer.getPersonData().getAge()).isEqualTo(30); | ||
| assertThat(foundCustomer.getPersonData().getPetName()).isEqualTo("Casper"); | ||
| assertThat(foundCustomer.getSessionData()).isNull(); | ||
|
|
||
| assertThat(capturedOutContent.toString()).doesNotContain("although it doesn't convert from a store-supported type"); | ||
| } | ||
|
|
||
| } |
8 changes: 8 additions & 0 deletions
8
...s/org.springframework.data.jdbc.core.dialect/PostgresDialectIntegrationTests-postgres.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| DROP TABLE customers; | ||
|
|
||
| CREATE TABLE customers ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| name TEXT NOT NULL, | ||
| person_data JSONB, | ||
| session_data JSONB | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.