Avoid duplicates in multipart reqs with overlapping parts and params
Fixes gh-746
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -64,6 +65,15 @@ final class CliOperationRequest implements OperationRequest {
|
||||
return null;
|
||||
}
|
||||
|
||||
Parameters getNonPartParameters() {
|
||||
Parameters parameters = getParameters();
|
||||
Parameters nonPartParameters = new Parameters();
|
||||
nonPartParameters.putAll(parameters);
|
||||
Set<String> partNames = getParts().stream().map(OperationRequestPart::getName).collect(Collectors.toSet());
|
||||
nonPartParameters.keySet().removeAll(partNames);
|
||||
return nonPartParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getContent() {
|
||||
return this.delegate.getContent();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -177,7 +177,7 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
lines.add(String.format("-d '%s'", content));
|
||||
}
|
||||
else if (!request.getParts().isEmpty()) {
|
||||
for (Entry<String, List<String>> entry : request.getParameters().entrySet()) {
|
||||
for (Entry<String, List<String>> entry : request.getNonPartParameters().entrySet()) {
|
||||
for (String value : entry.getValue()) {
|
||||
lines.add(String.format("-F '%s=%s'", entry.getKey(), value));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -192,7 +192,7 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
return;
|
||||
}
|
||||
if (!request.getParts().isEmpty()) {
|
||||
writeContentUsingParameters(request.getParameters(), lines);
|
||||
writeContentUsingParameters(request.getNonPartParameters(), lines);
|
||||
}
|
||||
else if (request.isPutOrPost()) {
|
||||
writeContentUsingParameters(request.getParameters().getUniqueParameters(request.getUri()), lines);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
* Copyright 2014-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -151,16 +153,20 @@ public class HttpRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
private void writeParts(OperationRequest request, PrintWriter writer) {
|
||||
writer.println();
|
||||
Set<String> partNames = request.getParts().stream().map(OperationRequestPart::getName)
|
||||
.collect(Collectors.toSet());
|
||||
for (Entry<String, List<String>> parameter : request.getParameters().entrySet()) {
|
||||
if (parameter.getValue().isEmpty()) {
|
||||
writePartBoundary(writer);
|
||||
writePart(parameter.getKey(), "", null, null, writer);
|
||||
}
|
||||
else {
|
||||
for (String value : parameter.getValue()) {
|
||||
if (!partNames.contains(parameter.getKey())) {
|
||||
if (parameter.getValue().isEmpty()) {
|
||||
writePartBoundary(writer);
|
||||
writePart(parameter.getKey(), value, null, null, writer);
|
||||
writer.println();
|
||||
writePart(parameter.getKey(), "", null, null, writer);
|
||||
}
|
||||
else {
|
||||
for (String value : parameter.getValue()) {
|
||||
writePartBoundary(writer);
|
||||
writePart(parameter.getKey(), value, null, null, writer);
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user