Affects: \<6.0.13>
Use Case
I'm using the method org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler#createProblemDetail()
to create ProblemDetails from my ResponseEntityExceptionHandler.
bellow an example of usage I'm making out of this method:
createProblemDetail(ex, status, "My message", null, null, exchange)
.
Expectation
Create a problem detail using the provided detail message, since I did not provide a detailMessageCode, however my default message is being overridden by updateAndGetBody
used inside createProblemDetail
.
I find it a little bit surprising that my default detail is being ignored especially since I provided null detailMessageCode and made my intention clair about what I want it to do.
I would love to hear what you think about this design and maybe clarify how can the defaultDetail be used if it will always be replaced by the org.springframework.context.MessageSource#getMessage(java.lang.String, java.lang.Object[], java.lang.String, java.util.Locale)
method.
Thank you !
Comment From: snicoll
Thanks for the report.
Create a problem detail using the provided detail message
This isn't the detail message to use, but rather the "default" detail message. MessageSource#getMessage
can return null
in which case the default you've specified is used. And not specifying a code doesn't mean either that no code should be used since this is a mandatory bit of the problem detail.
You haven't shared what the code is or how the message source is configured so we can't say for sure if that's a problem on our end. Sharing a small sample you've created from start.spring.io would be a good way to make sure we're on the same page. You can attach a zip here or push the code to a separate github repo.
Comment From: rstoyanchev
Indeed, this is all intended to work as you expect. Note however that updateAndGetBody
will only replace the "detail" field if the MessageSource
lookup finds returns a non-null value. So there must be some resource bundle property that matches the default code for the exception type.
Comment From: NaitYoussef
Hello, thank you for your quick reply @snicoll @rstoyanchev.
@snicoll I will upload a simple to my git repo and share the link with you.
I'm using in my application ResourceBundleMessageSource
, this message source is used when I call updateAndGetBody with the default detail message code org.springframework.web.ErrorResponse#getDefaultDetailMessageCode
since I did not provide one.
it's this part that I find a little bit confusing, I passed null detailMessageCode to org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler#createProblemDetail()
so that it won't resolve any detail message code and only use the description I provided, but from what I noticed it will however try the default detail message code and use it if resolved though the messageSource before it fallbacks to the defaultDefailt I provided.
Comment From: snicoll
I passed null detailMessageCode to org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler#createProblemDetail() so that it won't resolve any detail message code
Again, the code is mandatory in ProblemDetail
so passing a null code means that you let the default message code kicks in. It looks like to me that both your comments here could have been clarified by reading the Javadoc of the method. The javadoc for the detailMessageCode
states:
the code to use to look up the "detail" field through a MessageSource, falling back on ErrorResponse.getDefaultDetailMessageCode(Class, String)
The javadoc of the defaultDetail
states:
default value for the "detail" field
From that perspective, I fail to see how you expect setting a default detail with a null code will lead to your default to be used. It looks like to me that having a code in your message bundle with the detail that you want and specifying the code rather than null
is what you should be doing. This would also externalize (and potentially internationalize) the detail.
Now that you've clarified what you think is confusing, I don't think we need the sample and I hope the above addresses your concern. If they don't, then please provide the sample and we can reconsider.
Comment From: NaitYoussef
Thank you for your reply.