Skip to content

Commit a6417ae

Browse files
authored
Merge pull request #3179 from thc202/openapi/xml-warn
openapi: do not generate JSON for XML content
2 parents c6853ea + cb7eaaf commit a6417ae

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

addOns/openapi/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
### Added
88
- Use path and operation servers ([Issue #6754](https://github.com/zaproxy/zaproxy/issues/6754)).
99

10+
### Changed
11+
- Warn when request has content `application/xml`, not supported (Related to [Issue #6767](https://github.com/zaproxy/zaproxy/issues/6767)).
12+
1013
## [22] - 2021-09-16
1114
### Changed
1215
- Maintenance changes.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
*/
2020
package org.zaproxy.zap.extension.openapi.converter.swagger;
2121

22+
import io.swagger.v3.oas.models.Operation;
2223
import io.swagger.v3.oas.models.media.Content;
2324
import io.swagger.v3.oas.models.media.Encoding;
2425
import io.swagger.v3.oas.models.media.Schema;
2526
import io.swagger.v3.oas.models.parameters.RequestBody;
2627
import java.util.List;
2728
import java.util.Map;
29+
import org.parosproxy.paros.Constant;
2830
import org.parosproxy.paros.network.HttpHeaderField;
2931
import org.zaproxy.zap.extension.openapi.generators.Generators;
3032
import org.zaproxy.zap.extension.openapi.generators.HeadersGenerator;
@@ -33,6 +35,7 @@
3335

3436
public class RequestModelConverter {
3537

38+
private static final String CONTENT_APPLICATION_XML = "application/xml";
3639
private OperationModel operationModel;
3740
private Generators generators;
3841

@@ -58,7 +61,8 @@ private String generatePath() {
5861
}
5962

6063
private String generateBody() {
61-
RequestBody requestBody = operationModel.getOperation().getRequestBody();
64+
Operation operation = operationModel.getOperation();
65+
RequestBody requestBody = operation.getRequestBody();
6266
if (requestBody != null) {
6367
Content content = requestBody.getContent();
6468
Schema<?> schema;
@@ -81,6 +85,15 @@ private String generateBody() {
8185
return generators.getBodyGenerator().generateMultiPart(schema, encoding);
8286
}
8387

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 "";
95+
}
96+
8497
if (!content.isEmpty()) {
8598
schema = content.entrySet().iterator().next().getValue().getSchema();
8699
return generators.getBodyGenerator().generate(schema);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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.
1214
<br><br>
1315
The add-on will automatically detect any OpenAPI definitions and spider them as long as they are in scope.
1416
<br><br>

addOns/openapi/src/main/resources/org/zaproxy/zap/extension/openapi/resources/Messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ openapi.swaggerconverter.targeturl.invalid = Failed to create/normalise the targ
7979
openapi.swaggerconverter.targeturl.missingcomponents = The target URL does not have the scheme or authority component:\n{0}
8080
8181
openapi.unsupportedscheme = The scheme of the URL is not HTTP or HTTPS:\n{0}
82+
openapi.unsupportedcontent = Not generating request body for operation {0}, the content {1} is not supported.

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
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.isEmptyString;
2225
import static org.junit.jupiter.api.Assertions.assertEquals;
2326
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2427
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -31,16 +34,31 @@
3134
import java.nio.charset.StandardCharsets;
3235
import java.util.ArrayList;
3336
import org.apache.commons.io.IOUtils;
37+
import org.junit.jupiter.api.AfterAll;
38+
import org.junit.jupiter.api.BeforeAll;
3439
import org.junit.jupiter.api.BeforeEach;
3540
import org.junit.jupiter.api.Test;
41+
import org.parosproxy.paros.Constant;
42+
import org.zaproxy.zap.extension.openapi.ExtensionOpenApi;
3643
import org.zaproxy.zap.extension.openapi.converter.swagger.OperationModel;
3744
import org.zaproxy.zap.extension.openapi.converter.swagger.RequestModelConverter;
3845
import org.zaproxy.zap.extension.openapi.generators.BodyGenerator;
3946
import org.zaproxy.zap.extension.openapi.generators.Generators;
47+
import org.zaproxy.zap.testutils.TestUtils;
4048

41-
class BodyGeneratorUnitTest {
49+
class BodyGeneratorUnitTest extends TestUtils {
4250
Generators generators;
4351

52+
@BeforeAll
53+
static void setUp() {
54+
mockMessages(new ExtensionOpenApi());
55+
}
56+
57+
@AfterAll
58+
static void cleanUp() {
59+
Constant.messages = null;
60+
}
61+
4462
@BeforeEach
4563
void init() {
4664
generators = new Generators(null);
@@ -674,6 +692,22 @@ void shouldGenerateBodyWithNoSchema() throws IOException {
674692
assertEquals("", request);
675693
}
676694

695+
@Test
696+
void shouldNotGenerateContentForApplicationXml() throws IOException {
697+
// Given
698+
OpenAPI definition = parseResource("openapi_xml_bodies.yaml");
699+
OperationModel operationModel =
700+
new OperationModel("/xml", definition.getPaths().get("/xml").getPost(), null);
701+
// When
702+
String content = new RequestModelConverter().convert(operationModel, generators).getBody();
703+
// Then
704+
assertThat(content, isEmptyString());
705+
assertThat(
706+
generators.getErrorMessages(),
707+
contains(
708+
"Not generating request body for operation xml, the content application/xml is not supported."));
709+
}
710+
677711
private OpenAPI parseResource(String fileName) throws IOException {
678712
ParseOptions options = new ParseOptions();
679713
options.setResolveFully(true);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.0
2+
info:
3+
title: XML bodies
4+
version: 1.0.0
5+
servers:
6+
- url: http://localhost:@@@PORT@@@/
7+
paths:
8+
/xml:
9+
post:
10+
operationId: xml
11+
requestBody:
12+
content:
13+
'application/xml':
14+
schema:
15+
properties:
16+
value-string:
17+
type: string
18+
value-boolean:
19+
type: boolean
20+
value-integer:
21+
type: integer
22+
responses:
23+
default:
24+
description:
25+
content:
26+
text/plain: {}

0 commit comments

Comments
 (0)