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

Custom converter in R2dbcEntityOperations breaks nested mappings #850

Closed
SledgeHammer01 opened this issue Jan 31, 2024 · 1 comment
Closed
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@SledgeHammer01
Copy link

I'm using the MySQL Sakilla DB (a sample database they provide). My resulting JSON looks like:

  {
    "filmId": 1,
    "title": "Academy Dinosaur",
    "description": "An epic drama of a feminist and a mad scientist who must battle a teacher in the canadian rockies",
    "releaseYear": 2006,
    "rating": "PG",
    "actors": [
      {
        "firstName": "Penelope",
        "lastName": "Guiness"
      }
    ]
  }

I'm connecting to multiple DBs, so I use R2dbcEntityOperations. I need to do some mapping on the rating field, so I added a custom converter. Now the resulting JSON is:

  {
    "filmId": 1,
    "title": "Academy Dinosaur",
    "description": "An epic drama of a feminist and a mad scientist who must battle a teacher in the canadian rockies",
    "releaseYear": 2006,
    "rating": "PG",
    "actors": []
  }

My code looks like this:

  @Bean
  public R2dbcEntityOperations filmsEntityOperations(ConnectionFactory filmsConnectionFactory) {
    DatabaseClient databaseClient =
        DatabaseClient.builder().connectionFactory(filmsConnectionFactory).build();

    MappingR2dbcConverter customMappingConverter = new MappingR2dbcConverter(r2dbcMappingContext(), myr2dbcCustomConversions());

    return new R2dbcEntityTemplate(databaseClient, MySqlDialect.INSTANCE, customMappingConverter);
  }

  private R2dbcMappingContext r2dbcMappingContext() {
    R2dbcCustomConversions r2dbcCustomConversions = myr2dbcCustomConversions();
    R2dbcMappingContext context = new R2dbcMappingContext(DefaultNamingStrategy.INSTANCE);
    context.setSimpleTypeHolder(r2dbcCustomConversions.getSimpleTypeHolder());
    return context;
  }

  private R2dbcCustomConversions myr2dbcCustomConversions() {
    Collection<?> converters = Arrays.asList(new RatingConverter2(), new RatingConverter());
    R2dbcCustomConversions conversions = R2dbcCustomConversions.of(MySqlDialect.INSTANCE,converters);
    return conversions;
  }

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 31, 2024
@SankalpWayne
Copy link

@SledgeHammer01 how did u resolve the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants