Skip to content

Add "data:" prefix for multi-line SSE data field with Jackson - #1251

Merged
sdeleuze merged 1 commit into
spring-projects:masterfrom
sdeleuze:SPR-14899
Dec 8, 2016
Merged

sdeleuze merged 1 commit into
spring-projects:masterfrom
sdeleuze:SPR-14899

Conversation

@sdeleuze

@sdeleuze sdeleuze commented Nov 30, 2016 •

Copy link
Copy Markdown
Contributor

@rstoyanchev Based on the Jackson PrettyPrinter based trick with discussed + Jackson capabilities to specify such PrettyPrinter at ObjectWriter level, I think I ended up with a pretty good solution in the sense it is not intrusive and it does not lead to performance penalty. Could you say me if that's fine for you?

Also could you confirm that we should apply that to 4.3.x branch as well?

Issue: SPR-14899

@sdeleuze
sdeleuze force-pushed the SPR-14899 branch 2 times, most recently from 317b38b to 3ba6cac Compare December 1, 2016 10:33
@rstoyanchev

rstoyanchev commented Dec 1, 2016 •

Copy link
Copy Markdown
Contributor

@sdeleuze I'm wondering why the Jackson2JsonEncoder has to check a hint from ServerSentEventHttpMessageWriter? Couldn't it work like it's done in the AbstractJackson2HttpMessageConverter by checking the content type of the response? If that hint is removed essentially the Jackson encoder and converter have independent, built-in support to pretty print SSE data correctly which is a good place to be.

@sdeleuze

sdeleuze commented Dec 2, 2016

Copy link
Copy Markdown
Contributor Author

Good point, I missed the fact that encodeValue() is a private method and that encode() has the mimeType parameter, I will change that and if you are ok with the rest, merge to master.

@rstoyanchev

Copy link
Copy Markdown
Contributor

Yes

@sdeleuze

sdeleuze commented Dec 2, 2016

Copy link
Copy Markdown
Contributor Author

Hum in fact I am afraid we need the SSE hint, because the mime type we get at the encoder level is application/json and not text/event-stream. For Spring MVC that works because we retrieve the content type of the response, but on the encoder side, we don't have access to that so the hint is the only way I think.

@rstoyanchev

Copy link
Copy Markdown
Contributor

It looks like ServerSentEventMessageWriter passes application/json when it delegates to the Jacson encoder. I'm not sure that should be so actually. It's probably okay for locating the encoder but the content type of the response is text/event-stream after all. Would it cause an issue to pass text/event-stream?

@sdeleuze

sdeleuze commented Dec 2, 2016

Copy link
Copy Markdown
Contributor Author

In that Jackson particular case, that would work but nothing prevent an encoder implementation to re-validate the content type by calling canEncode() again + I find a little bit weird to pass not the same content type to canEncode() and encode(), that's why I tend to prefer using the SSE hint since hints are designed for such use case.

@rstoyanchev

rstoyanchev commented Dec 7, 2016 •

Copy link
Copy Markdown
Contributor

One could ask then if the JacksonEncoder supports "text/event-stream" but it's more like it's the ServerSentEventMessageWriter that supports it and delegates to other encoders. So the JacksonEncoder still only knows how to render JSON but it is also aware that's happening in the context of an SSE response.

Interestingly if JacksonEncoder was in spring-core where it could be shared with non-web modules, it would be harder or not possible to do this. Having a JacksonEncoder in spring-web is paying off and it's okay to make it aware of a hint from ServerSentEventMessageWriter, which is in the codec package above.

In short I'm okay with this. I would only change the hint to be more broadly about indicating that encoding is happening in the context of an SSE response -- something like SSE_CONTENT_HINT and in the Javadoc the "data:" prefixing is just an example of what the hint can be used for.

@sdeleuze
sdeleuze merged commit 2735cba into spring-projects:master Dec 8, 2016
@sdeleuze
sdeleuze deleted the SPR-14899 branch April 28, 2021 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants