From 8b674e44adb815c74b795d8a529f2cf02d447650 Mon Sep 17 00:00:00 2001 From: yfoelsin Date: Fri, 6 Sep 2024 15:15:29 +0200 Subject: [PATCH] Set status code on DefaultRenderingBuilder for RedirectView Fixes: #33497 --- .../reactive/result/view/DefaultRenderingBuilder.java | 3 +++ .../result/view/DefaultRenderingBuilderTests.java | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java index c1d0923e9773..4d452cd12794 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java @@ -85,6 +85,9 @@ private Model initModel() { @Override public DefaultRenderingBuilder status(HttpStatusCode status) { this.status = status; + if (this.view instanceof RedirectView) { + ((RedirectView) this.view).setStatusCode(status); + } return this; } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java index 1c33c68808f0..f96ab31a00ca 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java @@ -126,6 +126,15 @@ void redirectWithPropagateQuery() { assertThat(((RedirectView) view).isPropagateQuery()).isTrue(); } + @Test + void redirectWithCustomStatus() { + Rendering rendering = Rendering.redirectTo("foo").status(HttpStatus.MOVED_PERMANENTLY).build(); + + Object view = rendering.view(); + assertThat(view.getClass()).isEqualTo(RedirectView.class); + assertThat(((RedirectView) view).statusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY); + } + private static class Foo {}