Skip to content

Commit

Permalink
Fix crash on empty tables (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctytgat authored and RobWin committed Dec 1, 2017
1 parent aad3878 commit 1646fef
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.github.swagger2markup.spi.DefinitionsDocumentExtension;
import io.github.swagger2markup.spi.MarkupComponent;
import io.swagger.models.Model;
import io.swagger.models.properties.Property;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.Validate;

Expand Down Expand Up @@ -157,11 +158,14 @@ private List<ObjectType> typeSection(MarkupDocBuilder markupDocBuilder, String d
if (isNotBlank(typeInfosString))
markupDocBuilder.paragraph(typeInfosString, true);

propertiesTableComponent.apply(markupDocBuilder,
PropertiesTableComponent.parameters(
((ObjectType) modelType).getProperties(),
definitionName,
inlineDefinitions));
Map<String, Property> properties = ((ObjectType) modelType).getProperties();
if (!properties.isEmpty()) {
propertiesTableComponent.apply(markupDocBuilder,
PropertiesTableComponent.parameters(
properties,
definitionName,
inlineDefinitions));
}
} else if (modelType != null) {
MarkupDocBuilder typeInfos = copyMarkupDocBuilder(markupDocBuilder);
typeInfos.italicText(labels.getLabel(TYPE_COLUMN)).textLine(COLON + modelType.displaySchema(markupDocBuilder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,29 @@ private MarkupDocBuilder buildSecurityScheme(MarkupDocBuilder markupDocBuilder,
if (isNotBlank(oauth2Scheme.getTokenUrl())) {
paragraphBuilder.italicText(labels.getLabel(TOKEN_URL)).textLine(COLON + oauth2Scheme.getTokenUrl());
}
StringColumn.Builder nameColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(NAME_COLUMN)))
.putMetaData(TableComponent.WIDTH_RATIO, "3")
.putMetaData(TableComponent.HEADER_COLUMN, "true");
StringColumn.Builder descriptionColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(DESCRIPTION_COLUMN)))
.putMetaData(TableComponent.WIDTH_RATIO, "17")
.putMetaData(TableComponent.HEADER_COLUMN, "true");

if (oauth2Scheme.getScopes() != null) {

markupDocBuilder.paragraph(paragraphBuilder.toString(), true);

if (oauth2Scheme.getScopes() != null && !oauth2Scheme.getScopes().isEmpty()) {
StringColumn.Builder nameColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(NAME_COLUMN)))
.putMetaData(TableComponent.WIDTH_RATIO, "3")
.putMetaData(TableComponent.HEADER_COLUMN, "true");
StringColumn.Builder descriptionColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(DESCRIPTION_COLUMN)))
.putMetaData(TableComponent.WIDTH_RATIO, "17")
.putMetaData(TableComponent.HEADER_COLUMN, "true");

for (Map.Entry<String, String> scope : oauth2Scheme.getScopes().entrySet()) {
nameColumnBuilder.add(scope.getKey());
descriptionColumnBuilder.add(scope.getValue());
}

return tableComponent.apply(markupDocBuilder, TableComponent.parameters(nameColumnBuilder.build(),
descriptionColumnBuilder.build()));
} else {

return markupDocBuilder;
}

markupDocBuilder.paragraph(paragraphBuilder.toString(), true);
return tableComponent.apply(markupDocBuilder, TableComponent.parameters(nameColumnBuilder.build(),
descriptionColumnBuilder.build()));
} else {
return markupDocBuilder.paragraph(paragraphBuilder.toString(), true);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/github/swagger2markup/AsciidocConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,28 @@ public void testWithPageBreaks() throws IOException, URISyntaxException {
Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/page_breaks").toURI());
DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithPageBreaks.html");
}

@Test
public void testWithEmptyTables() throws IOException, URISyntaxException {
//Given
String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/json/swagger_empty_tables.json"));
Path outputDirectory = Paths.get("build/test/asciidoc/empty_tables");
FileUtils.deleteQuietly(outputDirectory.toFile());

//When
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withPageBreaks(new ArrayList<>(asList(PageBreakLocations.BEFORE_OPERATION, PageBreakLocations.BEFORE_OPERATION_EXAMPLE_REQUEST)))
.build();

Swagger2MarkupConverter.from(swaggerJsonString)
.withConfig(config)
.build()
.toFolder(outputDirectory);

//Then
String[] files = outputDirectory.toFile().list();
assertThat(files).hasSize(4).containsAll(expectedFiles);
Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/empty_tables").toURI());
DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithEmptyTables.html");
}
}
44 changes: 44 additions & 0 deletions src/test/resources/expected/asciidoc/empty_tables/definitions.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

[[_definitions]]
== Definitions

[[_category]]
=== Category
[%hardbreaks]
__Polymorphism__ : Composition


[options="header", cols=".^3a,.^4a"]
|===
|Name|Schema
|**id** +
__optional__|integer (int64)
|===


[[_identified]]
=== Identified

[options="header", cols=".^3a,.^4a"]
|===
|Name|Schema
|**id** +
__optional__|integer (int64)
|===


[[_user]]
=== User
[%hardbreaks]
__Polymorphism__ : Composition


[options="header", cols=".^3a,.^4a"]
|===
|Name|Schema
|**id** +
__optional__|integer (int64)
|===



42 changes: 42 additions & 0 deletions src/test/resources/expected/asciidoc/empty_tables/overview.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
= Swagger Petstore API


[[_overview]]
== Overview
This is a sample server Petstore server.

http://swagger.wordnik.com[Learn about Swagger] or join the IRC channel `#swagger` on irc.freenode.net.

For this sample, you can use the api key `special-key` to test the authorization filters


=== Version information
[%hardbreaks]
__Version__ : 1.0.0


=== Contact information
[%hardbreaks]
__Contact__ : [email protected]


=== License information
[%hardbreaks]
__License__ : Apache 2.0
__License URL__ : http://www.apache.org/licenses/LICENSE-2.0.html
__Terms of service__ : http://helloreverb.com/terms/


=== URI scheme
[%hardbreaks]
__Host__ : petstore.swagger.wordnik.com
__BasePath__ : /v2
__Schemes__ : HTTP


=== Tags

* user : User resource



48 changes: 48 additions & 0 deletions src/test/resources/expected/asciidoc/empty_tables/paths.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

[[_paths]]
== Paths

<<<

[[_createuser]]
=== Create user
....
POST /users
....


==== Description
This can only be done by the logged in user.


==== Parameters

[options="header", cols=".^2a,.^3a,.^9a,.^4a"]
|===
|Type|Name|Description|Schema
|**Body**|**body** +
__optional__|Created user object|<<_user,User>>
|===


==== Responses

[options="header", cols=".^2a,.^14a,.^4a"]
|===
|HTTP Code|Description|Schema
|**default**|successful operation|No Content
|===


==== Produces

* `application/json`
* `application/xml`


==== Tags

* user



21 changes: 21 additions & 0 deletions src/test/resources/expected/asciidoc/empty_tables/security.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

[[_securityscheme]]
== Security

[[_api_key]]
=== api_key
[%hardbreaks]
__Type__ : apiKey
__Name__ : api_key
__In__ : HEADER


[[_petstore_auth]]
=== petstore_auth
[%hardbreaks]
__Type__ : oauth2
__Flow__ : implicit
__Token URL__ : http://petstore.swagger.wordnik.com/api/oauth/dialog



122 changes: 122 additions & 0 deletions src/test/resources/json/swagger_empty_tables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server.\n\n[Learn about Swagger](http://swagger.wordnik.com) or join the IRC channel `#swagger` on irc.freenode.net.\n\nFor this sample, you can use the api key `special-key` to test the authorization filters\n",
"version": "1.0.0",
"title": "Swagger Petstore API",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "petstore.swagger.wordnik.com",
"basePath": "/v2",
"schemes": [
"http"
],
"tags": [
{
"name": "user",
"description": "User resource"
}
],
"paths": {
"/users": {
"post": {
"tags": [
"user"
],
"summary": "Create user",
"description": "This can only be done by the logged in user.",
"operationId": "createUser",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"in": "body",
"name": "body",
"description": "Created user object",
"required": false,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"default": {
"description": "successful operation"
}
}
}
}
},
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
},
"petstore_auth": {
"type": "oauth2",
"authorizationUrl": "http://petstore.swagger.wordnik.com/api/oauth/dialog",
"flow": "implicit",
"scopes": {
}
}
},
"responses": {
"FoundPets": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
}
},
"parameters": {
"petId": {
"in": "path",
"name": "petId",
"description": "ID of the pet",
"required": true,
"type": "integer",
"format": "int64"
}
},
"definitions": {
"Identified": {
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
}
},
"User": {
"allOf": [
{
"$ref": "#/definitions/Identified"
},
{
"properties": {
}
}
]
},
"Category": {
"allOf": [
{
"$ref": "#/definitions/Identified"
}
]
}
}
}

0 comments on commit 1646fef

Please sign in to comment.