spring-projects / spring-projects/spring-data-rest
UriToEntityConverter does not use BackendIdConverter to convert id [DATAREST-1371]
Open
@odrotbohm is already working on this.
Since Dec 31, 2020.
type: bug
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
XhstormR opened DATAREST-1371 and commented
I use BackendIdConverter to hash the id to prevent it from being traversed,this is my HashIdConverter:
object HashIdConverter : BackendIdConverter {
override fun fromRequestId(id: String?, entityType: Class<*>) =
id?.let { HashIdFormatter.parse(it) }
override fun toRequestId(id: Serializable, entityType: Class<*>) =
HashIdFormatter.print(id.toString().toLong())
override fun supports(delimiter: Class<*>) =
BaseSequenceEntity::class.java.isAssignableFrom(delimiter)
}
This works very well.
But when I have such an entity:
class Company @JsonCreator(mode = JsonCreator.Mode.DISABLED) constructor(
@Column(nullable = false, unique = true)
var name: String,
@Column(nullable = true)
var address: String?,
@ManyToOne(fetch = FetchType.LAZY, optional = false)
var account: Account
) : BaseSequenceEntity() {
I use curl to create a company entity:
curl http://127.0.0.1:8080/api/companies -H 'Content-Type: application/json' -s -d '
{
"name": "ABCD",
"address": "ABCD",
"account": "http://127.0.0.1:8080/api/accounts/gvK03jek"
}
'
It throws an exception:
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'gvK03jek'; nested exception is java.lang.NumberFormatException: For input string: "gvK03jek"
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:289)
at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindById(CrudRepositoryInvoker.java:92)
at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindById(UnwrappingRepositoryInvokerFactory.java:94)
at org.springframework.data.rest.core.UriToEntityConverter.convert(UriToEntityConverter.java:126)
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:517)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
... 75 more
Caused by: java.lang.NumberFormatException: For input string: "gvK03jek"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Long.parseLong(Long.java:699)
at java.base/java.lang.Long.valueOf(Long.java:1151)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:214)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:62)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:49)
at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
... 84 more
I looked at the source code and it seems that UriToEntityConverter did not use the BackendIdConverter to convert the id after extracting the id from the URI:
No further details from DATAREST-1371
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.