Skip to content

Commit c8d1a8c

Browse files
committed
Application/XML support for request bodies
This commit adds support for handling Application/XML content types in request bodies.This enhancement resolves Issue #6767 Signed-off-by:Tryfon Iason Papatriantafyllou <[email protected]>
1 parent d34392d commit c8d1a8c

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

addOns/openapi/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
### Added
88
- Allow to import the OpenAPI definitions with a user (Issue 7739).
99
- Honour context exclusions when importing (Issue 8021).
10+
- Application/XML support for request bodies (Issue 6767).
1011

1112
### Fixed
1213
- Allow to select the contexts of the Automation Framework plan when configuring the job.

addOns/openapi/src/main/java/org/zaproxy/zap/extension/openapi/converter/swagger/RequestModelConverter.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import io.swagger.v3.oas.models.parameters.RequestBody;
2727
import java.util.List;
2828
import java.util.Map;
29-
import org.parosproxy.paros.Constant;
3029
import org.parosproxy.paros.network.HttpHeaderField;
3130
import org.zaproxy.zap.extension.openapi.generators.Generators;
3231
import org.zaproxy.zap.extension.openapi.generators.HeadersGenerator;
@@ -85,13 +84,9 @@ private String generateBody() {
8584
return generators.getBodyGenerator().generateMultiPart(schema, encoding);
8685
}
8786

88-
if (content.containsKey(CONTENT_APPLICATION_XML)) {
89-
generators.addErrorMessage(
90-
Constant.messages.getString(
91-
"openapi.unsupportedcontent",
92-
operation.getOperationId(),
93-
CONTENT_APPLICATION_XML));
94-
return "";
87+
if (content.containsKey("application/xml")) {
88+
schema = content.get("application/xml").getSchema();
89+
return generators.getBodyGenerator().generateXml(schema);
9590
}
9691

9792
if (!content.isEmpty()) {

addOns/openapi/src/main/java/org/zaproxy/zap/extension/openapi/generators/BodyGenerator.java

+38
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,44 @@ public String generateMultiPart(Schema<?> schema, Map<String, Encoding> encoding
332332
return "";
333333
}
334334

335+
@SuppressWarnings("rawtypes")
336+
public String generateXml(Schema<?> schema) {
337+
if (schema == null) {
338+
return "";
339+
}
340+
341+
StringBuilder xml = new StringBuilder();
342+
String elementName = null;
343+
generateXmlElements(schema, xml);
344+
return xml.toString();
345+
}
346+
347+
@SuppressWarnings("rawtypes")
348+
private void generateXmlElements(Schema<?> schema, StringBuilder xml) {
349+
for (Map.Entry<String, Schema> property : schema.getProperties().entrySet()) {
350+
String elementName = property.getKey();
351+
xml.append("<");
352+
xml.append(elementName);
353+
xml.append(">");
354+
355+
if (property.getValue().getProperties() != null) {
356+
generateXmlElements(property.getValue(), xml);
357+
} else {
358+
String value = dataGenerator.generateValue(elementName, property.getValue(), false);
359+
if ("string".equals(property.getValue().getType())
360+
&& value.startsWith("\"")
361+
&& value.endsWith("\"")) {
362+
value = value.substring(1, value.length() - 1); // Remove surrounding quotes
363+
}
364+
xml.append(value);
365+
}
366+
367+
xml.append("</");
368+
xml.append(elementName);
369+
xml.append(">\n");
370+
}
371+
}
372+
335373
private static String getPropertyContentType(Schema<?> schema) {
336374
String type;
337375

addOns/openapi/src/main/javahelp/org/zaproxy/zap/extension/openapi/resources/help/contents/openapi.html

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
<BODY>
1010
<H1>OpenAPI Support</H1>
1111
This add-on allows you to spider and import OpenAPI (Swagger) definitions, versions 1.2, 2.0, and 3.0.
12-
<br>
13-
<strong>Note:</strong> Generation of XML content is currently not supported.
1412
<br><br>
1513
The add-on will automatically detect any OpenAPI definitions and spider them as long as they are in scope.
1614
<br><br>

addOns/openapi/src/test/java/org/zaproxy/zap/extension/openapi/v3/BodyGeneratorUnitTest.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
*/
2020
package org.zaproxy.zap.extension.openapi.v3;
2121

22-
import static org.hamcrest.MatcherAssert.assertThat;
23-
import static org.hamcrest.Matchers.contains;
24-
import static org.hamcrest.Matchers.emptyString;
25-
import static org.hamcrest.Matchers.is;
2622
import static org.junit.jupiter.api.Assertions.assertEquals;
2723
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2824
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -716,19 +712,30 @@ void shouldGenerateBodyWithNoSchema() throws IOException {
716712
}
717713

718714
@Test
719-
void shouldNotGenerateContentForApplicationXml() throws IOException {
715+
void shouldGenerateXmlObject() throws IOException {
720716
// Given
721-
OpenAPI definition = parseResource("openapi_xml_bodies.yaml");
722-
OperationModel operationModel =
723-
new OperationModel("/xml", definition.getPaths().get("/xml").getPost(), null);
717+
OpenAPI openAPI = parseResource("openapi_xml_bodies.yaml");
718+
724719
// When
725-
String content = new RequestModelConverter().convert(operationModel, generators).getBody();
720+
String xmlString =
721+
generators
722+
.getBodyGenerator()
723+
.generateXml(
724+
openAPI.getPaths()
725+
.get("/xml")
726+
.getPost()
727+
.getRequestBody()
728+
.getContent()
729+
.get("application/xml")
730+
.getSchema());
731+
726732
// Then
727-
assertThat(content, is(emptyString()));
728-
assertThat(
729-
generators.getErrorMessages(),
730-
contains(
731-
"Not generating request body for operation xml, the content type application/xml is not supported."));
733+
String expectedOutput =
734+
"<value-string>John Doe</value-string>\n"
735+
+ "<value-boolean>true</value-boolean>\n"
736+
+ "<value-integer>10</value-integer>\n";
737+
738+
assertEquals(expectedOutput, xmlString);
732739
}
733740

734741
@Test

0 commit comments

Comments
 (0)