-
Notifications
You must be signed in to change notification settings - Fork 67
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
Fail to set content on nested content properties when using JPA. #1480
Comments
Thanks @vierbergenlars . I see what you are asking for. I think I would have to make it configurable so I need to look into how best to do that (given its depth in the architecture). Just curious on your opinion in general. I think this can be easily solved in the application later with an
Would that work for you all? Or are there reasons/justifications why that wouldn't work (well) beyond keeping the app model simple? |
Putting some code in Although it's perhaps a small detail, the response provided by spring-data-rest would also be impacted, instead of looking like I would expect, we will have an object with all null fields when there is no content.
You might consider it a detail, but this makes it more difficult for API users to check if there is content. Adding some code to every entity that uses content is certainly possible, but to me it feels like additional effort that should not be needed to be able to use spring-content.
If taking the second approach ( I can't immediately think of a situation where you would want an exception while trying to read/write content. That effectively makes two paths that need to be handled for the "no content present" case, one with the read returning null, and one with it throwing an (undeclared/undocumented) exception which is an internal implementation detail. |
#1507) * feat: content property now uses setAutoGrowNestedPaths when setting content to provide better support for null embedded content objects - filesystem storage only * test: add @Embedded content tests for azure storage * test: add @Embedded content tests for gcp storage * test: add @Embedded content tests for s3 storage Fixes #1480
#1507) * feat: content property now uses setAutoGrowNestedPaths when setting content to provide better support for null embedded content objects - filesystem storage only * test: add @Embedded content tests for azure storage * test: add @Embedded content tests for gcp storage * test: add @Embedded content tests for s3 storage Fixes #1480
…content (#1510) * feat: content property now uses setAutoGrowNestedPaths when setting content to provide better support for null embedded content objects - filesystem storage only * test: add @Embedded content support/tests for azure storage * test: add @Embedded content support/tests for gcp storage * test: add @Embedded content support/tests for s3 storage * test: add @Embedded content support/tests for jpa storage Fixes #1480
Describe the bug
We're using nested content properties with JPA.
When trying to set the content via the REST API, an exception occurs because the field that contains the embedded object is
null
.Example setup
Exception
To Reproduce
Steps to reproduce the behavior:
./gradlew bootRun
demo.zipname
)pic_t
content object and try to upload a file to it (e.g.curl --upload-file gradle.properties -H "Content-Type: text/plain" http://localhost:8080/persons/830ccc3d-5131-4285-b462-2f0995e5f492/pic_t
; note that you need to change the ID)Expected behavior
I expect the content to be actually set on the object, even though the embedded object is not created yet.
Additional context
All columns defined by the
@Embeddable
Content
object are nullable in the database. If all fields arenull
, Hibernate does not create aContent
object at all for the@Embedded Content picture
field. Instead, it sets it tonull
. (https://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Nulls)The result is that, when loaded the entity looks like this
Traversing
null
with a springBeanWrapper
throws an exception by default, unlessConfigurablePropertyAccessor#setAutoGrowNestedPaths(true)
is called before accessing properties. That will automatically instanciate the object if it's there was arenull
s in the path.It looks like this issue would be resolved by using
wrapper.setAutoGrowNestedPaths(true)
everywhere inContentProperty
where aBeanWrapper
is created for an entity.A side-effect of this is that trying to read the contentId for a nested content property will also instanciate the embedded object. For JPA itself, that is not really an issue since embedded objects with all-null fields are the same as not having the embedded object at all.
However, for user code it may be an unexpected side-effect. If you want to cover this side-effect, you could catch the
org.springframework.beans.NullValueInNestedPathException
exception in the property read paths and returnnull
in that case.The text was updated successfully, but these errors were encountered: