Skip to content

Commit

Permalink
BREAKING CHANGE: del ViewsRenderer::render(String, T) (#271)
Browse files Browse the repository at this point in the history
Leave only method taking request ViewsRenderer::render(String, T, HttpRequest)
  • Loading branch information
sdelamo authored Aug 10, 2021
1 parent 3d37fcb commit 23ced61
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class NonHTMLViewRendererSpec extends Specification {
@Singleton
static class XmlViewRenderer implements ViewsRenderer<Library> {
@Override
Writable render(@NonNull String viewName, @Nullable Library data) {
Writable render(@NonNull String viewName, @Nullable Library data, @NonNull HttpRequest<?> request) {
new Writable() {
@Override
void writeTo(Writer out) throws IOException {
Expand All @@ -114,7 +114,7 @@ class NonHTMLViewRendererSpec extends Specification {
// this renderer should not be used because it specifies a different type

@Override
Writable render(@NonNull String viewName, @Nullable Book data) {
Writable render(@NonNull String viewName, @Nullable Book data, @NonNull HttpRequest<?> request) {
new Writable() {
@Override
void writeTo(Writer out) throws IOException {
Expand All @@ -139,7 +139,7 @@ class NonHTMLViewRendererSpec extends Specification {
@Singleton
static class CsvViewRenderer implements ViewsRenderer<Library> {
@Override
Writable render(@NonNull String viewName, @Nullable Library data) {
Writable render(@NonNull String viewName, @Nullable Library data, @NonNull HttpRequest<?> request) {
new Writable() {
@Override
void writeTo(Writer out) throws IOException {
Expand Down
12 changes: 1 addition & 11 deletions views-core/src/main/java/io/micronaut/views/ViewsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,13 @@ public interface ViewsRenderer<T> extends Ordered {
*/
String EXTENSION_SEPARATOR = ".";

/**
* @param viewName view name to be rendered
* @param data response body to render it with a view
* @return A writable where the view will be written to.
*/
@NonNull Writable render(@NonNull String viewName, @Nullable T data);

/**
* @param viewName view name to be rendered
* @param data response body to render it with a view
* @param request HTTP request
* @return A writable where the view will be written to.
*/
default @NonNull Writable render(@NonNull String viewName, @Nullable T data,
@NonNull HttpRequest<?> request) {
return render(viewName, data);
}
@NonNull Writable render(@NonNull String viewName, @Nullable T data, @NonNull HttpRequest<?> request);

/**
* @param viewName view name to be rendered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.io.Writable;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.views.ViewUtils;
import io.micronaut.views.ViewsConfiguration;
import io.micronaut.views.ViewsRenderer;
Expand Down Expand Up @@ -66,7 +67,7 @@ public FreemarkerViewsRenderer(ViewsConfiguration viewsConfiguration,

@NonNull
@Override
public Writable render(@NonNull String viewName, @Nullable T data) {
public Writable render(@NonNull String viewName, @Nullable T data, @NonNull HttpRequest<?> request) {
ArgumentUtils.requireNonNull("viewName", viewName);
return (writer) -> {
Map<String, Object> context = modelOf(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.core.io.scan.ClassPathResourceLoader;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.views.ViewUtils;
import io.micronaut.views.ViewsConfiguration;
import io.micronaut.views.ViewsRenderer;
Expand Down Expand Up @@ -71,7 +72,7 @@ public HandlebarsViewsRenderer(ViewsConfiguration viewsConfiguration,

@NonNull
@Override
public Writable render(@NonNull String viewName, @Nullable T data) {
public Writable render(@NonNull String viewName, @Nullable T data, @NonNull HttpRequest<?> request) {
ArgumentUtils.requireNonNull("viewName", viewName);
return (writer) -> {
String location = viewLocation(viewName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.micronaut.views.pebble;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.HttpRequest;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import com.mitchellbosecke.pebble.PebbleEngine;
Expand Down Expand Up @@ -47,7 +49,7 @@ public PebbleViewsRenderer(PebbleConfiguration configuration, PebbleEngine engin
}

@Override
public Writable render(String name, T data) {
public Writable render(String name, T data, @NonNull HttpRequest<?> request) {
return (writer) -> engine.getTemplate(ViewUtils.normalizeFile(name, extension)).evaluate(writer, modelOf(data));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fizzed.rocker.BindableRockerModel;
import io.micronaut.core.io.Writable;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.views.ViewsConfiguration;
import io.micronaut.views.ViewsRenderer;
import io.micronaut.core.annotation.NonNull;
Expand Down Expand Up @@ -58,7 +59,7 @@ public RockerViewsRenderer(ViewsConfiguration viewsConfiguration,

@NonNull
@Override
public Writable render(@NonNull String view, @Nullable T data) {
public Writable render(@NonNull String view, @Nullable T data, @NonNull HttpRequest<?> request) {
ArgumentUtils.requireNonNull("view", view);

Map<String, Object> context = modelOf(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ public SoySauceViewsRenderer(ViewsConfiguration viewsConfiguration,
}
}

/**
* @param viewName view name to be rendered
* @param data response body to render it with a view
* @return A writable where the view will be written to.
*/
@NonNull
public Writable render(@NonNull String viewName, @Nullable T data) {
return render(viewName, data, null);
}

/**
* @param viewName view name to be rendered
Expand All @@ -129,7 +120,7 @@ public Map<String, SoyValueProvider> getParamsAsMap() {
}
});
renderer.setData(context);
if (injectNonce && request != null) {
if (injectNonce) {
Optional<Object> nonceObj = request.getAttribute(CspFilter.NONCE_PROPERTY);
if (nonceObj.isPresent()) {
String nonceValue = ((String) nonceObj.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ public ThymeleafViewsRenderer(AbstractConfigurableTemplateResolver templateResol
this.engine = templateEngine;
}

@NonNull
@Override
public Writable render(@NonNull String viewName, @Nullable T data) {
ArgumentUtils.requireNonNull("viewName", viewName);
return (writer) -> {
IContext context = new Context(Locale.US, modelOf(data));
render(viewName, context, writer);
};
}

@Override
@NonNull
public Writable render(@NonNull String viewName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.io.Writable;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.views.ViewUtils;
import io.micronaut.views.ViewsConfiguration;
import io.micronaut.views.ViewsRenderer;
Expand Down Expand Up @@ -70,7 +71,7 @@ public VelocityViewsRenderer(ViewsConfiguration viewsConfiguration,

@NonNull
@Override
public Writable render(@NonNull String view, @Nullable T data) {
public Writable render(@NonNull String view, @Nullable T data, @NonNull HttpRequest<?> request) {
ArgumentUtils.requireNonNull("view", view);
return (writer) -> {
Map<String, Object> context = modelOf(data);
Expand Down

0 comments on commit 23ced61

Please sign in to comment.