-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Qute Character Escapes for Json Content #43369
Comments
/cc @mkouba (qute) |
Escaping is implemented as a However, if you need more context (string in an attribute value) you can't apply the escaping globally...
I'm not quite sure this should be a core functionality.
If you want to apply the default escaping to json files then just add |
Let me try to explain better. |
Yes, I do understand your example ;-) My point is that you probably can't apply a global escaping to a JSON template because you need some "context" in order to apply the correct escaping. For example, in a template like So a template extension that would escape quotes is IMO the way to go:
|
This is the workaround I've mentioned in the issue description. But, What I'm specting is to have something similar to the approach applied for XML, I mean https://quarkus.io/guides/qute-reference#character-escapes where the variable content is automatically escaped in case we're in a XML/HTML template, so if you use it in this, portion of template |
I think that I understand your use case but the question is: should we apply escaping for the whole JSON template? If so, what would be the rules? For XML/HTML it's simple. For JSON I'm not so sure... |
I'm not sure to understand what do you mean when talking about the "whole JSON template", if you mean in any instance of variables to render in a template, my answer will be yes, as users will be able to apply |
In that case, somethig like this should work: public class JsonEscaper implements ResultMapper {
@Override
public boolean appliesTo(Origin origin, Object result) {
if (result instanceof RawString) {
return false;
}
Optional<Variant> variant = origin.getVariant();
if (variant.isPresent()) {
return variant.getContentType().startsWith("application/json");
}
return false;
}
@Override
public String map(Object result, Expression expression) {
return StringEscapeUtils.html.escapeJson(result.toString());
} and then register the mapper: class MyBean {
void configureEngine(@Observes EngineBuilder builder) {
builder.addResultMapper(new JsonEscaper());
}
} I don't think we want to add Feel free to send a pull request ;-). |
I've found a easier implementation for encoding in other library that can fit in the way you've mentioned, may be something like this:
One more question, will this mapper be included in the default engine builder like And finally, I think there is an error in the code you've proposed |
Looks good.
A perf tip - if possible use the initial capacity for the
It depends. I mean if you register it with
👍 |
May be easy for padLeft() use ->
Avoid |
Description
It'll nice to have the same behavior for Json template for character escapes like the one we have for XML content explained here https://quarkus.io/guides/qute-reference#character-escapes allowing users to escape content we want to include, for example, as string in an attribute value, for those values that contain quotes, new line, ... characters.
Despite there is a workaround by creating a @TemplateExtension or @templatedata to access encode static functions available in Apache Commons Texts and others, but I think this must be a core functionality.
Implementation ideas
Use the same approach for escaping XML content, may be including in quarkus.qute.escape-content-types property "application/json" joined to the characters escape policy.
Other easy approach will be include in "Built-in Template Extensions" one for Strings, allowing encoding and unencoding options.
The text was updated successfully, but these errors were encountered: