Skip to content

Commit a6687cf

Browse files
committed
Merge branch '2.0.x'
2 parents e5a763b + a63a514 commit a6687cf

File tree

7 files changed

+69
-17
lines changed

7 files changed

+69
-17
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Map.Entry;
2626
import java.util.Set;
27+
import java.util.stream.Collectors;
2728

2829
import org.springframework.http.HttpHeaders;
2930
import org.springframework.http.HttpMethod;
@@ -64,6 +65,15 @@ String getBasicAuthCredentials() {
6465
return null;
6566
}
6667

68+
Parameters getNonPartParameters() {
69+
Parameters parameters = getParameters();
70+
Parameters nonPartParameters = new Parameters();
71+
nonPartParameters.putAll(parameters);
72+
Set<String> partNames = getParts().stream().map(OperationRequestPart::getName).collect(Collectors.toSet());
73+
nonPartParameters.keySet().removeAll(partNames);
74+
return nonPartParameters;
75+
}
76+
6777
@Override
6878
public byte[] getContent() {
6979
return this.delegate.getContent();

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -177,7 +177,7 @@ private void writeContent(CliOperationRequest request, List<String> lines) {
177177
lines.add(String.format("-d '%s'", content));
178178
}
179179
else if (!request.getParts().isEmpty()) {
180-
for (Entry<String, List<String>> entry : request.getParameters().entrySet()) {
180+
for (Entry<String, List<String>> entry : request.getNonPartParameters().entrySet()) {
181181
for (String value : entry.getValue()) {
182182
lines.add(String.format("-F '%s=%s'", entry.getKey(), value));
183183
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -192,7 +192,7 @@ private void writeParametersIfNecessary(CliOperationRequest request, List<String
192192
return;
193193
}
194194
if (!request.getParts().isEmpty()) {
195-
writeContentUsingParameters(request.getParameters(), lines);
195+
writeContentUsingParameters(request.getNonPartParameters(), lines);
196196
}
197197
else if (request.isPutOrPost()) {
198198
writeContentUsingParameters(request.getParameters().getUniqueParameters(request.getUri()), lines);

spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Map.Entry;
26+
import java.util.Set;
27+
import java.util.stream.Collectors;
2628

2729
import org.springframework.http.HttpHeaders;
2830
import org.springframework.http.HttpMethod;
@@ -151,16 +153,20 @@ private boolean isPutOrPost(OperationRequest request) {
151153

152154
private void writeParts(OperationRequest request, PrintWriter writer) {
153155
writer.println();
156+
Set<String> partNames = request.getParts().stream().map(OperationRequestPart::getName)
157+
.collect(Collectors.toSet());
154158
for (Entry<String, List<String>> parameter : request.getParameters().entrySet()) {
155-
if (parameter.getValue().isEmpty()) {
156-
writePartBoundary(writer);
157-
writePart(parameter.getKey(), "", null, null, writer);
158-
}
159-
else {
160-
for (String value : parameter.getValue()) {
159+
if (!partNames.contains(parameter.getKey())) {
160+
if (parameter.getValue().isEmpty()) {
161161
writePartBoundary(writer);
162-
writePart(parameter.getKey(), value, null, null, writer);
163-
writer.println();
162+
writePart(parameter.getKey(), "", null, null, writer);
163+
}
164+
else {
165+
for (String value : parameter.getValue()) {
166+
writePartBoundary(writer);
167+
writePart(parameter.getKey(), value, null, null, writer);
168+
writer.println();
169+
}
164170
}
165171
}
166172
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -299,6 +299,17 @@ public void multipartPostWithParameters() throws IOException {
299299
assertThat(this.generatedSnippets.curlRequest()).is(codeBlock("bash").withContent(expectedContent));
300300
}
301301

302+
@Test
303+
public void multipartPostWithOverlappingPartsAndParameters() throws IOException {
304+
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/upload")
305+
.method("POST").header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
306+
.part("image", new byte[0]).submittedFileName("documents/images/example.png").and()
307+
.part("a", "apple".getBytes()).and().param("a", "apple").build());
308+
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
309+
+ "'Content-Type: multipart/form-data' -F 'image=@documents/images/example.png' -F 'a=apple'";
310+
assertThat(this.generatedSnippets.curlRequest()).is(codeBlock("bash").withContent(expectedContent));
311+
}
312+
302313
@Test
303314
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
304315
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -303,6 +303,18 @@ public void multipartPostWithParameters() throws IOException {
303303
assertThat(this.generatedSnippets.httpieRequest()).is(codeBlock("bash").withContent(expectedContent));
304304
}
305305

306+
@Test
307+
public void multipartPostWithOverlappingPartsAndParameters() throws IOException {
308+
new HttpieRequestSnippet(this.commandFormatter)
309+
.document(this.operationBuilder.request("http://localhost/upload").method("POST")
310+
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
311+
.part("image", new byte[0]).submittedFileName("documents/images/example.png").and()
312+
.part("a", "apple".getBytes()).and().param("a", "apple").build());
313+
String expectedContent = "$ http --form POST 'http://localhost/upload'"
314+
+ " 'image'@'documents/images/example.png' 'a'@<(echo 'apple')";
315+
assertThat(this.generatedSnippets.httpieRequest()).is(codeBlock("bash").withContent(expectedContent));
316+
}
317+
306318
@Test
307319
public void basicAuthCredentialsAreSuppliedUsingAuthOption() throws IOException {
308320
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")

spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -263,6 +263,19 @@ public void multipartPostWithParameters() throws IOException {
263263
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
264264
}
265265

266+
@Test
267+
public void multipartPostWithOverlappingPartsAndParameters() throws IOException {
268+
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload").method("POST")
269+
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE).param("a", "apple")
270+
.part("a", "apple".getBytes()).and().part("image", "<< data >>".getBytes()).build());
271+
String paramPart = createPart(String.format("Content-Disposition: form-data; " + "name=a%n%napple"), false);
272+
String filePart = createPart(String.format("Content-Disposition: form-data; " + "name=image%n%n<< data >>"));
273+
String expectedContent = paramPart + filePart;
274+
assertThat(this.generatedSnippets.httpRequest()).is(httpRequest(RequestMethod.POST, "/upload")
275+
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
276+
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
277+
}
278+
266279
@Test
267280
public void multipartPostWithParameterWithNoValue() throws IOException {
268281
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload").method("POST")

0 commit comments

Comments
 (0)