Skip to content

Commit 90880a0

Browse files
author
Robert Winkler
committed
Added possibility to write object definitions to separate files. Issue Swagger2Markup#19
1 parent 675dd76 commit 90880a0

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

README.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= Swagger2Markup
22
:author: Robert Winkler
3-
:version: 0.6.2
3+
:version: 0.6.3
44
:hardbreaks:
55

66
image:https://travis-ci.org/RobWin/swagger2markup.svg?branch=master["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://img.shields.io/badge/Twitter-rbrtwnklr-blue.svg["Twitter", link="https://twitter.com/rbrtwnklr"] 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"]
@@ -43,7 +43,7 @@ The project is published in JCenter and Maven Central.
4343
<dependency>
4444
<groupId>io.github.robwin</groupId>
4545
<artifactId>swagger2markup</artifactId>
46-
<version>0.6.2</version>
46+
<version>0.6.3</version>
4747
</dependency>
4848
----
4949

@@ -55,7 +55,7 @@ repositories {
5555
jcenter()
5656
}
5757
58-
compile "io.github.robwin:swagger2markup:0.6.2"
58+
compile "io.github.robwin:swagger2markup:0.6.3"
5959
----
6060

6161
=== Using Swagger2Markup

RELEASENOTES.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@
5151

5252
== Version 0.6.2
5353
* curl-request.adoc from spring-restdocs is also added to the example chapters
54+
55+
== Version 0.6.3
56+
* Added possibility to write object definitions to separate files. Issue #19

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
}
1414
}
1515
description = 'swagger2markup Build'
16-
version = '0.6.2'
16+
version = '0.6.3'
1717
group = 'io.github.robwin'
1818

1919
apply plugin: 'java'

src/main/java/io/github/robwin/swagger2markup/Swagger2MarkupConverter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Swagger2MarkupConverter {
4646
private final String examplesFolderPath;
4747
private final String schemasFolderPath;
4848
private final String descriptionsFolderPath;
49-
private final boolean isSplitDescriptions;
49+
private final boolean separatedDefinitions;
5050
private static final String OVERVIEW_DOCUMENT = "overview";
5151
private static final String PATHS_DOCUMENT = "paths";
5252
private static final String DEFINITIONS_DOCUMENT = "definitions";
@@ -57,14 +57,15 @@ public class Swagger2MarkupConverter {
5757
* @param examplesFolderPath the folderPath where examples are stored
5858
* @param schemasFolderPath the folderPath where (XML, JSON)-Schema files are stored
5959
* @param descriptionsFolderPath the folderPath where descriptions are stored
60+
* @param separatedDefinitions create separate definition files for each model definition.
6061
*/
61-
Swagger2MarkupConverter(MarkupLanguage markupLanguage, Swagger swagger, String examplesFolderPath, String schemasFolderPath, String descriptionsFolderPath, boolean isSplitDescriptions){
62+
Swagger2MarkupConverter(MarkupLanguage markupLanguage, Swagger swagger, String examplesFolderPath, String schemasFolderPath, String descriptionsFolderPath, boolean separatedDefinitions){
6263
this.markupLanguage = markupLanguage;
6364
this.swagger = swagger;
6465
this.examplesFolderPath = examplesFolderPath;
6566
this.schemasFolderPath = schemasFolderPath;
6667
this.descriptionsFolderPath = descriptionsFolderPath;
67-
this.isSplitDescriptions = isSplitDescriptions;
68+
this.separatedDefinitions = separatedDefinitions;
6869
}
6970

7071
/**
@@ -137,19 +138,19 @@ public String asString() throws IOException{
137138
}
138139

139140
/**
140-
* Writes a file for the Paths (API) and a file for the Definitions (Model)
141+
* Builds all documents and writes them to a directory
141142
142143
* @param directory the directory where the generated file should be stored
143144
* @throws IOException if a file cannot be written
144145
*/
145146
private void buildDocuments(String directory) throws IOException {
146147
new OverviewDocument(swagger, markupLanguage).build().writeToFile(directory, OVERVIEW_DOCUMENT, StandardCharsets.UTF_8);
147148
new PathsDocument(swagger, markupLanguage, examplesFolderPath, descriptionsFolderPath).build().writeToFile(directory, PATHS_DOCUMENT, StandardCharsets.UTF_8);
148-
new DefinitionsDocument(swagger, markupLanguage, schemasFolderPath, descriptionsFolderPath, isSplitDescriptions, directory).build().writeToFile(directory, DEFINITIONS_DOCUMENT, StandardCharsets.UTF_8);
149+
new DefinitionsDocument(swagger, markupLanguage, schemasFolderPath, descriptionsFolderPath, separatedDefinitions, directory).build().writeToFile(directory, DEFINITIONS_DOCUMENT, StandardCharsets.UTF_8);
149150
}
150151

151152
/**
152-
* Returns a file for the Paths (API) and a file for the Definitions (Model)
153+
* Returns all documents as a String
153154
154155
* @return a the document as a String
155156
*/
@@ -165,7 +166,7 @@ public static class Builder{
165166
private String examplesFolderPath;
166167
private String schemasFolderPath;
167168
private String descriptionsFolderPath;
168-
private boolean isSplitDescriptions;
169+
private boolean separatedDefinitions;
169170
private MarkupLanguage markupLanguage = MarkupLanguage.ASCIIDOC;
170171

171172
/**
@@ -190,7 +191,7 @@ public static class Builder{
190191
}
191192

192193
public Swagger2MarkupConverter build(){
193-
return new Swagger2MarkupConverter(markupLanguage, swagger, examplesFolderPath, schemasFolderPath, descriptionsFolderPath, isSplitDescriptions);
194+
return new Swagger2MarkupConverter(markupLanguage, swagger, examplesFolderPath, schemasFolderPath, descriptionsFolderPath, separatedDefinitions);
194195
}
195196

196197
/**
@@ -216,11 +217,11 @@ public Builder withDescriptions(String descriptionsFolderPath){
216217
}
217218

218219
/**
219-
* In addition to definitions file, also create separate definition files for each entity.
220+
* In addition to the definitions file, also create separate definition files for each model definition.
220221
* @return the Swagger2MarkupConverter.Builder
221222
*/
222-
public Builder withSplitDescriptions() {
223-
this.isSplitDescriptions = true;
223+
public Builder withSeparatedDefinitions() {
224+
this.separatedDefinitions = true;
224225
return this;
225226
}
226227

src/main/java/io/github/robwin/swagger2markup/builder/document/DefinitionsDocument.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class DefinitionsDocument extends MarkupDocument {
5454
private String schemasFolderPath;
5555
private boolean handWrittenDescriptionsEnabled;
5656
private String descriptionsFolderPath;
57-
private boolean isSplitDescriptions;
57+
private boolean separatedDefinitionsEnabled;
5858
private String outputDirectory;
5959

60-
public DefinitionsDocument(Swagger swagger, MarkupLanguage markupLanguage, String schemasFolderPath, String descriptionsFolderPath, boolean isSplitDescriptions, String outputDirectory){
60+
public DefinitionsDocument(Swagger swagger, MarkupLanguage markupLanguage, String schemasFolderPath, String descriptionsFolderPath, boolean separatedDefinitionsEnabled, String outputDirectory){
6161
super(swagger, markupLanguage);
6262
if(StringUtils.isNotBlank(schemasFolderPath)){
6363
this.schemasEnabled = true;
@@ -85,14 +85,18 @@ public DefinitionsDocument(Swagger swagger, MarkupLanguage markupLanguage, Strin
8585
logger.debug("Include hand-written descriptions is disabled.");
8686
}
8787
}
88-
this.isSplitDescriptions = isSplitDescriptions;
89-
this.outputDirectory = outputDirectory;
90-
if (isSplitDescriptions) {
91-
Validate.notEmpty(outputDirectory, "Output directory is required for descriptions!");
88+
this.separatedDefinitionsEnabled = separatedDefinitionsEnabled;
89+
if(this.separatedDefinitionsEnabled){
9290
if (logger.isDebugEnabled()) {
93-
logger.debug("Include split descriptions for each model object in path: " + outputDirectory);
91+
logger.debug("Create separated definition files is enabled.");
92+
}
93+
Validate.notEmpty(outputDirectory, "Output directory is required for separated definition files!");
94+
}else{
95+
if (logger.isDebugEnabled()) {
96+
logger.debug("Create separated definition files is disabled.");
9497
}
9598
}
99+
this.outputDirectory = outputDirectory;
96100
}
97101

98102
@Override
@@ -116,7 +120,7 @@ private void definitions(Map<String, Model> definitions, MarkupDocBuilder docBui
116120
if (checkThatDefinitionIsNotInIgnoreList(definitionName)) {
117121
definition(definitionName, definitionsEntry.getValue(), docBuilder);
118122
definitionSchema(definitionName, docBuilder);
119-
if (isSplitDescriptions) {
123+
if (separatedDefinitionsEnabled) {
120124
MarkupDocBuilder defDocBuilder = MarkupDocBuilders.documentBuilder(markupLanguage);
121125
definition(definitionName, definitionsEntry.getValue(), defDocBuilder);
122126
definitionSchema(definitionName, defDocBuilder);

src/test/java/io/github/robwin/swagger2markup/Swagger2MarkupConverterTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ public void testSwagger2MarkdownConversionWithDescriptions() throws IOException
103103
}
104104

105105
@Test
106-
public void testSwagger2AsciiDocConversionWithSplitDescriptions() throws IOException {
106+
public void testSwagger2AsciiDocConversionWithSeparatedDefinitions() throws IOException {
107107
//Given
108108
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
109109
File outputDirectory = new File("build/docs/asciidoc/generated");
110110
FileUtils.deleteQuietly(outputDirectory);
111111

112112
//When
113-
Swagger2MarkupConverter.from(file.getAbsolutePath()).withSplitDescriptions().build()
113+
Swagger2MarkupConverter.from(file.getAbsolutePath()).withSeparatedDefinitions().build()
114114
.intoFolder(outputDirectory.getAbsolutePath());
115115

116116
//Then
@@ -122,6 +122,27 @@ public void testSwagger2AsciiDocConversionWithSplitDescriptions() throws IOExcep
122122
.contains(new String(Files.readAllBytes(Paths.get(outputDirectory + File.separator + "user.adoc"))));
123123
}
124124

125+
@Test
126+
public void testSwagger2MarkdownConversionWithSeparatedDefinitions() throws IOException {
127+
//Given
128+
File file = new File(Swagger2MarkupConverterTest.class.getResource("/json/swagger.json").getFile());
129+
File outputDirectory = new File("build/docs/asciidoc/generated");
130+
FileUtils.deleteQuietly(outputDirectory);
131+
132+
//When
133+
Swagger2MarkupConverter.from(file.getAbsolutePath()).withSeparatedDefinitions().
134+
withMarkupLanguage(MarkupLanguage.MARKDOWN).build()
135+
.intoFolder(outputDirectory.getAbsolutePath());
136+
137+
//Then
138+
String[] directories = outputDirectory.list();
139+
assertThat(directories).hasSize(8).containsAll(
140+
asList("definitions.md", "overview.md", "paths.md",
141+
"user.md", "category.md", "pet.md", "tag.md", "order.md"));
142+
assertThat(new String(Files.readAllBytes(Paths.get(outputDirectory + File.separator + "definitions.md"))))
143+
.contains(new String(Files.readAllBytes(Paths.get(outputDirectory + File.separator + "user.md"))));
144+
}
145+
125146
/*
126147
@Test
127148
public void testSwagger2HtmlConversion() throws IOException {

0 commit comments

Comments
 (0)