Skip to content

Commit 81ea35c

Browse files
committed
Update method names in FragmentsRendering
Closes gh-33974
1 parent 186f909 commit 81ea35c

File tree

11 files changed

+154
-36
lines changed

11 files changed

+154
-36
lines changed

framework-docs/modules/ROOT/pages/web/webflux-view.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ Java::
466466
----
467467
@GetMapping
468468
FragmentsRendering handle() {
469-
return FragmentsRendering.with("posts").fragment("comments").build();
469+
return FragmentsRendering.fragment("posts").fragment("comments").build();
470470
}
471471
----
472472
@@ -476,7 +476,7 @@ Kotlin::
476476
----
477477
@GetMapping
478478
fun handle(): FragmentsRendering {
479-
return FragmentsRendering.with("posts").fragment("comments").build()
479+
return FragmentsRendering.fragment("posts").fragment("comments").build()
480480
}
481481
----
482482
======

framework-docs/modules/ROOT/pages/web/webmvc-view/mvc-fragments.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Java::
4848
----
4949
@GetMapping
5050
FragmentsRendering handle() {
51-
return FragmentsRendering.with("posts").fragment("comments").build();
51+
return FragmentsRendering.fragment("posts").fragment("comments").build();
5252
}
5353
----
5454
@@ -58,7 +58,7 @@ Kotlin::
5858
----
5959
@GetMapping
6060
fun handle(): FragmentsRendering {
61-
return FragmentsRendering.with("posts").fragment("comments").build()
61+
return FragmentsRendering.fragment("posts").fragment("comments").build()
6262
}
6363
----
6464
======

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultFragmentsRenderingBuilder.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class DefaultFragmentsRenderingBuilder implements FragmentsRendering.Builder {
4949
@Nullable
5050
private HttpHeaders headers;
5151

52-
DefaultFragmentsRenderingBuilder(Collection<Fragment> fragments) {
53-
this.fragmentsCollection = new ArrayList<>(fragments);
52+
DefaultFragmentsRenderingBuilder() {
53+
this.fragmentsCollection = null;
5454
this.fragmentsFlux = null;
5555
}
5656

@@ -85,13 +85,13 @@ private HttpHeaders initHeaders() {
8585
}
8686

8787
@Override
88-
public FragmentsRendering.Builder fragment(String viewName, Map<String, Object> model) {
89-
return fragment(Fragment.create(viewName, model));
88+
public FragmentsRendering.Builder fragment(String viewName) {
89+
return fragment(Fragment.create(viewName));
9090
}
9191

9292
@Override
93-
public FragmentsRendering.Builder fragment(String viewName) {
94-
return fragment(Fragment.create(viewName));
93+
public FragmentsRendering.Builder fragment(String viewName, Map<String, Object> model) {
94+
return fragment(Fragment.create(viewName, model));
9595
}
9696

9797
@Override
@@ -100,6 +100,12 @@ public FragmentsRendering.Builder fragment(Fragment fragment) {
100100
return this;
101101
}
102102

103+
@Override
104+
public FragmentsRendering.Builder fragments(Collection<Fragment> fragments) {
105+
initFragmentsCollection().addAll(fragments);
106+
return this;
107+
}
108+
103109
private Collection<Fragment> initFragmentsCollection() {
104110
if (this.fragmentsCollection == null) {
105111
this.fragmentsCollection = new ArrayList<>();

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/FragmentsRendering.java

+86-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.web.reactive.result.view;
1818

1919
import java.util.Collection;
20-
import java.util.List;
2120
import java.util.Map;
2221
import java.util.function.Consumer;
2322

@@ -65,60 +64,122 @@ public interface FragmentsRendering {
6564
Flux<Fragment> fragments();
6665

6766

67+
/**
68+
* Create a builder with one HTML fragment, also inheriting attributes from
69+
* the shared model for the request.
70+
* @param viewName the name of the view for the fragment
71+
* @return this builder
72+
* @since 6.2.1
73+
*/
74+
static Builder fragment(String viewName) {
75+
return new DefaultFragmentsRenderingBuilder().fragment(viewName);
76+
}
77+
6878
/**
6979
* Create a builder with one HTML fragment.
7080
* @param viewName the view name for the fragment
7181
* @param model attributes for the fragment, in addition to attributes from the
7282
* shared model for the request
7383
* @return this builder
84+
* @since 6.2.1
7485
*/
75-
static Builder with(String viewName, Map<String, Object> model) {
76-
return withCollection(List.of(Fragment.create(viewName, model)));
86+
static Builder fragment(String viewName, Map<String, Object> model) {
87+
return new DefaultFragmentsRenderingBuilder().fragment(viewName, model);
88+
}
89+
90+
/**
91+
* Create a builder with multiple HTML fragments.
92+
* @param fragments the fragments to add; each fragment also inherits
93+
* attributes from the shared model for the request
94+
* @return the created builder
95+
* @since 6.2.1
96+
*/
97+
static Builder fragments(Collection<Fragment> fragments) {
98+
return new DefaultFragmentsRenderingBuilder().fragments(fragments);
99+
}
100+
101+
/**
102+
* Create a builder with a {@link Publisher} of fragments.
103+
* @param fragmentsPublisher the fragments to add; each fragment also
104+
* inherits model attributes from the shared model for the request
105+
* @return the created builder
106+
* @since 6.2.1
107+
*/
108+
static <P extends Publisher<Fragment>> Builder fragmentsPublisher(P fragmentsPublisher) {
109+
return new DefaultFragmentsRenderingBuilder(fragmentsPublisher);
110+
}
111+
112+
/**
113+
* Variant of {@link #fragmentsPublisher(Publisher)} that allows using any
114+
* producer that can be resolved to {@link Publisher} via
115+
* {@link ReactiveAdapterRegistry}.
116+
* @since 6.2.1
117+
*/
118+
static Builder fragmentsProducer(Object fragmentsProducer) {
119+
ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(fragmentsProducer.getClass());
120+
Assert.isTrue(adapter != null, "Unknown producer " + fragmentsProducer.getClass());
121+
Publisher<Fragment> publisher = adapter.toPublisher(fragmentsProducer);
122+
return fragmentsPublisher(publisher);
77123
}
78124

79125
/**
80126
* Create a builder with one HTML fragment, also inheriting attributes from
81127
* the shared model for the request.
82128
* @param viewName the name of the view for the fragment
83129
* @return this builder
130+
* @deprecated in favor of {@link #fragment(String)}
84131
*/
132+
@Deprecated(since = "6.2.1", forRemoval = true)
85133
static Builder with(String viewName) {
86-
return withCollection(List.of(Fragment.create(viewName)));
134+
return fragment(viewName);
135+
}
136+
137+
/**
138+
* Create a builder with one HTML fragment.
139+
* @param viewName the view name for the fragment
140+
* @param model attributes for the fragment, in addition to attributes from the
141+
* shared model for the request
142+
* @return this builder
143+
* @deprecated in favor of {@link #fragment(String, Map)}
144+
*/
145+
@Deprecated(since = "6.2.1", forRemoval = true)
146+
static Builder with(String viewName, Map<String, Object> model) {
147+
return fragment(viewName, model);
87148
}
88149

89150
/**
90151
* Create a builder with multiple HTML fragments.
91152
* @param fragments the fragments to add; each fragment also inherits
92153
* attributes from the shared model for the request
93154
* @return the created builder
155+
* @deprecated in favor of {@link #fragments(Collection)}
94156
*/
157+
@Deprecated(since = "6.2.1", forRemoval = true)
95158
static Builder withCollection(Collection<Fragment> fragments) {
96-
return new DefaultFragmentsRenderingBuilder(fragments);
159+
return fragments(fragments);
97160
}
98161

99162
/**
100163
* Create a builder with a {@link Publisher} of fragments.
101164
* @param fragmentsPublisher the fragments to add; each fragment also
102165
* inherits model attributes from the shared model for the request
103166
* @return the created builder
167+
* @deprecated in favor of {@link #fragmentsPublisher(Publisher)}
104168
*/
169+
@Deprecated(since = "6.2.1", forRemoval = true)
105170
static <P extends Publisher<Fragment>> Builder withPublisher(P fragmentsPublisher) {
106-
return new DefaultFragmentsRenderingBuilder(fragmentsPublisher);
171+
return fragmentsPublisher(fragmentsPublisher);
107172
}
108173

109174
/**
110-
* Variant of {@link #withPublisher(Publisher)} that allows using any
175+
* Variant of {@link #fragmentsPublisher(Publisher)} that allows using any
111176
* producer that can be resolved to {@link Publisher} via
112177
* {@link ReactiveAdapterRegistry}.
178+
* @deprecated in favor of {@link #fragmentsProducer(Object)}
113179
*/
180+
@Deprecated(since = "6.2.1", forRemoval = true)
114181
static Builder withProducer(Object fragmentsProducer) {
115-
return new DefaultFragmentsRenderingBuilder(adaptProducer(fragmentsProducer));
116-
}
117-
118-
private static Publisher<Fragment> adaptProducer(Object producer) {
119-
ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
120-
Assert.isTrue(adapter != null, "Unknown producer " + producer.getClass());
121-
return adapter.toPublisher(producer);
182+
return fragmentsProducer(fragmentsProducer);
122183
}
123184

124185

@@ -169,11 +230,21 @@ interface Builder {
169230

170231
/**
171232
* Add an HTML fragment.
172-
* @param fragment the fragment to add
233+
* @param fragment the fragment to add; the fragment also inherits
234+
* attributes from the shared model for the request
173235
* @return this builder
174236
*/
175237
Builder fragment(Fragment fragment);
176238

239+
/**
240+
* Add HTML fragments.
241+
* @param fragments the fragments to add; each fragment also inherits
242+
* attributes from the shared model for the request
243+
* @return this builder
244+
* @since 6.2.1
245+
*/
246+
Builder fragments(Collection<Fragment> fragments);
247+
177248
/**
178249
* Build the {@link FragmentsRendering} instance.
179250
*/

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result)
208208
if (adapter != null) {
209209
if (adapter.isMultiValue()) {
210210
valueMono = (result.getReturnValue() != null ?
211-
Mono.just(FragmentsRendering.withPublisher(adapter.toPublisher(result.getReturnValue())).build()) :
211+
Mono.just(FragmentsRendering.fragmentsPublisher(adapter.toPublisher(result.getReturnValue())).build()) :
212212
Mono.empty());
213213

214214
valueType = ResolvableType.forClass(FragmentsRendering.class);
@@ -242,7 +242,7 @@ public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result)
242242
}
243243

244244
if (Collection.class.isAssignableFrom(clazz)) {
245-
returnValue = FragmentsRendering.withCollection((Collection<Fragment>) returnValue).build();
245+
returnValue = FragmentsRendering.fragments((Collection<Fragment>) returnValue).build();
246246
clazz = FragmentsRendering.class;
247247
}
248248

spring-webflux/src/test/java/org/springframework/web/reactive/result/view/FragmentViewResolutionResultHandlerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public class FragmentViewResolutionResultHandlerTests {
6464
static Stream<Arguments> arguments() {
6565
Flux<Fragment> fragmentFlux = Flux.just(fragment1, fragment2).subscribeOn(Schedulers.boundedElastic());
6666
return Stream.of(
67-
Arguments.of(FragmentsRendering.withPublisher(fragmentFlux).build(),
67+
Arguments.of(FragmentsRendering.fragmentsPublisher(fragmentFlux).build(),
6868
on(Handler.class).resolveReturnType(FragmentsRendering.class)),
69-
Arguments.of(FragmentsRendering.withCollection(List.of(fragment1, fragment2)).build(),
69+
Arguments.of(FragmentsRendering.fragments(List.of(fragment1, fragment2)).build(),
7070
on(Handler.class).resolveReturnType(FragmentsRendering.class)),
7171
Arguments.of(fragmentFlux,
7272
on(Handler.class).resolveReturnType(Flux.class, Fragment.class)),

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
9292
}
9393

9494
if (returnValue instanceof Collection<?> mavs) {
95-
returnValue = FragmentsRendering.with((Collection<ModelAndView>) mavs).build();
95+
returnValue = FragmentsRendering.fragments((Collection<ModelAndView>) mavs).build();
9696
}
9797

9898
if (returnValue instanceof FragmentsRendering rendering) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public void handle(ModelAndView modelAndView) throws IOException {
393393
FragmentHttpServletResponse fragmentResponse =
394394
new FragmentHttpServletResponse(this.response, this.charset);
395395

396-
FragmentsRendering render = FragmentsRendering.with(List.of(modelAndView)).build();
396+
FragmentsRendering render = FragmentsRendering.fragments(List.of(modelAndView)).build();
397397
render.resolveNestedViews(this::resolveViewName, this.locale);
398398
render.render(modelAndView.getModel(), this.request, fragmentResponse);
399399

spring-webmvc/src/main/java/org/springframework/web/servlet/view/FragmentsRendering.java

+43-3
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,75 @@ public interface FragmentsRendering extends SmartView {
5454
HttpHeaders headers();
5555

5656

57+
/**
58+
* Create a builder with one HTML fragment, also inheriting attributes from
59+
* the shared model for the request.
60+
* @param viewName the name of the view for the fragment
61+
* @return the created builder
62+
* @since 6.2.1
63+
*/
64+
static Builder fragment(String viewName) {
65+
return new DefaultFragmentsRenderingBuilder().fragment(viewName);
66+
}
67+
5768
/**
5869
* Create a builder with one HTML fragment.
5970
* @param viewName the view name for the fragment
6071
* @param model attributes for the fragment, in addition to attributes from the
6172
* shared model for the request
6273
* @return the created builder
74+
* @since 6.2.1
6375
*/
64-
static Builder with(String viewName, Map<String, Object> model) {
76+
static Builder fragment(String viewName, Map<String, Object> model) {
6577
return new DefaultFragmentsRenderingBuilder().fragment(viewName, model);
6678
}
6779

80+
/**
81+
* Create a builder with multiple HTML fragments.
82+
* @param fragments the fragments to add; each fragment also inherits
83+
* attributes from the shared model for the request
84+
* @return the created builder
85+
* @since 6.2.1
86+
*/
87+
static Builder fragments(Collection<ModelAndView> fragments) {
88+
return new DefaultFragmentsRenderingBuilder().fragments(fragments);
89+
}
90+
6891
/**
6992
* Create a builder with one HTML fragment, also inheriting attributes from
7093
* the shared model for the request.
7194
* @param viewName the name of the view for the fragment
7295
* @return the created builder
96+
* @deprecated in favor of {@link #fragment(String)}
7397
*/
98+
@Deprecated(since = "6.2.1", forRemoval = true)
7499
static Builder with(String viewName) {
75-
return new DefaultFragmentsRenderingBuilder().fragment(viewName);
100+
return fragment(viewName);
101+
}
102+
103+
/**
104+
* Create a builder with one HTML fragment.
105+
* @param viewName the view name for the fragment
106+
* @param model attributes for the fragment, in addition to attributes from the
107+
* shared model for the request
108+
* @return the created builder
109+
* @deprecated in favor of {@link #fragment(String, Map)}
110+
*/
111+
@Deprecated(since = "6.2.1", forRemoval = true)
112+
static Builder with(String viewName, Map<String, Object> model) {
113+
return fragment(viewName, model);
76114
}
77115

78116
/**
79117
* Create a builder with multiple HTML fragments.
80118
* @param fragments the fragments to add; each fragment also inherits
81119
* attributes from the shared model for the request
82120
* @return the created builder
121+
* @deprecated in favor of {@link #fragments(Collection)}
83122
*/
123+
@Deprecated(since = "6.2.1", forRemoval = true)
84124
static Builder with(Collection<ModelAndView> fragments) {
85-
return new DefaultFragmentsRenderingBuilder().fragments(fragments);
125+
return fragments(fragments);
86126
}
87127

88128

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void handleViewInstance() throws Exception {
9191

9292
@Test
9393
void handleFragmentsRendering() throws Exception {
94-
FragmentsRendering rendering = FragmentsRendering.with("viewName").build();
94+
FragmentsRendering rendering = FragmentsRendering.fragment("viewName").build();
9595

9696
handler.handleReturnValue(rendering, returnParamModelAndView, mavContainer, webRequest);
9797
assertThat(mavContainer.getView()).isInstanceOf(SmartView.class);

spring-webmvc/src/test/java/org/springframework/web/servlet/view/DefaultFragmentsRenderingTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void render() throws Exception {
5656
MockHttpServletRequest request = new MockHttpServletRequest();
5757
MockHttpServletResponse response = new MockHttpServletResponse();
5858

59-
FragmentsRendering view = FragmentsRendering.with("fragment1", Map.of("foo", "Foo"))
59+
FragmentsRendering view = FragmentsRendering
60+
.fragment("fragment1", Map.of("foo", "Foo"))
6061
.fragment("fragment2", Map.of("bar", "Bar"))
6162
.header("headerName", "headerValue")
6263
.build();

0 commit comments

Comments
 (0)