Compare commits

..

145 Commits

Author SHA1 Message Date
Robert Winkler
4948c8bea1 * Swagger License is not mandatory anymore
* Updated markup-document-builder from v0.1.3 to v0.1.4
2015-05-20 10:12:21 +02:00
Robert Winkler
9397bf4255 Merge remote-tracking branch 'origin/master' 2015-05-18 12:40:13 +02:00
Robert Winkler
a6d85d7b0c * Swagger License is not mandatory anymore
* Updated markup-document-builder from v0.1.3 to v0.1.4
2015-05-18 12:40:05 +02:00
Robert Winkler
16371443e8 Updated documentation 2015-05-11 07:18:43 +02:00
Robert Winkler
5738d86988 Updated documentation 2015-05-11 07:05:38 +02:00
Robert Winkler
e1bf3fbe94 Updated documentation 2015-05-10 20:11:23 +02:00
Robert Winkler
7ce98408f4 Updated documentation 2015-05-10 20:07:07 +02:00
Robert Winkler
1a7b219c40 Updated documentation 2015-05-10 20:02:37 +02:00
Robert Winkler
8a8e93f3ca Updated documentation 2015-05-08 21:36:58 +02:00
Robert Winkler
1d95d9afc2 Updates documentation 2015-05-07 11:42:37 +02:00
Robert Winkler
5b49187cb7 Updated documentation 2015-05-06 09:11:33 +02:00
Robert Winkler
7b99565636 Updated documentation 2015-05-06 09:07:19 +02:00
Robert Winkler
32980d1476 Updated documentation 2015-05-06 09:05:04 +02:00
Robert Winkler
490a6d974f Updated documentation 2015-05-06 08:52:53 +02:00
Robert Winkler
4e971817a2 Updated documentation 2015-05-06 08:51:20 +02:00
Robert Winkler
f649553727 Updated documentation 2015-05-05 16:26:28 +02:00
Robert Winkler
3576a80872 Added Swagger2Markup Gradle Plugin 2015-05-05 15:59:54 +02:00
Robert Winkler
2bbb9ffd8e Merge remote-tracking branch 'origin/master' 2015-05-05 14:02:35 +02:00
Robert Winkler
7d2acae537 Bugfix: Definition name must be lowercase so that descriptions file can be found 2015-05-05 14:02:26 +02:00
Robert Winkler
1e68587097 Updates documentation 2015-05-05 10:35:34 +02:00
Robert Winkler
71d77ce6ab Updates documentation 2015-05-05 10:16:13 +02:00
Robert Winkler
de849b69d4 Updates documentation 2015-05-05 10:12:00 +02:00
Robert Winkler
a8e84995ea Updated documentation 2015-05-04 16:52:05 +02:00
Robert Winkler
edffd47067 Updated documentation 2015-05-04 16:51:30 +02:00
Robert Winkler
cef474955e Updated documentation 2015-05-04 16:49:33 +02:00
Robert Winkler
00dffe7b57 Updated documentation 2015-05-04 16:47:13 +02:00
Robert Winkler
fea79bd4f9 Updated documentation 2015-05-04 16:46:03 +02:00
Robert Winkler
fb5d30f700 Updated documentation 2015-05-04 16:43:36 +02:00
Robert Winkler
ab15b393d0 Support for including hand-written descriptions instead of using Swagger Annotations for descriptions 2015-05-04 16:05:00 +02:00
Robert Winkler
7d1cf12c48 Merge branch 'master' of https://github.com/RobWin/swagger2markup 2015-05-04 15:47:19 +02:00
Robert Winkler
14d8acc385 Support for including hand-written descriptions instead of using Swagger Annotations for descriptions 2015-05-04 15:46:30 +02:00
Robert Winkler
e91d639d93 Updates documentation 2015-05-04 11:55:02 +02:00
Robert Winkler
3671873349 Updates documentation 2015-05-04 10:50:15 +02:00
Robert Winkler
f920a3e954 Updates documentation 2015-05-04 10:47:24 +02:00
Robert Winkler
cb35f1f319 Updates documentation 2015-05-04 10:46:45 +02:00
Robert Winkler
c8805f5018 Updates documentation 2015-05-04 10:46:20 +02:00
Robert Winkler
cc04be6790 Updated documentation 2015-05-04 07:06:16 +02:00
Robert Winkler
13eb453c2a Updated documentation 2015-05-04 07:05:25 +02:00
Robert Winkler
d20dfa9027 Updated documentation 2015-05-04 06:55:52 +02:00
Robert Winkler
38429152f2 Updated documentation 2015-05-03 16:24:57 +02:00
Robert Winkler
cf382e9890 Updated documentation 2015-05-03 16:22:40 +02:00
Robert Winkler
81b1846fcd Updated documentation 2015-05-03 14:57:57 +02:00
Robert Winkler
7d48a65ec1 Updated documentation 2015-05-03 14:55:07 +02:00
Robert Winkler
dc50cdff15 Updated documentation 2015-05-03 14:51:43 +02:00
Robert Winkler
165e615698 Updated documentation 2015-05-03 14:46:43 +02:00
Robert Winkler
2559bef4ce Updated documentation 2015-05-03 14:43:01 +02:00
Robert Winkler
7847a74104 Updated documentation 2015-05-03 14:41:47 +02:00
Robert Winkler
685596c213 Updated documentation 2015-05-01 19:30:42 +02:00
Robert Winkler
20862d3ee4 Updated documentation 2015-04-30 20:41:52 +02:00
Robert Winkler
2527792b58 Updated documentation 2015-04-29 11:34:31 +02:00
Robert Winkler
d85d307e30 Updated documentation 2015-04-24 12:52:30 +02:00
Robert Winkler
58e01e96e1 Updated documentation 2015-04-24 08:29:25 +02:00
Robert Winkler
1b425b83b6 Merge remote-tracking branch 'origin/master' 2015-04-24 08:27:04 +02:00
Robert Winkler
96d7195365 Added Apache2 Copyright Header 2015-04-24 08:26:48 +02:00
Robert Winkler
e14af63a4c Updated documentation 2015-04-23 19:33:52 +02:00
Robert Winkler
925f1a1f81 Updated documentation 2015-04-23 16:28:45 +02:00
Robert Winkler
1e5e2b20dd Updated documentation 2015-04-23 15:56:03 +02:00
Robert Winkler
d209718987 Updated documentation 2015-04-22 13:55:38 +02:00
Robert Winkler
31619b26c9 Updated documentation 2015-04-21 14:04:42 +02:00
Robert Winkler
2277c7410b Updated documentation 2015-04-21 14:03:30 +02:00
Robert Winkler
90d8b088d2 Updated documentation 2015-04-21 14:02:45 +02:00
Robert Winkler
a5d9588a7d Updated documentation 2015-04-21 14:02:29 +02:00
Robert Winkler
ff17b1d857 Updated documentation 2015-04-21 13:52:56 +02:00
Robert Winkler
2a5bd83bb8 Updated documentation 2015-04-21 08:57:35 +02:00
Robert Winkler
aa7800ba37 Updated documentation 2015-04-21 08:56:58 +02:00
Robert Winkler
8a1a1a9d91 Updated documentation 2015-04-21 08:36:38 +02:00
Robert Winkler
e7b59f8a2f Updated documentation 2015-04-21 08:33:24 +02:00
Robert Winkler
8e75655346 Updated documentation 2015-04-21 08:31:31 +02:00
Robert Winkler
e45498a1d7 Updated documentation 2015-04-21 08:25:03 +02:00
Robert Winkler
5f838ca2b4 Updated documentation 2015-04-21 08:22:44 +02:00
Robert Winkler
85e53ce89f Updated documentation 2015-04-20 22:09:37 +02:00
Robert Winkler
51c1cd3fe5 Updated documentation 2015-04-20 17:00:15 +02:00
Robert Winkler
29c7db1980 Updated documentation 2015-04-20 16:53:45 +02:00
Robert Winkler
663323cd1e Updated documentation 2015-04-20 16:50:37 +02:00
Robert Winkler
ab13a6ec0e Updated documentation 2015-04-20 16:44:37 +02:00
Robert Winkler
d78eb490cb Updated documentation 2015-04-20 16:42:59 +02:00
Robert Winkler
48e724cb4b Updated documentation 2015-04-20 16:09:39 +02:00
Robert Winkler
0a3db6c013 Updated documentation 2015-04-20 16:08:42 +02:00
Robert Winkler
a843fad921 Fixed unit test issue 2015-04-20 15:50:41 +02:00
Robert Winkler
de41e3b058 Updated documentation 2015-04-20 15:42:45 +02:00
Robert Winkler
6bf3a5aab4 Updated documentation 2015-04-20 15:38:46 +02:00
Robert Winkler
8d4bc8c0bc == Version 0.4.0
* Updated Swagger-Parser from 1.0.0 to 1.0.5
* Updated commons-lang to commons-lang3
* Swagger2MarkupConverter generates three documents now: overview, paths and definitions
* Support for enums in HeaderParameter, QueryParameter, FormParameter and QueryParameter
* Support for global consumes, produces and tags
2015-04-20 15:35:37 +02:00
Robert Winkler
76df322bb4 Updated documentation 2015-04-18 20:35:41 +02:00
Robert Winkler
36cdd3cebb Updated documentation 2015-04-18 20:08:36 +02:00
Robert Winkler
ca4587cb29 Updated documentation 2015-04-18 20:05:11 +02:00
Robert Winkler
ce958ba798 Updated documentation 2015-04-18 20:02:49 +02:00
Robert Winkler
7c5c0fea8c Updated documentation 2015-04-18 13:39:57 +02:00
Robert Winkler
5bc557a5d2 Updated documentation 2015-04-18 13:38:02 +02:00
Robert Winkler
e23a649c17 Updated documentation 2015-04-18 13:36:56 +02:00
Robert Winkler
a5300ceab5 Updated documentation 2015-04-18 13:22:49 +02:00
Robert Winkler
fdb405e52a Updated documentation 2015-04-18 13:03:25 +02:00
Robert Winkler
79cdd26ef0 Updated documentation 2015-04-18 12:52:49 +02:00
Robert Winkler
87c24c8f31 Updated documentation 2015-04-18 11:03:17 +02:00
Robert Winkler
6216165515 Updated documentation 2015-04-18 10:59:51 +02:00
Robert Winkler
77640d21be Updates documentation 2015-04-18 08:28:10 +02:00
Robert Winkler
57282f4933 Updated documentation 2015-04-17 14:19:41 +02:00
Robert Winkler
1a12e0dc11 Updated documentation 2015-04-17 14:16:21 +02:00
Robert Winkler
2b0317d1f6 Updated documentation 2015-04-17 14:10:41 +02:00
Robert Winkler
8fce706f31 Updated documentation 2015-04-17 14:06:08 +02:00
Robert Winkler
ae0f3ab31d Updated documentation 2015-04-17 14:01:27 +02:00
Robert Winkler
34cb438757 Updated documentation 2015-04-17 13:58:29 +02:00
Robert Winkler
8747ba2c1d Updated documentation 2015-04-17 13:57:10 +02:00
Robert Winkler
bef4e3c735 Updated documentation 2015-04-17 13:55:14 +02:00
Robert Winkler
6396b8fdb7 * Support of YAML or JSON String as input. 2015-04-17 13:25:26 +02:00
Robert Winkler
e27ba4304d * Fixed issue #8: logback.xml on the classpath
* Fixed issue #13: unknown format not supported for properties
2015-04-16 12:17:40 +02:00
Robert Winkler
4c86e73c4e * Fixed issue #8: logback.xml on the classpath
* Fixed issue #13: unknown format not supported for properties
2015-04-16 12:15:37 +02:00
Robert Winkler
9f9fa8c444 Added springfox 2015-04-15 17:03:37 +02:00
Robert Winkler
9647116572 Added springfox 2015-04-15 17:01:45 +02:00
Robert Winkler
736891fbdd Merge branch 'master' of https://github.com/RobWin/swagger2markup 2015-04-15 16:40:58 +02:00
Robert Winkler
acec733af7 Added docs folder 2015-04-15 16:34:45 +02:00
Robert Winkler
73cdc35c80 Fixed issue: ResponseModel from api declaration is not rendered
Fixed issue: @ApiModelProperty metadata are ignored for definitions file
Fixed issue: logback.xml on the classpath
2015-04-15 16:32:22 +02:00
Robert Winkler
cae91b3565 Merge remote-tracking branch 'origin/develop' into develop 2015-04-15 16:30:51 +02:00
Robert Winkler
ec610f4fbb Fixed issue: ResponseModel from api declaration is not rendered
Fixed issue: @ApiModelProperty metadata are ignored for definitions file
Fixed issue: logback.xml on the classpath
2015-04-15 16:30:37 +02:00
Robert Winkler
edf9ba01bd Merge pull request #12 from RobWin/develop
Added Gitter badge
2015-04-08 08:42:29 +02:00
Robert Winkler
e942252d9a Added Gitter badge 2015-04-08 08:35:41 +02:00
Robert Winkler
04d1773c03 Fixed markup-document-builder version dependency 2015-03-23 09:02:43 +01:00
Robert Winkler
448899c193 Merge branch 'release/0.2.2' 2015-03-23 09:00:20 +01:00
Robert Winkler
23d1db4fd5 Fixed markup-document-builder version dependency 2015-03-23 08:59:49 +01:00
Robert Winkler
903d3ed452 Merge pull request #3 from RobWin/master
fixed wrong dependency
2015-03-17 12:11:42 +01:00
Robert Winkler
1ad78ba9f6 fixed wrong dependency 2015-03-17 12:01:38 +01:00
Robert Winkler
82416a33cc Fixed maven POM issue 2015-02-20 11:02:23 +01:00
Robert Winkler
e67b32d537 Merge branch 'master' of https://github.com/RobWin/swagger2markup 2015-02-20 10:13:32 +01:00
Robert Winkler
dbb6f9ace8 Merge branch 'release/0.2.1' 2015-02-20 10:13:01 +01:00
Robert Winkler
19384f97b9 Published to Maven Central 2015-02-20 10:12:28 +01:00
Robert Winkler
90de0a3c6a Fixed build 2015-02-20 09:54:56 +01:00
Robert Winkler
ae77e814f7 Updated Bintry badge 2015-02-20 09:53:37 +01:00
Robert Winkler
d165a0f842 Added GPG Signing and Maven Central Sync 2015-02-20 09:47:47 +01:00
Robert Winkler
0833a74c37 Updated documentation 2015-02-20 08:50:36 +01:00
Robert Winkler
4ec7ecf25b Updated documentation 2015-02-20 08:46:30 +01:00
Robert Winkler
99dd062fc9 Added markup-document-builder library and removed Builder from project
Updated documentation
2015-02-20 08:41:17 +01:00
Robert Winkler
52462466ff Added markup-document-builder library 2015-02-20 08:24:09 +01:00
Robert Winkler
d2c69abb71 Updated wrong groupId 2015-02-19 20:22:28 +01:00
Robert Winkler
11d4311986 Updated documentation 2015-02-19 16:38:21 +01:00
Robert Winkler
e2f1bca8f1 Removed header 2015-02-19 15:48:17 +01:00
Robert Winkler
1db1af705b Updated documentation 2015-02-19 14:49:09 +01:00
Robert Winkler
dd140a532b Updated documentation 2015-02-19 14:17:56 +01:00
Robert Winkler
1f51a9bc91 Merge branch 'develop' 2015-02-19 14:15:16 +01:00
Robert Winkler
5f28eb4073 Updated documentation 2015-02-19 14:15:05 +01:00
Robert Winkler
97541bfc83 Updated documentation 2015-02-19 14:14:17 +01:00
Robert Winkler
9144155200 Merge branch 'develop' 2015-02-19 14:07:20 +01:00
Robert Winkler
84d64ec6f6 Updated documentation 2015-02-19 14:07:07 +01:00
Robert Winkler
e7c9ed54db Merge branch 'develop' 2015-02-19 11:59:39 +01:00
Robert Winkler
44838f0b6f Updated maven repository 2015-02-19 11:59:28 +01:00
Robert Winkler
d7efd403c7 Merge branch 'release/v0.2.0' 2015-02-19 11:38:31 +01:00
Robert Winkler
31a4c21aa1 Merge branch 'release/v0.2.0' into develop 2015-02-19 11:38:31 +01:00
48 changed files with 1465 additions and 752 deletions

View File

@@ -1,19 +1,29 @@
= Swagger2Markup
:author: Robert Winkler
:version: 0.1.0
:version: 0.5.3
:hardbreaks:
image:https://travis-ci.org/RobWin/swagger2markup.svg["Build Status", link="https://travis-ci.org/RobWin/swagger2markup"] image:https://coveralls.io/repos/RobWin/swagger2markup/badge.svg["Coverage Status", link="https://coveralls.io/r/RobWin/swagger2markup"] image:http://img.shields.io/:version-{version}-blue.svg["Semantic Versioning", link="https://bintray.com/robwin/maven/swagger2markup/0.1.0/view"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
image:https://travis-ci.org/RobWin/swagger2markup.svg["Build Status", link="https://travis-ci.org/RobWin/swagger2markup"] image:https://coveralls.io/repos/RobWin/swagger2markup/badge.svg["Coverage Status", link="https://coveralls.io/r/RobWin/swagger2markup"] image:https://api.bintray.com/packages/robwin/maven/swagger2markup/images/download.svg[link="https://bintray.com/robwin/maven/swagger2markup/_latestVersion"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
image:https://badges.gitter.im/Join%20Chat.svg[link="https://gitter.im/RobWin/swagger2markup?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"]
== Overview
This project is a Swagger to Markup (AsciiDoc and Markdown) converter. The *Swagger2MarkupConverter* takes a swagger.json or swagger.yaml file as source and converts it into an AsciiDoc or Markdown document. The Swagger source file can be located locally or remotely accessible via HTTP. The Swagger2MarkupConverter supports the Swagger 1.2 and 2.0 specification. Internally it uses the _official_ https://github.com/swagger-api/swagger-parser[swagger-parser].
The primary goal of this project is to *simplify the generation of an up-to-date RESTful API documentation by combining documentation that's been hand-written with auto-generated API documentation* produced by https://github.com/swagger-api[Swagger]. The result is intended to be an *up-to-date, easy-to-read, on- and offline user guide*, comparable to https://developer.github.com/v3/[GitHub's API documentation]. The output of Swagger2Markup can be used as an alternative to https://github.com/swagger-api/swagger-ui[swagger-ui] and can be served as static content.
The primary goal of this project is to simplify the documentation of RESTful APIs. The result is intended to be an easy-to-read, on- and offline user guide, comparable to https://developer.github.com/v3/[GitHub's API documentation].
Swagger2Markup converts a Swagger JSON or YAML file into several *AsciiDoc* or *GitHub Flavored Markdown* documents which can be combined with hand-written documentation. The Swagger source file can be located locally or remotely via HTTP. Swagger2Markup supports the Swagger 1.2 and 2.0 specification. Internally it uses the _official_ https://github.com/swagger-api/swagger-parser[swagger-parser] and my https://github.com/RobWin/markup-document-builder[markup-document-builder].
== Usage
You can use Swagger2Markup to convert your contract-first Swagger YAML file into a human-readable format and combine it with hand-written documentation. As an alternative, you can choose the code-first approach and use Swagger2Markup together with https://github.com/swagger-api/swagger-core/tree/master/samples/java-jersey2[Swagger JAX-RS], https://github.com/springfox/springfox[springfox] or https://github.com/spring-projects/spring-restdocs[spring-restdocs]. See https://github.com/RobWin/swagger2markup#usage-guide[usage guide] below. If you are are Gradle user, you can also use the https://github.com/RobWin/swagger2markup-gradle-plugin[Swagger2Markup Gradle Plugin].
http://asciidoctor.org/docs/asciidoc-writers-guide/[AsciiDoc] is preferable to Markdown as it has more features. AsciiDoc is a text document format for writing documentation, articles, books, ebooks, slideshows, web pages and blogs. AsciiDoc files can be converted to *HTML*, *PDF* and *EPUB*. AsciiDoc is much better suited for describing public APIs than *JavaDoc* or *Annotations*.
You can generate your HTML5, PDF and EPUB documentation via https://github.com/asciidoctor/asciidoctorj[asciidoctorj] or even better via the https://github.com/asciidoctor/asciidoctor-gradle-plugin[asciidoctor-gradle-plugin] or https://github.com/asciidoctor/asciidoctor-maven-plugin[asciidoctor-maven-plugin]. You can also use https://github.com/jbake-org/jbake[JBake], https://github.com/tomchristie/mkdocs[MkDocs], https://github.com/rtfd/readthedocs.org[ReadTheDocs] or https://github.com/tripit/slate[slate] to publish your AsciiDoc or Markdown documentation.
The project requires at least JDK 7.
== Usage guide
=== Adding Swagger2Markup to your project
The project is published in JCenter and Maven Central.
==== Maven
@@ -21,16 +31,19 @@ The primary goal of this project is to simplify the documentation of RESTful API
----
<repositories>
<repository>
<id>jcenter-release</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependency>
<groupId>io.github.robwin.swagger2markup</groupId>
<groupId>io.github.robwin</groupId>
<artifactId>swagger2markup</artifactId>
<version>0.2.0</version>
<version>0.5.3</version>
</dependency>
----
@@ -42,105 +55,351 @@ repositories {
jcenter()
}
compile "io.github.robwin:swagger2markup:0.2.0"
compile "io.github.robwin:swagger2markup:0.5.3"
----
=== Using Swagger2Markup
Using the Swagger2MarkupConverter is simple. For instance, you can generate your AsciiDoc/Markdown documentation using https://github.com/spring-projects/spring-boot[Spring Boot] and https://github.com/martypitt/swagger-springmvc[swagger-springmvc] as follows.
See demo project https://github.com/RobWin/spring-swagger2markup-demo[spring-swagger2markup-demo].
Using the Swagger2MarkupConverter is simple. For example, if you are using https://github.com/spring-projects/spring-boot[Spring Boot] and https://github.com/springfox/springfox[springfox], you can generate your Swagger JSON file during the integration or unit test phase, convert the Swagger JSON file into AsciiDoc, convert AsciiDoc into HTML and PDF, copy the documentation into the Jar file and serve it as static content. That way there is no runtime overhead and there are no additional runtime libraries required.
The quickest way to get started is to look at the demo project https://github.com/RobWin/spring-swagger2markup-demo[spring-swagger2markup-demo]. The demo shows how to generate static docs (HTML5 and PDF) with the https://github.com/RobWin/swagger2markup-gradle-plugin[Swagger2Markup Gradle Plugin] and serve them as static content in a Spring Boot App under http://localhost:9080/docs/index.html and http://localhost:9080/docs/index.pdf.
==== Generate Markup during an integration test
Swagger2MarkupConverter can be used to make a request to a Swagger endpoint during an integration test. The Swagger2MarkupConverter writes the generated documents into the folder `src/docs/asciidoc/generated` or `src/docs/markdown/generated`.
[source,java]
----
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootSwaggerConfig.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest
@WebAppConfiguration
public class Swagger2MarkupTest {
@Test
public void convertSwaggerToMarkup() {
//Remote
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withMarkupLanguage(MarkupLanguage.MARKDOWN).
withExamples("docs/generated").withSchemas("docs/schemas").build()
.intoFolder("src/docs/markdown");
public void convertRemoteSwaggerToAsciiDoc() {
// Remote Swagger source
// Default is AsciiDoc
Swagger2MarkupConverter.from("http://localhost:8080/v2/api-docs").build()
.intoFolder("src/docs/asciidoc/generated");
// Then validate that three AsciiDoc files have been created
String[] files = new File("src/docs/asciidoc/generated").list();
assertThat(files).hasSize(3)
.containsAll(Arrays.asList("definitions.adoc", "overview.adoc", "paths.adoc"));
}
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withExamples("docs").withSchemas("docs/schemas").build()
.intoFolder("src/docs/asciidoc");
@Test
public void convertRemoteSwaggerToMarkdown() {
// Remote Swagger source
// Markdown
Swagger2MarkupConverter.from("http://localhost:8080/v2/api-docs")
.withMarkupLanguage(MarkupLanguage.MARKDOWN).build()
.intoFolder("src/docs/markdown/generated");
// Then validate that three Markdown files have been created
String[] files = new File("src/docs/markdown/generated").list();
assertThat(files).hasSize(3)
.containsAll(Arrays.asList("definitions.md", "overview.md", "paths.md"));
}
//Local
File file = new File(Swagger2MarkupTest.class.getResource("/json/swagger.json").getFile());
Swagger2MarkupConverter.from(file.getAbsolutePath()).build()
.intoFolder("src/docs/asciidoc");
@Test
public void convertLocalSwaggerToAsciiDoc() {
//Local Swagger source
//Default is AsciiDoc
String location = Swagger2MarkupTest.class.getResource("/json/swagger.json").getPath();
Swagger2MarkupConverter.from(location).build()
.intoFolder("src/docs/asciidoc/generated");
}
}
----
==== Generate Markup during an unit test with springfox-staticdocs
Spring's MVC Test framework can also be used to make a request to a https://github.com/springfox/springfox[springfox] Swagger endpoint during an unit test. A custom ResultHandler `Swagger2MarkupResultHandler` is used to automatically convert the Swagger JSON response into an AsciiDoc document. The custom ResultHandler is part of `springfox-staticdocs`. That way you also verify that your Swagger endpoint is working.
[source,java]
----
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
public class Swagger2MarkupTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void convertSwaggerToAsciiDoc() throws Exception {
this.mockMvc.perform(get("/v2/api-docs")
.accept(MediaType.APPLICATION_JSON))
.andDo(Swagger2MarkupResultHandler.outputDirectory("src/docs/asciidoc/generated").build())
.andExpect(status().isOk());
}
@Test
public void convertSwaggerToMarkdown() throws Exception {
this.mockMvc.perform(get("/v2/api-docs")
.accept(MediaType.APPLICATION_JSON))
.andDo(Swagger2MarkupResultHandler.outputDirectory("src/docs/markdown/generated")
.withMarkupLanguage(MarkupLanguage.MARKDOWN).build())
.andExpect(status().isOk());
}
}
----
==== Gradle dependencies
[source,groovy]
----
dependencies {
...
compile 'io.springfox:springfox-swagger2:2.0.0'
testCompile 'io.springfox:springfox-staticdocs:2.0.0'
...
}
----
==== Springfox configuration
The following is a complete https://github.com/springfox/springfox[springfox] configuration to use Swagger in a Spring Boot Application.
[source,java]
----
@SpringBootApplication
@EnableSwagger
public class SpringBootSwaggerConfig {
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootTestConfig.class, args);
SpringApplication.run(Application.class, args);
}
@Autowired
private SpringSwaggerConfig springSwaggerConfig;
@Bean
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo()).excludeAnnotations(Controller.class);
public Docket restApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(ant("/api/**"))
.build();
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"My Apps API Title",
"My Apps API Description",
"My Apps API terms of service",
"My Apps API Contact Email",
"My Apps API Licence Type",
"My Apps API License URL"
);
return apiInfo;
return new ApiInfoBuilder()
.title("Petstore API Title")
.description("Petstore API Description")
.contact("Petstore API Contact Email")
.version("1.0.0")
.build();
}
}
----
You can generate your HTML5 and PDF documentation via https://github.com/asciidoctor/asciidoctorj[asciidoctorj] or even better via the https://github.com/asciidoctor/asciidoctor-gradle-plugin[asciidoctor-gradle-plugin] or https://github.com/aalmiray/markdown-gradle-plugin[markdown-gradle-plugin].
You can also use https://github.com/tomchristie/mkdocs[MkDocs] and https://github.com/rtfd/readthedocs.org[ReadTheDocs] to publish your Markdown documentation.
See http://spring-swagger2markup-demo.readthedocs.org/[ReadTheDocs-demo]
==== Combine generated documentation with your hand-written documentation
== Examples
== Swagger source file
The following shows how you can combine the generated documentation with your hand-written AsciiDoc documentation. You have to create an `index.adoc` (it must not be necessarily called index). To include the programmatically generated snippets in your documentation, you use Asciidoc's `include` macro. The `generated` variable is configured below.
image::images/generated_docs.PNG[generated_docs]
You can generate your HTML5 and PDF documentation via the https://github.com/asciidoctor/asciidoctor-gradle-plugin[asciidoctor-gradle-plugin] or https://github.com/asciidoctor/asciidoctor-maven-plugin[asciidoctor-maven-plugin]. The following listing shows how to configure the Asciidoctor Gradle plugin. By default it searches for AsciiDoc files in `src/docs/asciidoc` and puts the HTML and PDF output into `build/asciidoc/html5` and `build/asciidoc/pdf`. The `generated` attribute is used to replace the variable in the `index.adoc` file and to provide configurable access to the generated snippets.
[source,groovy]
----
ext {
generatedDocumentation = file('src/docs/asciidoc/generated')
}
test {
outputs.dir generatedDocumentation
}
asciidoctor {
dependsOn test
sources {
include 'index.adoc'
}
backends = ['html5', 'pdf']
attributes = [
doctype: 'book',
toc: 'left',
toclevels: '2',
numbered: '',
sectlinks: '',
sectanchors: '',
generated: generatedDocumentation
]
}
----
You can copy the output into your Jar file and serve the documentation as static content under `http://localhost:9080/docs/index.html` and `http://localhost:9080/docs/index.pdf`.
[source,groovy]
----
jar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
from ("${asciidoctor.outputDir}/pdf") {
into 'static/docs'
}
}
----
==== Include spring-restdocs example snippets
Swagger2Markup can be used together with https://github.com/spring-projects/spring-restdocs[spring-restdocs]. Swagger2Markup can include the generated HTTP request and response example snippets from spring-restdocs into the generated AsciiDoc document. See https://github.com/spring-projects/spring-restdocs[spring-restdocs] how to configure it. Currently spring-restdocs does only support AsciiDoc.
Let's say you have a Swagger-annotated Spring RestController method with an ApiOperation value: `Add a new pet to the store`
[source,java]
----
@RequestMapping(method = POST)
@ApiOperation(value = "Add a new pet to the store")
@ApiResponses(value = {@ApiResponse(code = 405, message = "Invalid input")})
public ResponseEntity<String> addPet(
@ApiParam(value = "Pet object that needs to be added to the store", required = true) @RequestBody Pet pet) {
petData.add(pet);
return Responses.ok("SUCCESS");
}
----
By convention the target folder of the generated request and response example files must be similar to the value of the ApiOperation, but with underscores and lowercase. For example a folder for `@ApiOperation(value = "Add a new pet to the store")` must be called `add_a_new_pet_to_the_store`.
[source,java]
----
@Test
public void findPetById() throws Exception {
this.mockMvc.perform(post("/api/pet/").content(createPet())
.contentType(MediaType.APPLICATION_JSON))
.andDo(RestDocumentation.document("add_a_new_pet_to_the_store"))
.andExpect(status().isOk());
}
----
The system property `org.springframework.restdocs.outputDir` is used to control the output base directory where the folder `add_a_new_pet_to_the_store` and the generated snippets are written to. The spring-restdocs output base directory is configured as follows:
[source,groovy]
----
ext {
generatedDocumentation = file('src/docs/asciidoc/generated')
}
test {
systemProperty 'org.springframework.restdocs.outputDir', generatedDocumentation
outputs.dir generatedDocumentation
}
----
You must specify the base output directory of spring-restdocs with the builder method `withExamples("src/docs/asciidoc/generated")`.
[source,java]
----
@Test
public void convertToAsciiDoc() throws Exception {
this.mockMvc.perform(get("/v2/api-docs")
.accept(MediaType.APPLICATION_JSON))
.andDo(Swagger2MarkupResultHandler.outputDirectory("src/docs/asciidoc")
.withExamples("src/docs/asciidoc/generated").build())
.andExpect(status().isOk());
}
----
By convention the Swagger2MarkupConverter searches for a method annotated with `@ApiOperation(value = "Add a new pet to the store")` in a folder called `src/docs/asciidoc/generated/add_a_new_pet_to_the_store` and includes the `http-request.adoc` and `http-response.adoc` files, if they are available.
The AsciiDoc HTML output would look as follows:
image::images/springrestdocs_examples.PNG[springrestdocs]
==== Include hand-written descriptions into the generated documentation
If you don't want to pollute your source code with Swagger annotations just to add descriptions to Operations, Parameters and Model definitions. Like here:
[source, java]
----
@RequestMapping(method = PUT)
@ApiOperation(value = "Update an existing pet")
@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid ID supplied"),
@ApiResponse(code = 404, message = "Pet not found"),
@ApiResponse(code = 405, message = "Validation exception")})
public ResponseEntity<String> updatePet(
@ApiParam(value = "Pet object that needs to be added to the store", required = true) @RequestBody Pet pet) {
petData.add(pet);
return Responses.ok("SUCCESS");
}
----
You can create hand-written descriptions and include them with the builder method `withDescriptions()` into your documentation by specifying the base folder of your documentation.
[source, java]
----
Swagger2MarkupConverter.from(file.getAbsolutePath()).withDescriptions("src/docs/asciidoc").build()
.intoFolder("src/docs/asciidoc");
----
By convention you need two folders `paths` and `definitions` inside your description base folder.
The `paths` folder contains sub folders for all operations. The folder must be named similar to the value of the ApiOperation annotation, but with underscores and lowercase.
For example a folder for `@ApiOperation(value = "Update an existing pet")` must be called `update_an_existing_pet`.
The `definitions` folder contains sub folders for all models. The folder must be named similar to the name of the Model, but lowercase.
For example a folder for a model called `User` must be called `user`.
You can have a global description file for each operation or model. And you can have one description file per operation parameter or model property.
For example:
image::images/handwritten_descriptions.PNG[handwritten_descriptions]
The AsciiDoc HTML output would look as follows:
image::images/handwritten_descr_asciidoc.PNG[handwritten_descr_asciidoc]
==== Include JSON and XML Schema files.
Swagger2Markup can also include JSON and XML Schema files into the generated document.
[source,java]
----
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").withSchemas("src/docs/schemas").build()
.intoFolder("src/docs/asciidoc");
----
You can create the schema files during a unit test as follows:
[source,java]
----
RestDocumented restDocumented = RestDocumented.fromProperties();
restDocumented.documentJsonSchema(MailStorageQuota.class, "src/docs/schemas");
restDocumented.documentXmlSchema(MailStorageQuota.class, "src/docs/schemas");
----
I will make RestDocumented public soon. RestDocumented creates a MailStorageQuota.xsd and MailStorageQuota.json file in the folder `src/docs/schemas`. The Swagger2MarkupConverter will include the JSON and XML Schemas, if a Swagger Operation uses the MailStorageQuota class as input or output.
== Screenshots
=== Swagger source
image::images/swagger_json.PNG[swagger_json]
=== Generated AsciiDoc file
=== Generated AsciiDoc
image::images/asciidoc.PNG[asciidoc]
=== Generated Markdown file
=== Generated Markdown
image::images/markdown.PNG[markdown]
=== Generated HTML using AsciidoctorJ
image::images/asciidoc_html.PNG[asciidoc_html]
=== Generated HTML using Mkdocs
image::images/mkdocs_html.PNG[mkdocs_html]
=== Generated PDF using AsciidoctorJ
image::images/asciidoc_pdf.PNG[asciidoc_pdf]
== Document Builder
The Swagger2Markup library allows to build an AsciiDoc or Markdown document via the Builder pattern:
[source,java]
----
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.ASCIIDOC);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
== License
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.MARKDOWN);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
----
Copyright 2015 Robert Winkler
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

View File

@@ -4,4 +4,40 @@
* Initial version with support for AsciiDoc and Markdown
== Version 0.2.0
* This version is not downward compatible. This version supports includes of example files and JSON/XML Schema files. See documentation.
* This version is not downward compatible. This version supports includes of example files and JSON/XML Schema files. See documentation.
=== Version 0.2.1
* Signed jar files and published in Maven Central
=== Version 0.2.2
* Fixed wrong dependency version to io.github.robwin:markup-document-builder
=== Version 0.2.3
* Fixed issue #7: @ApiModelProperty metadata are ignored for definitions file
=== Version 0.2.4
* Fixed issue #8: logback.xml on the classpath
* Fixed issue #13: unknown format not supported for properties
== Version 0.3.0
* Support of YAML or JSON String as input.
== Version 0.4.0
* Updated Swagger-Parser from 1.0.0 to 1.0.5
* Updated commons-lang to commons-lang3
* Swagger2MarkupConverter generates three documents now: overview, paths and definitions
* Support for enums in HeaderParameter, QueryParameter, FormParameter and QueryParameter
* Support for global consumes, produces and tags
== Version 0.5.0
* Support for including hand-written descriptions instead of using Swagger Annotations for descriptions
== Version 0.5.1
* Bugfix: Definition name must be lowercase so that descriptions file can be found
== Version 0.5.2
* Swagger License is not mandatory anymore
* Updated markup-document-builder from v0.1.3 to v0.1.4
== Version 0.5.3
* Fixed compiler warning: [options] bootstrap class path not set in conjunction with -source 1.7

View File

@@ -6,14 +6,14 @@ buildscript {
dependencies {
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.2'
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.6'
classpath 'io.spring.gradle:dependency-management-plugin:0.3.1.RELEASE'
classpath 'io.spring.gradle:dependency-management-plugin:0.5.0.RELEASE'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
classpath 'org.asciidoctor:asciidoctorj:1.5.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
}
}
description = 'swagger2markup Build'
version = '0.2.0'
version = '0.5.3'
group = 'io.github.robwin'
apply plugin: 'java'
@@ -36,35 +36,68 @@ tasks.withType(JavaCompile) {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
compile 'io.github.robwin:markup-document-builder'
compile 'io.swagger:swagger-compat-spec-parser'
compile 'commons-collections:commons-collections'
compile 'commons-io:commons-io'
compile 'ch.qos.logback:logback-classic'
compile 'org.slf4j:slf4j-api'
testCompile 'junit:junit'
testCompile 'org.asciidoctor:asciidoctorj:1.5.2'
testCompile 'ch.qos.logback:logback-classic'
testCompile 'org.assertj:assertj-core'
}
dependencyManagement {
dependencies {
"io.swagger:swagger-compat-spec-parser" "1.0.0"
"commons-collections:commons-collections" "3.2.1"
"commons-io:commons-io" "2.4"
"com.mangofactory:swagger-springmvc" "0.9.5"
"com.jayway.restassured:spring-mock-mvc" "2.4.0"
"ch.qos.logback:logback-classic" "1.1.2"
"junit:junit" "4.11"
dependency "io.github.robwin:markup-document-builder:0.1.4"
dependency "io.swagger:swagger-compat-spec-parser:1.0.5"
dependency "commons-collections:commons-collections:3.2.1"
dependency "commons-io:commons-io:2.4"
dependency "junit:junit:4.11"
dependency "org.slf4j:slf4j-api:1.7.12"
dependency "ch.qos.logback:logback-classic:1.1.2"
dependency "org.assertj:assertj-core:2.0.0"
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
ext {
generatedDocumentation = file('build/docs/asciidoc/generated')
}
asciidoctor {
dependsOn test
sources {
include 'index.adoc'
}
backends = ['html5', 'pdf']
attributes = [
doctype: 'book',
toc: 'left',
toclevels: '2',
numbered: ''
numbered: '',
sectlinks: '',
sectanchors: '',
hardbreaks: '',
generated: generatedDocumentation
]
}
@@ -85,4 +118,4 @@ tasks.asciidoctor {
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
}

View File

@@ -25,10 +25,15 @@ jar {
if (!project.hasProperty('bintrayUsername')) ext.bintrayUsername = ''
if (!project.hasProperty('bintrayApiKey')) ext.bintrayApiKey = ''
if (!project.hasProperty('gpgPassphrase')) ext.gpgPassphrase = ''
if (!project.hasProperty('ossUser')) ext.ossUser = ''
if (!project.hasProperty('ossPassword')) ext.ossPassword = ''
bintray {
user = project.bintrayUsername
key = project.bintrayApiKey
dryRun = false //Whether to run this as dry-run, without deploying
publish = true //If version should be auto published after an upload
publications = ['mavenJava']
pkg {
repo = 'maven'
@@ -40,6 +45,15 @@ bintray {
licenses = ['Apache-2.0']
version {
vcsTag = project.version
gpg {
sign = true //Determines whether to GPG sign the files. The default is false
passphrase = project.gpgPassphrase //Optional. The passphrase for GPG signing'
}
mavenCentralSync {
sync = true //Optional (true by default). Determines whether to sync the version to Maven Central.
user = ossUser //OSS user token
password = ossPassword //OSS user password
}
}
}
}
@@ -51,6 +65,15 @@ publishing {
pom.withXml {
def devs = ['RobWin': 'Robert Winkler']
def root = asNode()
root.dependencies.'*'.findAll() {
it.scope.text() == 'runtime' && project.configurations.compile.allDependencies.find { dep ->
dep.name == it.artifactId.text()
}
}.each() {
it.scope*.value = 'compile'
}
root.appendNode('name', 'swagger2markup')
root.appendNode('packaging', 'jar')
root.appendNode('url', 'https://github.com/RobWin/swagger2markup')
@@ -70,6 +93,8 @@ publishing {
d.appendNode('name', it.value)
}
}
artifact sourcesJar
artifact javadocJar
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 44 KiB

BIN
images/asciidoc2_html.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

After

Width:  |  Height:  |  Size: 138 KiB

BIN
images/generated_docs.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -0,0 +1,5 @@
This is a hand-written description.
AsciiDoc is better suited for descriptions than:
* JavaDoc
* Annotations

View File

@@ -0,0 +1 @@
This is a hand-written description. AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1 @@
This is a hand-written description. AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1,3 @@
include::{generated}/overview.adoc[]
include::{generated}/paths.adoc[]
include::{generated}/definitions.adoc[]

View File

@@ -0,0 +1,6 @@
[source,bash]
----
$ curl 'http://localhost:8080/api/pet/' -i -X POST -H 'Content-Type: application/json' -H 'Content-Length: 111' -d '{"id":1,"category":{"id":1,"name":"Hund"},"name":"Wuffy","photoUrls":[],"tags":[],"status":null,"identifier":1}'
----

View File

@@ -0,0 +1,10 @@
[source,http]
----
POST /api/pet/ HTTP/1.1
Content-Type: application/json
Content-Length: 111
{"id":1,"category":{"id":1,"name":"Hund"},"name":"Wuffy","photoUrls":[],"tags":[],"status":null,"identifier":1}
----

View File

@@ -0,0 +1,10 @@
[source,http]
----
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 7
SUCCESS
----

View File

@@ -0,0 +1,2 @@
This is a hand-written description.
AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1,5 @@
This is a hand-written description.
AsciiDoc is better suited for descriptions than:
* JavaDoc
* Annotations

View File

@@ -0,0 +1,5 @@
This is a hand-written description.
AsciiDoc is better suited for descriptions than:
* JavaDoc
* Annotations

View File

@@ -0,0 +1 @@
This is a hand-written description. AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1 @@
This is a hand-written description. AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1 @@
This is a hand-written description. AsciiDoc is better suited for descriptions than *JavaDoc* and *Annotations*

View File

@@ -0,0 +1,5 @@
This is a hand-written description.
It is better suited for adding descriptions than:
* JavaDoc
* Annotations

View File

@@ -1,11 +1,34 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wordnik.swagger.models.Swagger;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.util.Yaml;
import io.github.robwin.markup.builder.MarkupLanguage;
import io.github.robwin.swagger2markup.builder.document.DefinitionsDocument;
import io.github.robwin.swagger2markup.builder.document.OverviewDocument;
import io.github.robwin.swagger2markup.builder.document.PathsDocument;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.swagger.parser.SwaggerParser;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,6 +45,8 @@ public class Swagger2MarkupConverter {
private final MarkupLanguage markupLanguage;
private final String examplesFolderPath;
private final String schemasFolderPath;
private final String descriptionsFolderPath;
private static final String OVERVIEW_DOCUMENT = "overview";
private static final String PATHS_DOCUMENT = "paths";
private static final String DEFINITIONS_DOCUMENT = "definitions";
@@ -30,23 +55,61 @@ public class Swagger2MarkupConverter {
* @param swagger the Swagger object
* @param examplesFolderPath the folderPath where examples are stored
* @param schemasFolderPath the folderPath where (XML, JSON)-Schema files are stored
* @param descriptionsFolderPath the folderPath where descriptions are stored
*/
Swagger2MarkupConverter(MarkupLanguage markupLanguage, Swagger swagger, String examplesFolderPath, String schemasFolderPath){
Swagger2MarkupConverter(MarkupLanguage markupLanguage, Swagger swagger, String examplesFolderPath, String schemasFolderPath, String descriptionsFolderPath){
this.markupLanguage = markupLanguage;
this.swagger = swagger;
this.examplesFolderPath = examplesFolderPath;
this.schemasFolderPath = schemasFolderPath;
this.descriptionsFolderPath = descriptionsFolderPath;
}
/**
* Creates a Swagger2MarkupConverter.Builder using a given Swagger source.
*
* @param swaggerSource the Swagger source. Can be a HTTP url or a path to a local file.
* @param swaggerLocation the Swagger location. Can be a HTTP url or a path to a local file.
* @return a Swagger2MarkupConverter
*/
public static Builder from(String swaggerSource){
Validate.notEmpty(swaggerSource, "swaggerSource must not be null!");
return new Builder(swaggerSource);
public static Builder from(String swaggerLocation){
Validate.notEmpty(swaggerLocation, "swaggerLocation must not be empty!");
return new Builder(swaggerLocation);
}
/**
* Creates a Swagger2MarkupConverter.Builder from a given Swagger model.
*
* @param swagger the Swagger source.
* @return a Swagger2MarkupConverter
*/
public static Builder from(Swagger swagger){
Validate.notNull(swagger, "swagger must not be null!");
return new Builder(swagger);
}
/**
* Creates a Swagger2MarkupConverter.Builder from a given Swagger YAML or JSON String.
*
* @param swagger the Swagger YAML or JSON String.
* @return a Swagger2MarkupConverter
* @throws java.io.IOException if String can not be parsed
*/
public static Builder fromString(String swagger) throws IOException {
Validate.notEmpty(swagger, "swagger must not be null!");
ObjectMapper mapper;
if(swagger.trim().startsWith("{")) {
mapper = Json.mapper();
}else {
mapper = Yaml.mapper();
}
JsonNode rootNode = mapper.readTree(swagger);
// must have swagger node set
JsonNode swaggerNode = rootNode.get("swagger");
if(swaggerNode == null)
throw new IllegalArgumentException("Swagger String is in the wrong format");
return new Builder(mapper.convertValue(rootNode, Swagger.class));
}
/**
@@ -61,6 +124,16 @@ public class Swagger2MarkupConverter {
buildDocuments(targetFolderPath);
}
/**
* Builds the document with the given markup language and returns it as a String
*
* @return a the document as a String
* @throws java.io.IOException if files can not be read
*/
public String asString() throws IOException{
return buildDocuments();
}
/**
* Writes a file for the Paths (API) and a file for the Definitions (Model)
@@ -68,8 +141,20 @@ public class Swagger2MarkupConverter {
* @throws IOException if a file cannot be written
*/
private void buildDocuments(String directory) throws IOException {
new PathsDocument(swagger, markupLanguage, examplesFolderPath).build().writeToFile(directory, PATHS_DOCUMENT, StandardCharsets.UTF_8);
new DefinitionsDocument(swagger, markupLanguage, schemasFolderPath).build().writeToFile(directory, DEFINITIONS_DOCUMENT, StandardCharsets.UTF_8);
new OverviewDocument(swagger, markupLanguage).build().writeToFile(directory, OVERVIEW_DOCUMENT, StandardCharsets.UTF_8);
new PathsDocument(swagger, markupLanguage, examplesFolderPath, descriptionsFolderPath).build().writeToFile(directory, PATHS_DOCUMENT, StandardCharsets.UTF_8);
new DefinitionsDocument(swagger, markupLanguage, schemasFolderPath, descriptionsFolderPath).build().writeToFile(directory, DEFINITIONS_DOCUMENT, StandardCharsets.UTF_8);
}
/**
* Returns a file for the Paths (API) and a file for the Definitions (Model)
* @return a the document as a String
*/
private String buildDocuments() throws IOException {
return new OverviewDocument(swagger, markupLanguage).build().toString().concat(
new PathsDocument(swagger, markupLanguage, examplesFolderPath, schemasFolderPath).build().toString()
.concat(new DefinitionsDocument(swagger, markupLanguage, schemasFolderPath, schemasFolderPath).build().toString()));
}
@@ -77,19 +162,29 @@ public class Swagger2MarkupConverter {
private final Swagger swagger;
private String examplesFolderPath;
private String schemasFolderPath;
private String descriptionsFolderPath;
private MarkupLanguage markupLanguage = MarkupLanguage.ASCIIDOC;
/**
* Creates a Builder using a given Swagger source.
*
* @param swaggerSource the Swagger source. Can be a HTTP url or a path to a local file.
* @param swaggerLocation the Swagger location. Can be a HTTP url or a path to a local file.
*/
Builder(String swaggerSource){
swagger = new SwaggerParser().read(swaggerSource);
Builder(String swaggerLocation){
swagger = new SwaggerParser().read(swaggerLocation);
}
/**
* Creates a Builder using a given Swagger model.
*
* @param swagger the Swagger source.
*/
Builder(Swagger swagger){
this.swagger = swagger;
}
public Swagger2MarkupConverter build(){
return new Swagger2MarkupConverter(markupLanguage, swagger, examplesFolderPath, schemasFolderPath);
return new Swagger2MarkupConverter(markupLanguage, swagger, examplesFolderPath, schemasFolderPath, descriptionsFolderPath);
}
/**
@@ -103,6 +198,17 @@ public class Swagger2MarkupConverter {
return this;
}
/**
* Include hand-written descriptions into the Paths and Definitions document
*
* @param descriptionsFolderPath the path to the folder where the description documents reside
* @return the Swagger2MarkupConverter.Builder
*/
public Builder withDescriptions(String descriptionsFolderPath){
this.descriptionsFolderPath = descriptionsFolderPath;
return this;
}
/**
* Include examples into the Paths document
*
@@ -124,7 +230,6 @@ public class Swagger2MarkupConverter {
this.schemasFolderPath = schemasFolderPath;
return this;
}
}
}

View File

@@ -1,12 +1,31 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.Model;
import com.wordnik.swagger.models.Swagger;
import com.wordnik.swagger.models.properties.Property;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupLanguage;
import io.github.robwin.swagger2markup.utils.PropertyUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@@ -18,11 +37,7 @@ import java.util.List;
import java.util.Map;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public class DefinitionsDocument extends MarkupDocument {
@@ -30,21 +45,26 @@ public class DefinitionsDocument extends MarkupDocument {
private static final List<String> IGNORED_DEFINITIONS = Arrays.asList("Void");
private static final String JSON_SCHEMA = "JSON Schema";
private static final String XML_SCHEMA = "XML Schema";
private static final String NAME_COLUMN = "Name";
private static final String TYPE_COLUMN = "Type";
public static final String JSON_SCHEMA_EXTENSION = ".json";
public static final String XML_SCHEMA_EXTENSION = ".xsd";
public static final String JSON = "json";
public static final String XML = "xml";
private static final String DESCRIPTION_FILE_NAME = "description";
private boolean schemasEnabled;
private String schemasFolderPath;
private String schemasFolderPath;
private boolean handWrittenDescriptionsEnabled;
private String descriptionsFolderPath;
public DefinitionsDocument(Swagger swagger, MarkupLanguage markupLanguage, String schemasFolderPath){
public DefinitionsDocument(Swagger swagger, MarkupLanguage markupLanguage, String schemasFolderPath, String descriptionsFolderPath){
super(swagger, markupLanguage);
if(StringUtils.isNotBlank(schemasFolderPath)){
this.schemasEnabled = true;
this.schemasFolderPath = schemasFolderPath;
}
if(StringUtils.isNotBlank(descriptionsFolderPath)){
this.handWrittenDescriptionsEnabled = true;
this.descriptionsFolderPath = descriptionsFolderPath + "/" + DEFINITIONS.toLowerCase();
}
if(schemasEnabled){
if (logger.isDebugEnabled()) {
logger.debug("Include schemas is enabled.");
@@ -54,6 +74,15 @@ public class DefinitionsDocument extends MarkupDocument {
logger.debug("Include schemas is disabled.");
}
}
if(handWrittenDescriptionsEnabled){
if (logger.isDebugEnabled()) {
logger.debug("Include hand-written descriptions is enabled.");
}
}else{
if (logger.isDebugEnabled()) {
logger.debug("Include hand-written descriptions is disabled.");
}
}
}
@Override
@@ -69,7 +98,7 @@ public class DefinitionsDocument extends MarkupDocument {
*/
private void definitions(Map<String, Model> definitions) throws IOException {
if(MapUtils.isNotEmpty(definitions)){
this.documentBuilder.sectionTitleLevel1(DEFINITIONS);
this.markupDocBuilder.sectionTitleLevel1(DEFINITIONS);
for(Map.Entry<String, Model> definitionsEntry : definitions.entrySet()){
String definitionName = definitionsEntry.getKey();
if(StringUtils.isNotBlank(definitionName)) {
@@ -105,16 +134,98 @@ public class DefinitionsDocument extends MarkupDocument {
* @param definitionName the name of the definition
* @param model the Swagger Model of the definition
*/
private void definition(String definitionName, Model model) {
this.documentBuilder.sectionTitleLevel2(definitionName);
private void definition(String definitionName, Model model) throws IOException {
this.markupDocBuilder.sectionTitleLevel2(definitionName);
descriptionSection(definitionName, model);
propertiesSection(definitionName, model);
}
private void propertiesSection(String definitionName, Model model) throws IOException {
Map<String, Property> properties = model.getProperties();
List<String> csvContent = new ArrayList<>();
csvContent.add(NAME_COLUMN + DELIMITER + TYPE_COLUMN + DELIMITER + REQUIRED_COLUMN);
for (Map.Entry<String, Property> propertyEntry : properties.entrySet()) {
Property property = propertyEntry.getValue();
csvContent.add(propertyEntry.getKey() + DELIMITER + property.getType() + DELIMITER + property.getRequired());
List<String> headerAndContent = new ArrayList<>();
List<String> header = Arrays.asList(NAME_COLUMN, DESCRIPTION_COLUMN, SCHEMA_COLUMN, REQUIRED_COLUMN);
headerAndContent.add(StringUtils.join(header, DELIMITER));
if(MapUtils.isNotEmpty(properties)){
for (Map.Entry<String, Property> propertyEntry : properties.entrySet()) {
Property property = propertyEntry.getValue();
String type = PropertyUtils.getType(property, markupLanguage);
String propertyName = propertyEntry.getKey();
List<String> content = Arrays.asList(propertyName, propertyDescription(definitionName, propertyName, property), type, Boolean.toString(property.getRequired()));
headerAndContent.add(StringUtils.join(content, DELIMITER));
}
this.markupDocBuilder.tableWithHeaderRow(headerAndContent);
}
this.documentBuilder.tableWithHeaderRow(csvContent);
}
private void descriptionSection(String definitionName, Model model) throws IOException {
if(handWrittenDescriptionsEnabled){
String description = handWrittenPathDescription(definitionName.toLowerCase(), DESCRIPTION_FILE_NAME);
if(StringUtils.isNotBlank(description)){
this.markupDocBuilder.paragraph(description);
}else{
if (logger.isInfoEnabled()) {
logger.info("Hand-written description cannot be read. Trying to use description from Swagger source.");
}
modelDescription(model);
}
}
else{
modelDescription(model);
}
}
private void modelDescription(Model model) {
String description = model.getDescription();
if (StringUtils.isNotBlank(description)) {
this.markupDocBuilder.paragraph(description);
}
}
private String propertyDescription(String definitionName, String propertyName, Property property) throws IOException {
String description;
if(handWrittenDescriptionsEnabled){
description = handWrittenPathDescription(definitionName.toLowerCase() + "/" + propertyName.toLowerCase(), DESCRIPTION_FILE_NAME);
if(StringUtils.isBlank(description)) {
if (logger.isInfoEnabled()) {
logger.info("Hand-written description file cannot be read. Trying to use description from Swagger source.");
}
description = StringUtils.defaultString(property.getDescription());
}
}
else{
description = StringUtils.defaultString(property.getDescription());
}
return description;
}
/**
* Reads a hand-written description
*
* @param descriptionFolder the name of the folder where the description file resides
* @param descriptionFileName the name of the description file
* @return the content of the file
* @throws IOException
*/
private String handWrittenPathDescription(String descriptionFolder, String descriptionFileName) throws IOException {
for (String fileNameExtension : markupLanguage.getFileNameExtensions()) {
java.nio.file.Path path = Paths.get(descriptionsFolderPath, descriptionFolder, descriptionFileName + fileNameExtension);
if (Files.isReadable(path)) {
if (logger.isInfoEnabled()) {
logger.info("Description file processed: {}", path);
}
return FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim();
} else {
if (logger.isDebugEnabled()) {
logger.debug("Description file is not readable: {}", path);
}
}
}
if (logger.isWarnEnabled()) {
logger.info("No description file found with correct file name extension in folder: {}", Paths.get(descriptionsFolderPath, descriptionFolder));
}
return null;
}
private void definitionSchema(String definitionName) throws IOException {
@@ -129,8 +240,8 @@ public class DefinitionsDocument extends MarkupDocument {
private void schema(String title, String schemasFolderPath, String schemaName, String language) throws IOException {
java.nio.file.Path path = Paths.get(schemasFolderPath, schemaName);
if (Files.isReadable(path)) {
this.documentBuilder.sectionTitleLevel3(title);
this.documentBuilder.source(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim(), language);
this.markupDocBuilder.sectionTitleLevel3(title);
this.markupDocBuilder.source(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim(), language);
if (logger.isInfoEnabled()) {
logger.info("Schema file processed: {}", path);
}
@@ -139,6 +250,5 @@ public class DefinitionsDocument extends MarkupDocument {
logger.debug("Schema file is not readable: {}", path);
}
}
}
}

View File

@@ -1,9 +1,27 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.Swagger;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilders;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupDocBuilders;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -11,25 +29,28 @@ import java.io.IOException;
import java.nio.charset.Charset;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public abstract class MarkupDocument {
protected static final String DELIMITER = ",";
protected static final String DELIMITER = "|";
protected static final String REQUIRED_COLUMN = "Required";
protected static final String SCHEMA_COLUMN = "Schema";
protected static final String NAME_COLUMN = "Name";
protected static final String DESCRIPTION_COLUMN = "Description";
protected static final String DESCRIPTION = DESCRIPTION_COLUMN;
protected static final String PRODUCES = "Produces";
protected static final String CONSUMES = "Consumes";
protected static final String TAGS = "Tags";
protected Logger logger = LoggerFactory.getLogger(getClass());
protected Swagger swagger;
protected MarkupLanguage markupLanguage;
protected DocumentBuilder documentBuilder;
protected MarkupDocBuilder markupDocBuilder;
MarkupDocument(Swagger swagger, MarkupLanguage markupLanguage){
this.swagger = swagger;
this.markupLanguage = markupLanguage;
this.documentBuilder = DocumentBuilders.documentBuilder(markupLanguage);
this.markupDocBuilder = MarkupDocBuilders.documentBuilder(markupLanguage);
}
/**
@@ -44,7 +65,7 @@ public abstract class MarkupDocument {
* Returns a string representation of the document.
*/
public String toString(){
return documentBuilder.toString();
return markupDocBuilder.toString();
}
/**
@@ -56,6 +77,6 @@ public abstract class MarkupDocument {
* @throws IOException if the file cannot be written
*/
public void writeToFile(String directory, String fileName, Charset charset) throws IOException{
documentBuilder.writeToFile(directory, fileName, charset);
markupDocBuilder.writeToFile(directory, fileName, charset);
}
}

View File

@@ -0,0 +1,153 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.*;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class OverviewDocument extends MarkupDocument {
private static final String OVERVIEW = "Overview";
private static final String CURRENT_VERSION = "Version information";
private static final String VERSION = "Version: ";
private static final String CONTACT_INFORMATION = "Contact information";
private static final String CONTACT_NAME = "Contact: ";
private static final String CONTACT_EMAIL = "Contact Email: ";
private static final String LICENSE_INFORMATION = "License information";
private static final String LICENSE = "License: ";
private static final String LICENSE_URL = "License URL: ";
private static final String TERMS_OF_SERVICE = "Terms of service: ";
private static final String URI_SCHEME = "URI scheme";
private static final String HOST = "Host: ";
private static final String BASE_PATH = "BasePath: ";
private static final String SCHEMES = "Schemes: ";
public OverviewDocument(Swagger swagger, MarkupLanguage markupLanguage){
super(swagger, markupLanguage);
}
/**
* Builds the MarkupDocument.
*
* @return the built MarkupDocument
* @throws java.io.IOException if the files to include are not readable
*/
@Override
public MarkupDocument build() throws IOException {
overview();
return this;
}
/**
* Builds the document header of the swagger model
*/
private void overview() {
Info info = swagger.getInfo();
this.markupDocBuilder.documentTitle(info.getTitle());
this.markupDocBuilder.sectionTitleLevel1(OVERVIEW);
if(StringUtils.isNotBlank(info.getDescription())){
this.markupDocBuilder.textLine(info.getDescription());
this.markupDocBuilder.newLine();
}
if(StringUtils.isNotBlank(info.getVersion())){
this.markupDocBuilder.sectionTitleLevel2(CURRENT_VERSION);
this.markupDocBuilder.textLine(VERSION + info.getVersion());
this.markupDocBuilder.newLine();
}
Contact contact = info.getContact();
if(contact != null){
this.markupDocBuilder.sectionTitleLevel2(CONTACT_INFORMATION);
if(StringUtils.isNotBlank(contact.getName())){
this.markupDocBuilder.textLine(CONTACT_NAME + contact.getName());
}
if(StringUtils.isNotBlank(contact.getEmail())){
this.markupDocBuilder.textLine(CONTACT_EMAIL + contact.getEmail());
}
this.markupDocBuilder.newLine();
}
License license = info.getLicense();
if(license != null && (StringUtils.isNotBlank(license.getName()) || StringUtils.isNotBlank(license.getUrl()))) {
this.markupDocBuilder.sectionTitleLevel2(LICENSE_INFORMATION);
if (StringUtils.isNotBlank(license.getName())) {
this.markupDocBuilder.textLine(LICENSE + license.getName());
}
if (StringUtils.isNotBlank(license.getUrl())) {
this.markupDocBuilder.textLine(LICENSE_URL + license.getUrl());
}
this.markupDocBuilder.newLine();
}
if(StringUtils.isNotBlank(info.getTermsOfService())){
this.markupDocBuilder.textLine(TERMS_OF_SERVICE + info.getTermsOfService());
this.markupDocBuilder.newLine();
}
this.markupDocBuilder.sectionTitleLevel2(URI_SCHEME);
if(StringUtils.isNotBlank(swagger.getHost())){
this.markupDocBuilder.textLine(HOST + swagger.getHost());
}
if(StringUtils.isNotBlank(swagger.getBasePath())){
this.markupDocBuilder.textLine(BASE_PATH + swagger.getBasePath());
}
if(CollectionUtils.isNotEmpty(swagger.getSchemes())){
List<String> schemes = new ArrayList<>();
for(Scheme scheme : swagger.getSchemes()){
schemes.add(scheme.toString());
}
this.markupDocBuilder.textLine(SCHEMES + StringUtils.join(schemes, ", "));
}
this.markupDocBuilder.newLine();
if(CollectionUtils.isNotEmpty(swagger.getTags())){
this.markupDocBuilder.sectionTitleLevel2(TAGS);
List<String> tags = new ArrayList<>();
for(Tag tag : swagger.getTags()){
String name = tag.getName();
String description = tag.getDescription();
if(StringUtils.isNoneBlank(description)){
tags.add(name + ": " + description);
}
tags.add(name);
}
this.markupDocBuilder.unorderedList(tags);
this.markupDocBuilder.newLine();
}
if(CollectionUtils.isNotEmpty(swagger.getConsumes())){
this.markupDocBuilder.sectionTitleLevel2(CONSUMES);
this.markupDocBuilder.unorderedList(swagger.getConsumes());
this.markupDocBuilder.newLine();
}
if(CollectionUtils.isNotEmpty(swagger.getProduces())){
this.markupDocBuilder.sectionTitleLevel2(PRODUCES);
this.markupDocBuilder.unorderedList(swagger.getProduces());
this.markupDocBuilder.newLine();
}
}
}

View File

@@ -1,54 +1,79 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.Operation;
import com.wordnik.swagger.models.Path;
import com.wordnik.swagger.models.Response;
import com.wordnik.swagger.models.Swagger;
import com.wordnik.swagger.models.parameters.Parameter;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import com.wordnik.swagger.models.properties.Property;
import io.github.robwin.markup.builder.MarkupLanguage;
import io.github.robwin.swagger2markup.utils.ParameterUtils;
import io.github.robwin.swagger2markup.utils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public class PathsDocument extends MarkupDocument {
private static final String VERSION = "Version: ";
private static final String DESCRIPTION_COLUMN = "Description";
private static final String DESCRIPTION = DESCRIPTION_COLUMN;
private static final String PATHS = "Paths";
private static final String PARAMETERS = "Parameters";
private static final String PRODUCES = "Produces";
private static final String CONSUMES = "Consumes";
private static final String RESPONSES = "Responses";
private static final String EXAMPLE_REQUEST = "Example request";
private static final String EXAMPLE_RESPONSE = "Example response";
private static final String NAME_COLUMN = "Name";
private static final String LOCATED_IN_COLUMN = "Located in";
private static final String CODE_COLUMN = "Code";
public static final String REQUEST_EXAMPLE_FILE_NAME = "request";
public static final String RESPONSE_EXAMPLE_FILE_NAME = "response";
private static final String TYPE_COLUMN = "Type";
private static final String HTTP_CODE_COLUMN = "HTTP Code";
private static final String REQUEST_EXAMPLE_FILE_NAME = "http-request";
private static final String RESPONSE_EXAMPLE_FILE_NAME = "http-response";
private static final String DESCRIPTION_FILE_NAME = "description";
private static final String PARAMETER = "Parameter";
private boolean examplesEnabled;
private String examplesFolderPath;
private boolean handWrittenDescriptionsEnabled;
private String descriptionsFolderPath;
public PathsDocument(Swagger swagger, MarkupLanguage markupLanguage, String examplesFolderPath){
public PathsDocument(Swagger swagger, MarkupLanguage markupLanguage, String examplesFolderPath, String descriptionsFolderPath){
super(swagger, markupLanguage);
if(StringUtils.isNotBlank(examplesFolderPath)){
this.examplesEnabled = true;
this.examplesFolderPath = examplesFolderPath;
}
if(StringUtils.isNotBlank(descriptionsFolderPath)){
this.handWrittenDescriptionsEnabled = true;
this.descriptionsFolderPath = descriptionsFolderPath + "/" + PATHS.toLowerCase();
}
if(examplesEnabled){
if (logger.isDebugEnabled()) {
logger.debug("Include examples is enabled.");
@@ -58,43 +83,39 @@ public class PathsDocument extends MarkupDocument {
logger.debug("Include examples is disabled.");
}
}
if(handWrittenDescriptionsEnabled){
if (logger.isDebugEnabled()) {
logger.debug("Include hand-written descriptions is enabled.");
}
}else{
if (logger.isDebugEnabled()) {
logger.debug("Include hand-written descriptions is disabled.");
}
}
}
@Override
public MarkupDocument build() throws IOException {
documentHeader(swagger.getInfo());
paths(swagger.getPaths());
paths();
return this;
}
/**
* Builds the document header
*
* @param info the Swagger Info
* Builds all paths of the Swagger model
*/
private void documentHeader(Info info) {
this.documentBuilder
.documentTitle(info.getTitle())
.textLine(info.getDescription())
.textLine(VERSION + info.getVersion())
.newLine();
}
/**
* Builds all paths of the Swagger file
*
* @param paths a Map of Swagger Paths
*/
private void paths(Map<String, Path> paths) throws IOException {
private void paths() throws IOException {
Map<String, Path> paths = swagger.getPaths();
if(MapUtils.isNotEmpty(paths)) {
//this.documentBuilder.sectionTitleLevel1(FEATURES);
this.markupDocBuilder.sectionTitleLevel1(PATHS);
for (Map.Entry<String, Path> entry : paths.entrySet()) {
Path path = entry.getValue();
path("GET", entry.getKey(), path.getGet());
path("PUT", entry.getKey(), path.getPut());
path("DELETE", entry.getKey(), path.getDelete());
path("POST", entry.getKey(), path.getPost());
path("PATCH", entry.getKey(), path.getPatch());
if(path != null) {
path("GET", entry.getKey(), path.getGet());
path("PUT", entry.getKey(), path.getPut());
path("DELETE", entry.getKey(), path.getDelete());
path("POST", entry.getKey(), path.getPost());
path("PATCH", entry.getKey(), path.getPatch());
}
}
}
}
@@ -114,53 +135,111 @@ public class PathsDocument extends MarkupDocument {
responsesSection(operation);
consumesSection(operation);
producesSection(operation);
tagsSection(operation);
examplesSection(operation);
}
}
private void pathTitle(String httpMethod, String resourcePath, Operation operation) {
String summary = operation.getSummary();
String title;
if(StringUtils.isNotBlank(summary)) {
title = summary;
this.documentBuilder.sectionTitleLevel1(title);
this.documentBuilder.listing(httpMethod + " " + resourcePath);
this.markupDocBuilder.sectionTitleLevel2(title);
this.markupDocBuilder.listing(httpMethod + " " + resourcePath);
}else{
title = httpMethod + " " + resourcePath;
this.documentBuilder.sectionTitleLevel1(title);
this.markupDocBuilder.sectionTitleLevel2(title);
}
if (logger.isInfoEnabled()) {
logger.info("Path processed: {}", title);
}
}
private void descriptionSection(Operation operation) {
String description = operation.getDescription();
if(StringUtils.isNotBlank(description)){
this.documentBuilder.sectionTitleLevel2(DESCRIPTION);
this.documentBuilder.paragraph(description);
private void descriptionSection(Operation operation) throws IOException {
if(handWrittenDescriptionsEnabled){
String summary = operation.getSummary();
if(StringUtils.isNotBlank(summary)) {
String operationFolder = summary.replace(".", "").replace(" ", "_").toLowerCase();
String description = handWrittenPathDescription(operationFolder, DESCRIPTION_FILE_NAME);
if(StringUtils.isNotBlank(description)){
this.markupDocBuilder.sectionTitleLevel3(DESCRIPTION);
this.markupDocBuilder.paragraph(description);
}else{
if (logger.isInfoEnabled()) {
logger.info("Hand-written description cannot be read. Trying to use description from Swagger source.");
}
pathDescription(operation);
}
}else{
if (logger.isInfoEnabled()) {
logger.info("Hand-written description cannot be read, because summary of operation is empty. Trying to use description from Swagger source.");
}
pathDescription(operation);
}
}else {
pathDescription(operation);
}
}
private void parametersSection(Operation operation) {
private void pathDescription(Operation operation) {
String description = operation.getDescription();
if (StringUtils.isNotBlank(description)) {
this.markupDocBuilder.sectionTitleLevel3(DESCRIPTION);
this.markupDocBuilder.paragraph(description);
}
}
private void parametersSection(Operation operation) throws IOException {
List<Parameter> parameters = operation.getParameters();
if(CollectionUtils.isNotEmpty(parameters)){
List<String> csvContent = new ArrayList<>();
csvContent.add(NAME_COLUMN + DELIMITER + LOCATED_IN_COLUMN + DELIMITER + DESCRIPTION_COLUMN + DELIMITER + REQUIRED_COLUMN);
List<String> headerAndContent = new ArrayList<>();
// Table header row
List<String> header = Arrays.asList(TYPE_COLUMN, NAME_COLUMN, DESCRIPTION_COLUMN, REQUIRED_COLUMN, SCHEMA_COLUMN);
headerAndContent.add(StringUtils.join(header, DELIMITER));
for(Parameter parameter : parameters){
csvContent.add(parameter.getName() + DELIMITER + parameter.getIn() + DELIMITER + parameter.getDescription() + DELIMITER + parameter.getRequired());
String type = ParameterUtils.getType(parameter, markupLanguage);
String parameterType = WordUtils.capitalize(parameter.getIn() + PARAMETER);
// Table content row
List<String> content = Arrays.asList(parameterType, parameter.getName(), parameterDescription(operation, parameter), Boolean.toString(parameter.getRequired()), type);
headerAndContent.add(StringUtils.join(content, DELIMITER));
}
this.documentBuilder.sectionTitleLevel2(PARAMETERS);
this.documentBuilder.tableWithHeaderRow(csvContent);
this.markupDocBuilder.sectionTitleLevel3(PARAMETERS);
this.markupDocBuilder.tableWithHeaderRow(headerAndContent);
}
}
private String parameterDescription(Operation operation, Parameter parameter) throws IOException {
String description;
if(handWrittenDescriptionsEnabled){
String summary = operation.getSummary();
String operationFolder = summary.replace(".", "").replace(" ", "_").toLowerCase();
String parameterName = parameter.getName();
if(StringUtils.isNotBlank(operationFolder) && StringUtils.isNotBlank(parameterName)) {
description = handWrittenPathDescription(operationFolder + "/" + parameterName, DESCRIPTION_FILE_NAME);
if(StringUtils.isBlank(description)) {
if (logger.isInfoEnabled()) {
logger.info("Hand-written description file cannot be read. Trying to use description from Swagger source.");
}
description = StringUtils.defaultString(parameter.getDescription());
}
}else{
if (logger.isInfoEnabled()) {
logger.info("Hand-written description file cannot be read, because summary of operation or name of parameter is empty. Trying to use description from Swagger source.");
}
description = StringUtils.defaultString(parameter.getDescription());
}
}else {
description = StringUtils.defaultString(parameter.getDescription());
}
return description;
}
private void consumesSection(Operation operation) {
List<String> consumes = operation.getConsumes();
if(CollectionUtils.isNotEmpty(consumes)){
this.documentBuilder.sectionTitleLevel2(CONSUMES);
this.documentBuilder.unorderedList(consumes);
this.markupDocBuilder.sectionTitleLevel3(CONSUMES);
this.markupDocBuilder.unorderedList(consumes);
}
}
@@ -168,8 +247,16 @@ public class PathsDocument extends MarkupDocument {
private void producesSection(Operation operation) {
List<String> produces = operation.getProduces();
if(CollectionUtils.isNotEmpty(produces)){
this.documentBuilder.sectionTitleLevel2(PRODUCES);
this.documentBuilder.unorderedList(produces);
this.markupDocBuilder.sectionTitleLevel3(PRODUCES);
this.markupDocBuilder.unorderedList(produces);
}
}
private void tagsSection(Operation operation) {
List<String> tags = operation.getTags();
if(CollectionUtils.isNotEmpty(tags)){
this.markupDocBuilder.sectionTitleLevel3(TAGS);
this.markupDocBuilder.unorderedList(tags);
}
}
@@ -184,49 +271,97 @@ public class PathsDocument extends MarkupDocument {
String summary = operation.getSummary();
if(StringUtils.isNotBlank(summary)) {
String exampleFolder = summary.replace(".", "").replace(" ", "_").toLowerCase();
example(EXAMPLE_REQUEST, exampleFolder, REQUEST_EXAMPLE_FILE_NAME);
example(EXAMPLE_RESPONSE, exampleFolder, RESPONSE_EXAMPLE_FILE_NAME);
String requestExample = example(exampleFolder, REQUEST_EXAMPLE_FILE_NAME);
if(StringUtils.isNotBlank(requestExample)){
this.markupDocBuilder.sectionTitleLevel3(EXAMPLE_REQUEST);
this.markupDocBuilder.paragraph(requestExample);
}
String responseExample = example(exampleFolder, RESPONSE_EXAMPLE_FILE_NAME);
if(StringUtils.isNotBlank(responseExample)){
this.markupDocBuilder.sectionTitleLevel3(EXAMPLE_RESPONSE);
this.markupDocBuilder.paragraph(responseExample);
}
}else{
if (logger.isWarnEnabled()) {
logger.warn("Example file cannot be read, because summary of operation is empty.");
}
}
}
}
/**
* Builds a concrete example
* Reads an example
*
* @param title the title of the example
* @param exampleFolder the name of the folder where the example file resides
* @param exampleFileName the name of the example file
* @return the content of the file
* @throws IOException
*/
private void example(String title, String exampleFolder, String exampleFileName) throws IOException {
private String example(String exampleFolder, String exampleFileName) throws IOException {
for (String fileNameExtension : markupLanguage.getFileNameExtensions()) {
java.nio.file.Path path = Paths.get(examplesFolderPath, exampleFolder, exampleFileName + fileNameExtension);
if (Files.isReadable(path)) {
this.documentBuilder.sectionTitleLevel2(title);
this.documentBuilder.paragraph(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim());
if (logger.isInfoEnabled()) {
logger.info("Example file processed: {}", path);
}
break;
return FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim();
} else {
if (logger.isDebugEnabled()) {
logger.debug("Example file is not readable: {}", path);
}
}
}
if (logger.isWarnEnabled()) {
logger.info("No example file found with correct file name extension in folder: {}", Paths.get(examplesFolderPath, exampleFolder));
}
return null;
}
/**
* Reads a hand-written description
*
* @param descriptionFolder the name of the folder where the description file resides
* @param descriptionFileName the name of the description file
* @return the content of the file
* @throws IOException
*/
private String handWrittenPathDescription(String descriptionFolder, String descriptionFileName) throws IOException {
for (String fileNameExtension : markupLanguage.getFileNameExtensions()) {
java.nio.file.Path path = Paths.get(descriptionsFolderPath, descriptionFolder, descriptionFileName + fileNameExtension);
if (Files.isReadable(path)) {
if (logger.isInfoEnabled()) {
logger.info("Description file processed: {}", path);
}
return FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim();
} else {
if (logger.isDebugEnabled()) {
logger.debug("Description file is not readable: {}", path);
}
}
}
if (logger.isWarnEnabled()) {
logger.info("No description file found with correct file name extension in folder: {}", Paths.get(descriptionsFolderPath, descriptionFolder));
}
return null;
}
private void responsesSection(Operation operation) {
Map<String, Response> responses = operation.getResponses();
if(MapUtils.isNotEmpty(responses)){
List<String> csvContent = new ArrayList<>();
csvContent.add(CODE_COLUMN + DELIMITER + DESCRIPTION_COLUMN);
csvContent.add(HTTP_CODE_COLUMN + DELIMITER + DESCRIPTION_COLUMN + DELIMITER + SCHEMA_COLUMN);
for(Map.Entry<String, Response> entry : responses.entrySet()){
Response response = entry.getValue();
csvContent.add(entry.getKey() + DELIMITER + response.getDescription());
if(response.getSchema() != null){
Property property = response.getSchema();
String type = PropertyUtils.getType(property, markupLanguage);
csvContent.add(entry.getKey() + DELIMITER + response.getDescription() + DELIMITER + type);
}else{
csvContent.add(entry.getKey() + DELIMITER + response.getDescription() + DELIMITER + "No Content");
}
}
this.documentBuilder.sectionTitleLevel2(RESPONSES);
this.documentBuilder.tableWithHeaderRow(csvContent);
this.markupDocBuilder.sectionTitleLevel3(RESPONSES);
this.markupDocBuilder.tableWithHeaderRow(csvContent);
}
}

View File

@@ -1,97 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* @author Robert Winkler
*/
public abstract class AbstractDocumentBuilder implements DocumentBuilder {
protected StringBuilder documentBuilder = new StringBuilder();
protected String newLine = System.getProperty("line.separator");
protected Logger logger = LoggerFactory.getLogger(getClass());
protected void documentTitle(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel1(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel2(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel3(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
@Override
public DocumentBuilder textLine(String text){
documentBuilder.append(text).append(newLine);
return this;
}
protected void paragraph(Markup markup, String text){
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(newLine);
}
protected void listing(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void delimitedTextLine(Markup markup, String text){
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(markup).append(newLine).append(newLine);
}
protected void preserveLineBreaks(Markup markup){
documentBuilder.append(markup).append(newLine);
}
protected void boldTextLine(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void italicTextLine(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void unorderedList(Markup markup, List<String> list){
for(String listEntry : list){
documentBuilder.append(markup).append(listEntry).append(newLine);
}
documentBuilder.append(newLine);
}
@Override
public DocumentBuilder newLine(){
documentBuilder.append(newLine);
return this;
}
@Override
public String toString(){
return documentBuilder.toString();
}
@Override
public void writeToFile(String directory, String fileNameWithExtension, Charset charset) throws IOException {
Files.createDirectories(Paths.get(directory));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(directory, fileNameWithExtension), charset)){
writer.write(documentBuilder.toString());
}
if (logger.isInfoEnabled()) {
logger.info("{} was written to: {}", fileNameWithExtension, directory);
}
documentBuilder = new StringBuilder();
}
}

View File

@@ -1,51 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* @author Robert Winkler
*/
public interface DocumentBuilder {
DocumentBuilder documentTitle(String title);
DocumentBuilder sectionTitleLevel1(String title);
DocumentBuilder sectionTitleLevel2(String title);
DocumentBuilder sectionTitleLevel3(String title);
DocumentBuilder textLine(String text);
DocumentBuilder paragraph(String text);
DocumentBuilder listing(String text);
DocumentBuilder source(String text, String language);
DocumentBuilder boldTextLine(String text);
DocumentBuilder italicTextLine(String text);
DocumentBuilder unorderedList(List<String> list);
DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV);
DocumentBuilder newLine();
/**
* Returns a string representation of the document.
*/
String toString();
/**
* Writes the content of the builder to a file and clears the builder.
*
* @param directory the directory where the generated file should be stored
* @param fileName the name of the file
* @param charset the the charset to use for encoding
* @throws IOException if the file cannot be written
*/
void writeToFile(String directory, String fileName, Charset charset) throws IOException;
}

View File

@@ -1,25 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import io.github.robwin.swagger2markup.builder.markup.asciidoc.AsciiDocBuilder;
import io.github.robwin.swagger2markup.builder.markup.markdown.MarkdownBuilder;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public final class DocumentBuilders {
private DocumentBuilders(){};
public static DocumentBuilder documentBuilder(MarkupLanguage markupLanguage){
switch(markupLanguage){
case MARKDOWN: return new MarkdownBuilder();
case ASCIIDOC: return new AsciiDocBuilder();
default: return new AsciiDocBuilder();
}
}
}

View File

@@ -1,12 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public interface Markup {
public String toString();
}

View File

@@ -1,25 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import java.util.Arrays;
import java.util.List;
/**
* @author Robert Winkler
*/
public enum MarkupLanguage {
ASCIIDOC(".adoc,.asciidoc"),
MARKDOWN(".md,.markdown");
private final String fileNameExtensions;
/**
* @param fileNameExtensions file name suffix
*/
private MarkupLanguage(final String fileNameExtensions) {
this.fileNameExtensions = fileNameExtensions;
}
public List<String> getFileNameExtensions() {
return Arrays.asList(fileNameExtensions.split(","));
}
}

View File

@@ -1,37 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.asciidoc;
import io.github.robwin.swagger2markup.builder.markup.Markup;
/**
* @author Robert Winkler
*/
public enum AsciiDoc implements Markup {
LABELED(":: "),
TABLE("|==="),
LISTING("----"),
HARDBREAKS(":hardbreaks:"),
DOCUMENT_TITLE("= "),
SECTION_TITLE_LEVEL1("== "),
SECTION_TITLE_LEVEL2("=== "),
SECTION_TITLE_LEVEL3("==== "),
BOLD("*"),
ITALIC("*"),
LIST_ENTRY("* ");
private final String markup;
/**
* @param markup AsciiDoc markup
*/
private AsciiDoc(final String markup) {
this.markup = markup;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return markup;
}
}

View File

@@ -1,93 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.asciidoc;
import io.github.robwin.swagger2markup.builder.markup.AbstractDocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* @author Robert Winkler
*/
public class AsciiDocBuilder extends AbstractDocumentBuilder{
@Override
public DocumentBuilder documentTitle(String title){
documentTitle(AsciiDoc.DOCUMENT_TITLE, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel1(String title){
sectionTitleLevel1(AsciiDoc.SECTION_TITLE_LEVEL1, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel2(String title){
sectionTitleLevel2(AsciiDoc.SECTION_TITLE_LEVEL2, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel3(String title){
sectionTitleLevel3(AsciiDoc.SECTION_TITLE_LEVEL3, title);
return this;
}
@Override
public DocumentBuilder paragraph(String text){
paragraph(AsciiDoc.HARDBREAKS, text);
return this;
}
@Override
public DocumentBuilder listing(String text){
listing(AsciiDoc.LISTING, text);
return this;
}
@Override
public DocumentBuilder boldTextLine(String text){
boldTextLine(AsciiDoc.BOLD, text);
return this;
}
@Override
public DocumentBuilder italicTextLine(String text) {
italicTextLine(AsciiDoc.ITALIC, text);
return this;
}
@Override
public DocumentBuilder unorderedList(List<String> list){
unorderedList(AsciiDoc.LIST_ENTRY, list);
return this;
}
@Override
public DocumentBuilder source(String text, String language){
documentBuilder.append(String.format("[source,%s]", language)).append(newLine);
listing(AsciiDoc.LISTING, text);
return this;
}
@Override
public DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV){
documentBuilder.append("[format=\"csv\", options=\"header\"]").append(newLine);
documentBuilder.append(AsciiDoc.TABLE).append(newLine);
for(String row : rowsInCSV){
documentBuilder.append(row).append(newLine);
}
documentBuilder.append(AsciiDoc.TABLE).append(newLine).append(newLine);
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".adoc";
super.writeToFile(directory, fileNameWithExtension, charset);
}
}

View File

@@ -1,41 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.markdown;
import io.github.robwin.swagger2markup.builder.markup.Markup;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public enum Markdown implements Markup {
HARDBREAKS(""),
TABLE_COLUMN("|"),
TABLE_ROW("-"),
LISTING("```"),
DOCUMENT_TITLE("# "),
SECTION_TITLE_LEVEL1("## "),
SECTION_TITLE_LEVEL2("### "),
SECTION_TITLE_LEVEL3("### "),
BOLD("**"),
ITALIC("*"),
LIST_ENTRY("* ");
private final String markup;
/**
* @param markup AsciiDoc markup
*/
private Markdown(final String markup) {
this.markup = markup;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return markup;
}
}

View File

@@ -1,120 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.markdown;
import io.github.robwin.swagger2markup.builder.markup.AbstractDocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public class MarkdownBuilder extends AbstractDocumentBuilder
{
@Override
public DocumentBuilder documentTitle(String title){
documentTitle(Markdown.DOCUMENT_TITLE, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel1(String title){
sectionTitleLevel1(Markdown.SECTION_TITLE_LEVEL1, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel2(String title){
sectionTitleLevel2(Markdown.SECTION_TITLE_LEVEL2, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel3(String title){
sectionTitleLevel3(Markdown.SECTION_TITLE_LEVEL3, title);
return this;
}
@Override
public DocumentBuilder paragraph(String text){
paragraph(Markdown.HARDBREAKS, text);
return this;
}
@Override
public DocumentBuilder listing(String text){
listing(Markdown.LISTING, text);
return this;
}
@Override
public DocumentBuilder source(String text, String language){
documentBuilder.append(Markdown.LISTING).append(language).append(newLine).
append(text).append(newLine).
append(Markdown.LISTING).append(newLine).append(newLine);
return this;
}
@Override
public DocumentBuilder boldTextLine(String text){
boldTextLine(Markdown.BOLD, text);
return this;
}
@Override
public DocumentBuilder italicTextLine(String text) {
italicTextLine(Markdown.ITALIC, text);
return this;
}
@Override
public DocumentBuilder unorderedList(List<String> list){
unorderedList(Markdown.LIST_ENTRY, list);
return this;
}
@Override
public DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV){
String headersInCSV = rowsInCSV.get(0);
List<String> contentRowsInCSV = rowsInCSV.subList(1, rowsInCSV.size());
List<String> headers = Arrays.asList(headersInCSV.split(","));
// Header
documentBuilder.append(Markdown.TABLE_COLUMN);
for(String header : headers){
documentBuilder.append(header).append(Markdown.TABLE_COLUMN);
}
newLine();
// Header/Content separator
documentBuilder.append(Markdown.TABLE_COLUMN);
for(String header : headers){
for(int i = 1; i<5; i++) {
documentBuilder.append(Markdown.TABLE_ROW);
}
documentBuilder.append(Markdown.TABLE_COLUMN);
}
newLine();
// Content
for(String contentRow : contentRowsInCSV){
documentBuilder.append(Markdown.TABLE_COLUMN);
List<String> columns = Arrays.asList(contentRow.split(","));
for(String columnText : columns){
documentBuilder.append(columnText).append(Markdown.TABLE_COLUMN);
}
newLine();
}
newLine().newLine();
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".md";
super.writeToFile(directory, fileNameWithExtension, charset);
}
}

View File

@@ -0,0 +1,45 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.utils;
import com.wordnik.swagger.models.ArrayModel;
import com.wordnik.swagger.models.Model;
import com.wordnik.swagger.models.ModelImpl;
import com.wordnik.swagger.models.RefModel;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.lang3.Validate;
public final class ModelUtils {
public static String getType(Model model, MarkupLanguage markupLanguage) {
Validate.notNull(model, "model must not be null!");
if (model instanceof ModelImpl) {
return ((ModelImpl) model).getType();
} else if (model instanceof RefModel) {
switch (markupLanguage){
case ASCIIDOC: return "<<" + ((RefModel) model).getSimpleRef() + ">>";
default: return ((RefModel) model).getSimpleRef();
}
} else if (model instanceof ArrayModel) {
ArrayModel arrayModel = ((ArrayModel) model);
return PropertyUtils.getType(arrayModel.getItems(), markupLanguage) + " " + arrayModel.getType();
}
return "NOT FOUND";
}
}

View File

@@ -0,0 +1,116 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.utils;
import com.wordnik.swagger.models.Model;
import com.wordnik.swagger.models.parameters.*;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import java.util.List;
public final class ParameterUtils {
public static String getType(Parameter parameter, MarkupLanguage markupLanguage){
Validate.notNull(parameter, "property must not be null!");
String type = "NOT FOUND";
if(parameter instanceof BodyParameter){
BodyParameter bodyParameter = (BodyParameter)parameter;
Model model = bodyParameter.getSchema();
type = ModelUtils.getType(model, markupLanguage);
}
else if(parameter instanceof PathParameter){
PathParameter pathParameter = (PathParameter)parameter;
type = getTypeWithFormat(pathParameter.getType(), pathParameter.getFormat());
}
else if(parameter instanceof QueryParameter){
QueryParameter queryParameter = (QueryParameter)parameter;
List<String> enums = queryParameter.getEnum();
if(CollectionUtils.isNotEmpty(enums)){
type = "enum" + " (" + StringUtils.join(enums, ", ") + ")";
}else{
type = getTypeWithFormat(queryParameter.getType(), queryParameter.getFormat());
}
if(type.equals("array")){
String collectionFormat = queryParameter.getCollectionFormat();
type = collectionFormat + " " + PropertyUtils.getType(queryParameter.getItems(), markupLanguage) + " " + type;
}
}
else if(parameter instanceof HeaderParameter){
HeaderParameter headerParameter = (HeaderParameter)parameter;
List<String> enums = headerParameter.getEnum();
if(CollectionUtils.isNotEmpty(enums)){
type = "enum" + " (" + StringUtils.join(enums, ", ") + ")";
}else{
type = getTypeWithFormat(headerParameter.getType(), headerParameter.getFormat());
}
if(type.equals("array")){
String collectionFormat = headerParameter.getCollectionFormat();
type = collectionFormat + " " + PropertyUtils.getType(headerParameter.getItems(), markupLanguage) + " " + type;
}
}
else if(parameter instanceof FormParameter){
FormParameter formParameter = (FormParameter)parameter;
List<String> enums = formParameter.getEnum();
if(CollectionUtils.isNotEmpty(enums)){
type = "enum" + " (" + StringUtils.join(enums, ", ") + ")";
}else{
type = getTypeWithFormat(formParameter.getType(), formParameter.getFormat());
}
if(type.equals("array")){
String collectionFormat = formParameter.getCollectionFormat();
type = collectionFormat + " " + PropertyUtils.getType(formParameter.getItems(), markupLanguage) + " " + type;
}
}
else if(parameter instanceof CookieParameter){
CookieParameter cookieParameter = (CookieParameter)parameter;
List<String> enums = cookieParameter.getEnum();
if(CollectionUtils.isNotEmpty(enums)){
type = "enum" + " (" + StringUtils.join(enums, ", ") + ")";
}else{
type = getTypeWithFormat(cookieParameter.getType(), cookieParameter.getFormat());
}
if(type.equals("array")){
String collectionFormat = cookieParameter.getCollectionFormat();
type = collectionFormat + " " + PropertyUtils.getType(cookieParameter.getItems(), markupLanguage) + " " + type;
}
}
else if(parameter instanceof RefParameter){
RefParameter refParameter = (RefParameter)parameter;
switch (markupLanguage){
case ASCIIDOC: return "<<" + refParameter.getSimpleRef() + ">>";
default: return refParameter.getSimpleRef();
}
}
return type;
}
private static String getTypeWithFormat(String typeWithoutFormat, String format) {
String type;
if(StringUtils.isNotBlank(format)){
type = typeWithoutFormat + " (" + format + ")";
}else{
type = typeWithoutFormat;
}
return type;
}
}

View File

@@ -0,0 +1,65 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup.utils;
import com.wordnik.swagger.models.properties.ArrayProperty;
import com.wordnik.swagger.models.properties.Property;
import com.wordnik.swagger.models.properties.RefProperty;
import com.wordnik.swagger.models.properties.StringProperty;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import java.util.List;
public final class PropertyUtils {
public static String getType(Property property, MarkupLanguage markupLanguage){
Validate.notNull(property, "property must not be null!");
String type;
if(property instanceof RefProperty){
RefProperty refProperty = (RefProperty)property;
switch (markupLanguage){
case ASCIIDOC: return "<<" + refProperty.getSimpleRef() + ">>";
default: return refProperty.getSimpleRef();
}
}else if(property instanceof ArrayProperty){
ArrayProperty arrayProperty = (ArrayProperty)property;
Property items = arrayProperty.getItems();
type = getType(items, markupLanguage) + " " + arrayProperty.getType();
}else if(property instanceof StringProperty){
StringProperty stringProperty = (StringProperty)property;
List<String> enums = stringProperty.getEnum();
if(CollectionUtils.isNotEmpty(enums)){
type = "enum" + " (" + StringUtils.join(enums, ", ") + ")";
}else{
type = property.getType();
}
}
else{
if(StringUtils.isNotBlank(property.getFormat())){
type = property.getType() + " (" + property.getFormat() + ")";
}else{
type = property.getType();
}
}
return type;
}
}

View File

@@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>
</configuration>

View File

@@ -1,28 +1,120 @@
/*
*
* Copyright 2015 Robert Winkler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package io.github.robwin.swagger2markup;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import static java.util.Arrays.asList;
import static org.assertj.core.api.BDDAssertions.assertThat;
/**
* @author Robert Winkler
*/
public class Swagger2MarkupConverterTest {
@Test
public void testSwagger2AsciiDocConverter() throws IOException {
public void testSwagger2AsciiDocConversion() throws IOException {
//Given
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
File outputDirectory = new File("build/docs/asciidoc/generated");
FileUtils.deleteQuietly(outputDirectory);
Swagger2MarkupConverter.from(file.getAbsolutePath()).
withMarkupLanguage(MarkupLanguage.MARKDOWN).
withExamples("docs").withSchemas("docs/schemas").build()
.intoFolder("src/docs/markdown");
Swagger2MarkupConverter.from(file.getAbsolutePath()).
withExamples("docs").withSchemas("docs/schemas").build()
.intoFolder("src/docs/asciidoc");
//When
Swagger2MarkupConverter.from(file.getAbsolutePath()).build()
.intoFolder(outputDirectory.getAbsolutePath());
//Then
String[] directories = outputDirectory.list();
assertThat(directories).hasSize(3).containsAll(asList("definitions.adoc", "overview.adoc", "paths.adoc"));
}
@Test
public void testSwagger2AsciiDocConversionWithDescriptionsAndExamples() throws IOException {
//Given
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
File outputDirectory = new File("build/docs/asciidoc/generated");
FileUtils.deleteQuietly(outputDirectory);
//When
Swagger2MarkupConverter.from(file.getAbsolutePath()).withDescriptions("src/docs/asciidoc")
.withExamples("src/docs/asciidoc/paths").build()
.intoFolder(outputDirectory.getAbsolutePath());
//Then
String[] directories = outputDirectory.list();
assertThat(directories).hasSize(3).containsAll(asList("definitions.adoc", "overview.adoc", "paths.adoc"));
}
@Test
public void testSwagger2MarkdownConversion() throws IOException {
//Given
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
File outputDirectory = new File("build/docs/markdown/generated");
FileUtils.deleteQuietly(outputDirectory);
//When
Swagger2MarkupConverter.from(file.getAbsolutePath()).
withMarkupLanguage(MarkupLanguage.MARKDOWN).build()
.intoFolder(outputDirectory.getAbsolutePath());
//Then
String[] directories = outputDirectory.list();
assertThat(directories).hasSize(3).containsAll(asList("definitions.md", "overview.md", "paths.md"));
}
@Test
public void testSwagger2MarkdownConversionWithDescriptions() throws IOException {
//Given
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
File outputDirectory = new File("build/docs/markdown/generated");
FileUtils.deleteQuietly(outputDirectory);
//When
Swagger2MarkupConverter.from(file.getAbsolutePath()).withDescriptions("src/docs/markdown").
withMarkupLanguage(MarkupLanguage.MARKDOWN).build()
.intoFolder(outputDirectory.getAbsolutePath());
//Then
String[] directories = outputDirectory.list();
assertThat(directories).hasSize(3).containsAll(asList("definitions.md", "overview.md", "paths.md"));
}
/*
@Test
public void testSwagger2HtmlConversion() throws IOException {
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
String asciiDoc = Swagger2MarkupConverter.from(file.getAbsolutePath()).build().asString();
String path = "build/docs/generated/asciidocAsString";
Files.createDirectories(Paths.get(path));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(path, "swagger.adoc"), StandardCharsets.UTF_8)){
writer.write(asciiDoc); }
String asciiDocAsHtml = Asciidoctor.Factory.create().convert(asciiDoc,
OptionsBuilder.options().backend("html5").headerFooter(true).safe(SafeMode.UNSAFE).docType("book").attributes(AttributesBuilder.attributes()
.tableOfContents(true).tableOfContents(Placement.LEFT).sectionNumbers(true).hardbreaks(true).setAnchors(true).attribute("sectlinks")));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(path, "swagger.html"), StandardCharsets.UTF_8)){
writer.write(asciiDocAsHtml);
}
}
*/
}

View File

@@ -1,26 +0,0 @@
package io.github.robwin.swagger2markup.builder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilders;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public class DocumentBuilderTest {
@Test
public void testToFile() throws IOException {
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.ASCIIDOC);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
}
}

View File

@@ -3,7 +3,7 @@
"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",
"title": "Swagger Petstore API",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "apiteam@wordnik.com"
@@ -770,6 +770,7 @@
}
},
"Pet": {
"description" : "Test description",
"required": [
"name",
"photoUrls"