micronaut-projects / micronaut-views

Micronaut Integration with Server Side View Rendering
Apache License 2.0
28 stars 32 forks source link

JTE will not render a TURBO_STREAM response #776

Closed PeterFokkinga closed 2 months ago

PeterFokkinga commented 3 months ago

Expected Behavior

It should be possible to return a TURBO_STREAM response using JTE.

Actual Behaviour

No output will be generated, no exception is thrown or logged.

Steps To Reproduce

a handler like the own below would not work when "home" refers to a JTE template ie home.jte

@TurboView(value = "home")
@Produces(value = {MediaType.TEXT_HTML, TurboMediaType.TURBO_STREAM})
@Get("/home")
public Map<String, Object> home(@Nullable Authentication authentication, Locale locale) {
...
}

Environment Information

OSX / Java 21

Example Application

No response

Version

4.3.8

PeterFokkinga commented 3 months ago

The issue is that HtmlJteViewsRenderer has the annotation @Produces(MediaType.TEXT_HTML) whereas DefaultViewsRendererLocator.resolveViewsRenderer searches for a renderer that produces TurboMediaType.TURBO_STREAM

There are two possible solutions:

  1. create a TurboStreamJteViewsRenderer class that extends JteViewsRenderer and has class annotation @Produces(TurboMediaType.TURBO_STREAM)
  2. add TurboMediaType.TURBO_STREAM to the annotation of HtmlJteViewsRenderer

I can confirm that option 1 works. (I created such a class in my own application)

Option 2 would also require that the resolveViewsRenderer method supports a @Produces annotation with multiple values as it doesn't at the moment ie replace in DefaultViewsRendererLocator line 91 and further with

if (annotation == null) {
    return true;
} else if (annotation.getValue(String[].class).isPresent()) {
    return Arrays.asList(annotation.getValue(String[].class).get())
        .contains(contentType);
} else if (annotation.getValue(String.class).isPresent()) {
    return annotation.getValue(String.class).get().equals(contentType);
}
return false;

I'm not sure which of the two would be preferable.

sdelamo commented 3 months ago

Problably, option 2. Would you be willing to contribute a PR @PeterFokkinga ?

PeterFokkinga commented 3 months ago

Yes, I'll see if I can add a test to verify the fix (I'm new to Spock).

PeterFokkinga commented 3 months ago

that turned out to be rather easy 😊 test fails on the 5.3.x branch but is successful with the fix in place

sdelamo commented 2 months ago

closed via https://github.com/micronaut-projects/micronaut-views/pull/777