spring-projects / spring-projects/spring-data-rest

Spring Data Rest using Jackson snake_case naming strategy [DATAREST-1361]

Open
#1,724 2 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

hypr2771 opened DATAREST-1361 and commented

What

Using :

  • spring.jackson.property-naming-strategy: SNAKE_CASE
  • spring-boot-starter-data-rest

I run into a

`

JSON parse error: Cannot construct instance of net.bugreport.domain.entities.SomethingCamelCase (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('/something_camel_cases/1'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of net.bugreport.domain.entities.SomethingCamelCase (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('/something_camel_cases/1'){quote`}

What I expect to be able to do the following :

{{POST /something_containing_camel_cases
{
"something_camel_case": "/something_camel_cases/1"
}
}}

while having my @Entity named according to Java conventions (camelCase)

Done so far

I set up a small GitHub repository to give a minimal reproducible example : https://github.com/hypr2771/naming-strategy-issue.

I analyzed org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier#updateBuilder which is responsible for my issue :

Indeed, when getting the com.fasterxml.jackson.databind.deser.SettableBeanProperty, the name of the property is its JSON name, which is snake_case.

Whereas when looking for org.springframework.data.mapping.model.BasicPersistentEntity#getPersistentProperty, it cannot find it since in org.springframework.data.mapping.model.BasicPersistentEntity#propertyCache, the name is camelCase.

Code

As code is pretty heavy, containing pom.xml, few classes and their repositories, a test case and the @SpringBootApplication, naming strategy and a data set, I set up a GitHub repository to store it.

Repository on GitHub is available here for anyone to fork and get the error.

You simply have to run the net.bugreport.domain.repositories.SomethingContainingCamelCaseRepositoryTest#create test case


Affects: 3.1.6 (Lovelace SR6)

Reference URL: https://github.com/hypr2771/naming-strategy-issue

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.