micronaut-projects / micronaut-views

Micronaut Integration with Server Side View Rendering
Apache License 2.0
31 stars 34 forks source link

Allow ViewsRenderers to specify content type dynamically #167

Closed Verdoso closed 3 years ago

Verdoso commented 3 years ago

Hi there,

We use XSLT/XML as render technologies and as part of the process of experimenting with Micronaut, I''ve been developing a custom renderer that transforms the model object using JAXB and then applies an XSLT stylesheet to the resulting XML. So far, so good. However, one of the features of an XSLT stylesheet is that you can produce different outputs HTML/XML/text and, on top of that, we also usually make extensive use of a feature where we can visualise the XML produced by the model, usally by passing a parameter that makes the render process skip the "apply xslt" step.

However, currently there is no way from inside a ViewsRenderer implementation to notify the filter which content type it is going to produce per request. This means producing one content per view renderer and it is quite limiting.

I have seen other issues that might be related and could be solved by adding something like this (#159, #56) so I'd like to propose implementing some mechanism so the ViewsRenderer implementations can tell the ViewFilter which contentType/encoding to set in the response.

Other frameworks, you know which one ;), do so by passing the response along with the request to the view renderers, so they can set the content type directly on the response, but I understand the idea behind not letting the implemenations have that full object to mess with, so may be adding some limited object to the "render(view, model, request)" call on the filter that the implementations can use to set extra values for the response might work better without compromising the whole response object.

Does that sound reasonable? I'd like to get some feedback before trying to make any code changes, in case I'm missing something as I'm quite new to Micronaut.

Thanks.

Expected Behaviour

ViewRenderer implementations able to set the content type of the response per request

Actual Behaviour

io.micronaut.views.ViewsFilter sets the content type of the response based on the value provided statically by the view renderer implementation

Environment Information

Verdoso commented 3 years ago

It would've been nice to see some kind of feedback, but not important now anyway.