Skip to content

Commit

Permalink
Merge branch 'master' into issue1658
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina authored Mar 8, 2022
2 parents 2ec8ebb + 6a82e92 commit 4fe4669
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ components:
```

### Extensions
This project has a core artifact--`swagger-parser`, which uses Java Service Provider Inteface (SPI) so additional extensions can be added.
This project has a core artifact--`swagger-parser`, which uses Java Service Provider Interface (SPI) so additional extensions can be added.

To build your own extension, you simply need to create a `src/main/resources/META-INF/services/io.swagger.v3.parser.core.extensions.SwaggerParserExtension` file with the full classname of your implementation. Your class must also implement the `io.swagger.v3.parser.core.extensions.SwaggerParserExtension` interface. Then, including your library with the `swagger-parser` module will cause it to be triggered automatically.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,26 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul

ObjectNode contentNode = getObject("content", obj, false, location, result);
if (contentNode != null) {
parameter.setContent(getContent(contentNode, String.format("%s.%s", location, "content"), result));
}
Content content = getContent(contentNode, String.format("%s.%s", location, "content"), result);
if(content.size() == 0) {
result.unsupported(location,"content with no media type",contentNode);
result.invalid();
}
else if(content.size() > 1) {
result.unsupported(location,"content with multiple media types",contentNode);
result.invalid();
}
else if(parameter.getSchema() != null) {
result.unsupported(location,"content when schema defined",contentNode);
result.invalid();
}
else {
parameter.setContent(content);
}
}
else if(parameter.getSchema() == null) {
result.missing(location,"content");
}

Map<String, Object> extensions = getExtensions(obj);
if (extensions != null && extensions.size() > 0) {
Expand Down Expand Up @@ -3105,9 +3123,14 @@ public RequestBody getRequestBody(ObjectNode node, String location, ParseResult
}

final ObjectNode contentNode = getObject("content", node, true, location, result);
if (contentNode != null) {
body.setContent(getContent(contentNode, location + ".content", result));
}
Content content = getContent(contentNode, location + ".content", result);
if(content != null && content.isEmpty()) {
result.unsupported(location,"content with no media type",contentNode);
result.invalid();
}
else {
body.setContent(content);
}

Map<String, Object> extensions = getExtensions(node);
if (extensions != null && extensions.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,63 @@ public void testDeserializeByteString() {
"attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
}

@Test
public void testBodyContent() {
String json =
"{"
+ " \"openapi\": \"3.0.0\","
+ " \"info\": {"
+ " \"title\": \"Operations\","
+ " \"version\": \"0.0.0\""
+ " },"
+ " \"paths\": {"
+ " \"/operations\": {"
+ " \"post\": {"
+ " \"requestBody\": {"
+ " \"description\": \"Content empty\","
+ " \"content\": {"
+ " }"
+ " },"
+ " \"responses\": {"
+ " \"default\": {"
+ " \"description\": \"None\""
+ " }"
+ " }"
+ " },"
+ " \"put\": {"
+ " \"requestBody\": {"
+ " \"description\": \"Content undefined\""
+ " },"
+ " \"responses\": {"
+ " \"default\": {"
+ " \"description\": \"None\""
+ " }"
+ " }"
+ " }"
+ " }"
+ " }"
+ "}"
;
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);

Operation post = result.getOpenAPI().getPaths().get( "/operations").getPost();
assertEquals( post.getRequestBody().getContent(), null, "Empty content");
assertEquals
(result.getMessages().contains("attribute paths.'/operations'(post).requestBody.content with no media type is unsupported"),
true,
"Empty content error reported");

Operation put = result.getOpenAPI().getPaths().get( "/operations").getPut();
assertEquals( put.getRequestBody().getContent(), null, "Empty content");
assertEquals
(result.getMessages().contains("attribute paths.'/operations'(put).requestBody.content is missing"),
true,
"Missing content error reported");

assertEquals( result.getMessages().size(), 2, "Messages");
}

@Test
public void testStyleInvalid() {
String json =
Expand Down Expand Up @@ -1270,6 +1327,161 @@ public void testStyleInvalid() {
assertEquals(result.getMessages().get(0), "attribute paths.'/realize/{param}'(post).parameters.[param].style is not of type `StyleEnum`");
}

@Test
public void testParamContent() {
String json =
"{"
+ " \"openapi\": \"3.0.0\","
+ " \"info\": {"
+ " \"title\": \"Operations\","
+ " \"version\": \"0.0.0\""
+ " },"
+ " \"paths\": {"
+ " \"/operations\": {"
+ " \"post\": {"
+ " \"parameters\": ["
+ " {"
+ " \"name\": \"param0\","
+ " \"in\": \"query\","
+ " \"content\": {"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"param1\","
+ " \"in\": \"query\","
+ " \"content\": {"
+ " \"text/plain\": {"
+ " }"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"param2\","
+ " \"in\": \"query\","
+ " \"content\": {"
+ " \"text/plain\": {"
+ " },"
+ " \"application/json\": {"
+ " \"schema\": {"
+ " \"type\": \"object\""
+ " }"
+ " }"
+ " }"
+ " }"
+ " ],"
+ " \"responses\": {"
+ " \"default\": {"
+ " \"description\": \"None\""
+ " }"
+ " }"
+ " }"
+ " }"
+ " }"
+ "}"
;
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
Operation post = result.getOpenAPI().getPaths().get( "/operations").getPost();

Parameter param0 =
post.getParameters().stream()
.filter( p -> "param0".equals( p.getName()))
.findFirst()
.orElseThrow( () -> new IllegalStateException( "Can't find parameter=param0"));
assertEquals
(result.getMessages().contains( "attribute paths.'/operations'(post).parameters.[param0].content with no media type is unsupported"),
true,
"No media types error reported");
assertEquals( param0.getContent(), null, "Empty content");

Parameter param1 =
post.getParameters().stream()
.filter( p -> "param1".equals( p.getName()))
.findFirst()
.orElseThrow( () -> new IllegalStateException( "Can't find parameter=param1"));
assertEquals( param1.getContent().size(), 1, "Valid content size");

Parameter param2 =
post.getParameters().stream()
.filter( p -> "param2".equals( p.getName()))
.findFirst()
.orElseThrow( () -> new IllegalStateException( "Can't find parameter=param2"));
assertEquals
(result.getMessages().contains( "attribute paths.'/operations'(post).parameters.[param2].content with multiple media types is unsupported"),
true,
"Multiple media types error reported");
assertEquals( param2.getContent(), null, "Content with multiple media types");

assertEquals( result.getMessages().size(), 2, "Messages");
}

@Test
public void testParamData() {
String json =
"{"
+ " \"openapi\": \"3.0.0\","
+ " \"info\": {"
+ " \"title\": \"Operations\","
+ " \"version\": \"0.0.0\""
+ " },"
+ " \"paths\": {"
+ " \"/operations\": {"
+ " \"post\": {"
+ " \"parameters\": ["
+ " {"
+ " \"name\": \"param0\","
+ " \"in\": \"query\""
+ " },"
+ " {"
+ " \"name\": \"param2\","
+ " \"in\": \"query\","
+ " \"content\": {"
+ " \"text/plain\": {"
+ " }"
+ " },"
+ " \"schema\": {"
+ " \"type\": \"object\""
+ " }"
+ " }"
+ " ],"
+ " \"responses\": {"
+ " \"default\": {"
+ " \"description\": \"None\""
+ " }"
+ " }"
+ " }"
+ " }"
+ " }"
+ "}"
;
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
Operation post = result.getOpenAPI().getPaths().get( "/operations").getPost();

Parameter param0 =
post.getParameters().stream()
.filter( p -> "param0".equals( p.getName()))
.findFirst()
.orElseThrow( () -> new IllegalStateException( "Can't find parameter=param0"));
assertEquals
(result.getMessages().contains( "attribute paths.'/operations'(post).parameters.[param0].content is missing"),
true,
"No schema or content error reported");
assertEquals( param0.getContent(), null, "No schema or content");

Parameter param2 =
post.getParameters().stream()
.filter( p -> "param2".equals( p.getName()))
.findFirst()
.orElseThrow( () -> new IllegalStateException( "Can't find parameter=param2"));
assertEquals
(result.getMessages().contains( "attribute paths.'/operations'(post).parameters.[param2].content when schema defined is unsupported"),
true,
"Both schema and content error reported");
assertEquals( param2.getContent(), null, "Content when schema defined");

assertEquals( result.getMessages().size(), 2, "Messages");
}

@Test
public void testDeserializeWithMessages() {
String yaml = "openapi: '3.0.0'\n" +
Expand Down Expand Up @@ -2602,29 +2814,32 @@ public void readContentObject(JsonNode rootNode) throws Exception {
final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);

PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
PathItem petByStatusEndpoint = paths.get("/pet/findByStatusContent");
Assert.assertNotNull(petByStatusEndpoint.getGet());
Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 3);

Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(),3);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(),1);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(),"array");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExample(),null);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExamples().get("list").getSummary(),"List of Names");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(),"array");

Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/xml").getExamples().get("list").getSummary(),"List of names");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/xml").getExamples().get("list").getValue(),"<Users><User name='Bob'/><User name='Diane'/><User name='Mary'/><User name='Bill'/></Users>");

Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/xml").getExamples().get("empty").getSummary());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/xml").getExamples().get("empty").getSummary(),"Empty list");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/xml").getExamples().get("empty").getValue(),"<Users/>");


Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getSummary(),"List of names");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getValue(),"Bob,Diane,Mary,Bill");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getSummary(),"Empty");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getValue(),"");
Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().size(),1);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getSummary(),"List of names");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("list").getValue(),"<Users><User name='Bob'/><User name='Diane'/><User name='Mary'/><User name='Bill'/></Users>");
Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getSummary(),"Empty list");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(1).getContent().get("application/xml").getExamples().get("empty").getValue(),"<Users/>");

Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(2).getContent());
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().size(),1);
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getSummary(),"List of names");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("list").getValue(),"Bob,Diane,Mary,Bill");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getSummary(),"Empty");
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(2).getContent().get("text/plain").getExamples().get("empty").getValue(),"");

PathItem petEndpoint = paths.get("/pet");
Assert.assertNotNull(petEndpoint.getPut());
Expand Down Expand Up @@ -2825,7 +3040,7 @@ public void readExamplesObject(JsonNode rootNode) throws Exception {

final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);
Assert.assertEquals(paths.size(), 18);
Assert.assertEquals(paths.size(), 19);

//parameters operation get
PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
Expand All @@ -2849,7 +3064,7 @@ public void readSchemaObject(JsonNode rootNode) throws Exception {

final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);
Assert.assertEquals(paths.size(), 18);
Assert.assertEquals(paths.size(), 19);

//parameters operation get
PathItem petByStatusEndpoint = paths.get("/pet/findByStatus");
Expand All @@ -2875,7 +3090,7 @@ public void readSchemaArray(JsonNode rootNode) throws Exception {

final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);
Assert.assertEquals(paths.size(), 18);
Assert.assertEquals(paths.size(), 19);

//parameters operation get
PathItem petByStatusEndpoint = paths.get("/pet/findByTags");
Expand All @@ -2902,7 +3117,7 @@ public void readProducesTestEndpoint(JsonNode rootNode) throws Exception {

final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);
Assert.assertEquals(paths.size(), 18);
Assert.assertEquals(paths.size(), 19);

//parameters operation get
PathItem producesTestEndpoint = paths.get("/producesTest");
Expand Down Expand Up @@ -2969,7 +3184,7 @@ public void readPathsObject(JsonNode rootNode) throws Exception {

final Paths paths = openAPI.getPaths();
Assert.assertNotNull(paths);
Assert.assertEquals(paths.size(), 18);
Assert.assertEquals(paths.size(), 19);


PathItem petRef = paths.get("/pathItemRef");
Expand Down
Loading

0 comments on commit 4fe4669

Please sign in to comment.