Skip to content

Commit 2f33522

Browse files
cascer1RobWin
authored andcommitted
Issue #214 Added support for page breaks (#226)
* Added support for page breaks * Added documentation and config file support for page breaks and regex
1 parent 76c54a7 commit 2f33522

File tree

14 files changed

+1295
-33
lines changed

14 files changed

+1295
-33
lines changed

RELEASENOTES.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,12 @@
114114
* Issue #205: Fixed the option to influence resource ordering
115115
* Issue #198: Chinese chinese language encoding
116116
* Issue #207: Properties that start with an underscore are displayed correctly now
117-
* Refactored Swagger2Markup to use a Component-Based design. A document can be composed of components and components can be composed of other components.
117+
* Refactored Swagger2Markup to use a Component-Based design. A document can be composed of components and components can be composed of other components.
118+
119+
=== Version 1.1.2
120+
* Issue #214: Add page break locations
121+
* Issue #223: Improve example rendering
122+
* Issue #215: Add ability to group operations by RegEx
123+
* Added new configuration options: pageBreakLocations, headerRegex
124+
* Added new valid value for configuration headerRegex: REGEX
125+
* Updated markup-document-builder from 1.1.0 to 1.1.1-SNAPSHOT

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
}
1414
}
1515
description = 'swagger2markup Build'
16-
version = '1.1.2-SNAPSHOT'
16+
version = '1.2.0'
1717
ext.releaseVersion = '1.1.1'
1818
group = 'io.github.swagger2markup'
1919

@@ -41,7 +41,7 @@ repositories {
4141
}
4242

4343
dependencies {
44-
compile 'io.github.swagger2markup:markup-document-builder:1.1.0'
44+
compile 'io.github.swagger2markup:markup-document-builder:1.1.1-SNAPSHOT'
4545
compile 'io.swagger:swagger-compat-spec-parser:1.0.23'
4646
compile 'org.apache.commons:commons-configuration2:2.1'
4747
compile 'commons-beanutils:commons-beanutils:1.9.2'

src/docs/asciidoc/usage_guide.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ The following tables list all available properties of Swagger2Markup:
136136
|Name | Description | Possible Values | Default
137137
|swagger2markup.markupLanguage| Specifies the markup language which should be used to generate the files. | ASCIIDOC, MARKDOWN, CONFLUENCE_MARKUP | ASCIIDOC
138138
|swagger2markup.swaggerMarkupLanguage| Specifies the markup language used in Swagger descriptions. | ASCIIDOC, MARKDOWN, CONFLUENCE_MARKUP | MARKDOWN
139-
|swagger2markup.pathsGroupedBy| Specifies how the paths should be grouped | AS_IS, TAGS | AS_IS
139+
|swagger2markup.pathsGroupedBy| Specifies how the paths should be grouped | AS_IS, TAGS, REGEX | AS_IS
140140
|swagger2markup.outputLanguage| Specifies the language of the labels | EN, DE, FR, RU | EN
141141
|swagger2markup.lineSeparator| Specifies the line separator which should be used | UNIX, WINDOWS, MAC | <System-dependent>
142142
|swagger2markup.generatedExamplesEnabled| Specifies if HTTP request and response examples should be generated | true, false | false
143143
|swagger2markup.flatBodyEnabled| Optionally isolate the body parameter, if any, from other parameters | true, false | false
144144
|swagger2markup.pathSecuritySectionEnabled| Optionally disable the security section for path sections | true, false | true
145145
|swagger2markup.anchorPrefix| Optionally prefix all anchors for uniqueness if you want to include generated documents into a global documentation | Any String |
146146
|swagger2markup.basePathPrefixEnabled| Prepend the basePath to all paths | true, false | false
147+
|swagger2markup.headerRegex | Regular expression to use when grouping by RegEx | Any valid RegEx pattern with at least one capture group |
147148
|===
148149

149150
[options="header"]
@@ -193,6 +194,13 @@ The following tables list all available properties of Swagger2Markup:
193194
|swagger2markup.inlineSchemaEnabled| Enable inline object schema support | true, false | true
194195
|===
195196

197+
[options="header"]
198+
.Properties which configure page breaking
199+
|===
200+
|Name | Description | Possible Values | Default
201+
|swagger2markup.pageBreakLocations | Specifies where page breaks should be inserted. | BEFORE_OPERATION, BEFORE_OPERATION_DESCRIPTION, BEFORE_OPERATION_PARAMETERS, BEFORE_OPERATION_RESPONSES, BEFORE_OPERATION_CONSUMES, BEFORE_OPERATION_PRODUCES, BEFORE_OPERATION_EXAMPLE_REQUEST, BEFORE_OPERATION_EXAMPLE_RESPONSE, BEFORE_DEFINITION, AFTER_OPERATION, AFTER_OPERATION_DESCRIPTION, AFTER_OPERATION_PARAMETERS, AFTER_OPERATION_RESPONSES, AFTER_OPERATION_CONSUMES, AFTER_OPERATION_PRODUCES, AFTER_OPERATION_EXAMPLE_REQUEST, AFTER_OPERATION_EXAMPLE_RESPONSE, AFTER_DEFINITION | empty
202+
|===
203+
196204
=== Logging
197205

198206
Swagger2Markup uses http://www.slf4j.org/[SLF4J] for all internal logging, but leaves the underlying log implementation open. To change the log level, you have the set the log level of the `io.github.swagger2markup` package.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Robert Winkler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.swagger2markup;
17+
18+
public enum PageBreakLocations {
19+
BEFORE_OPERATION,
20+
BEFORE_OPERATION_DESCRIPTION,
21+
BEFORE_OPERATION_PARAMETERS,
22+
BEFORE_OPERATION_RESPONSES,
23+
BEFORE_OPERATION_CONSUMES,
24+
BEFORE_OPERATION_PRODUCES,
25+
BEFORE_OPERATION_EXAMPLE_REQUEST,
26+
BEFORE_OPERATION_EXAMPLE_RESPONSE,
27+
BEFORE_DEFINITION,
28+
AFTER_OPERATION,
29+
AFTER_OPERATION_DESCRIPTION,
30+
AFTER_OPERATION_PARAMETERS,
31+
AFTER_OPERATION_RESPONSES,
32+
AFTER_OPERATION_CONSUMES,
33+
AFTER_OPERATION_PRODUCES,
34+
AFTER_OPERATION_EXAMPLE_REQUEST,
35+
AFTER_OPERATION_EXAMPLE_RESPONSE,
36+
AFTER_DEFINITION
37+
}

src/main/java/io/github/swagger2markup/Swagger2MarkupConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.models.parameters.Parameter;
2222

2323
import java.util.Comparator;
24+
import java.util.List;
2425
import java.util.regex.Pattern;
2526

2627
/**
@@ -219,4 +220,11 @@ public interface Swagger2MarkupConfig {
219220
* @return the extension properties
220221
*/
221222
Swagger2MarkupProperties getExtensionsProperties();
223+
224+
/**
225+
* Returns the list of page break locations
226+
*
227+
* @return List of PageBreakLocations
228+
*/
229+
List<PageBreakLocations> getPageBreakLocations();
222230
}

src/main/java/io/github/swagger2markup/Swagger2MarkupProperties.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
import java.net.URI;
2626
import java.nio.file.Path;
2727
import java.nio.file.Paths;
28-
import java.util.List;
29-
import java.util.Map;
30-
import java.util.Optional;
31-
import java.util.Properties;
28+
import java.util.*;
29+
import java.util.regex.Pattern;
3230

3331
public class Swagger2MarkupProperties {
3432

@@ -65,6 +63,7 @@ public class Swagger2MarkupProperties {
6563
public static final String PROPERTY_ORDER_BY = PROPERTIES_PREFIX + ".propertyOrderBy";
6664
public static final String RESPONSE_ORDER_BY = PROPERTIES_PREFIX + ".responseOrderBy";
6765
public static final String LINE_SEPARATOR = PROPERTIES_PREFIX + ".lineSeparator";
66+
public static final String PAGE_BREAK_LOCATIONS = PROPERTIES_PREFIX + ".pageBreakLocations";
6867

6968
/**
7069
* Prefix for Swagger2Markup extension properties
@@ -337,4 +336,20 @@ public List<String> getKeys() {
337336
public List<String> getKeys(String prefix) {
338337
return IteratorUtils.toList(configuration.getKeys(prefix));
339338
}
339+
340+
public List<PageBreakLocations> getPageBreakLocations(String key) {
341+
List result = configuration.getList(PageBreakLocations.class, key);
342+
if(result == null) result = new ArrayList<PageBreakLocations>();
343+
344+
return result;
345+
}
346+
347+
public Optional<Pattern> getHeaderPattern(String key) {
348+
Optional<String> property = getString(key);
349+
if (property.isPresent()) {
350+
return Optional.of(Pattern.compile(property.get()));
351+
} else {
352+
return Optional.empty();
353+
}
354+
}
340355
}

src/main/java/io/github/swagger2markup/builder/Swagger2MarkupConfigBuilder.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
import org.apache.commons.lang3.StringUtils;
3131
import org.apache.commons.lang3.Validate;
3232

33-
import java.util.Comparator;
34-
import java.util.Map;
35-
import java.util.Optional;
36-
import java.util.Properties;
33+
import java.util.*;
3734
import java.util.regex.Pattern;
3835
import java.util.regex.PatternSyntaxException;
3936

@@ -107,6 +104,12 @@ public Swagger2MarkupConfigBuilder(Configuration configuration) {
107104
config.lineSeparator = LineSeparator.valueOf(lineSeparator.get());
108105
}
109106

107+
config.pageBreakLocations = swagger2MarkupProperties.getPageBreakLocations(PAGE_BREAK_LOCATIONS);
108+
109+
Optional<Pattern> headerPattern = swagger2MarkupProperties.getHeaderPattern(HEADER_REGEX);
110+
111+
config.headerPattern = headerPattern.orElse(null);
112+
110113
Configuration swagger2markupConfiguration = compositeConfiguration.subset(PROPERTIES_PREFIX);
111114
Configuration extensionsConfiguration = swagger2markupConfiguration.subset(EXTENSION_PREFIX);
112115
config.extensionsProperties = new Swagger2MarkupProperties(extensionsConfiguration);
@@ -486,11 +489,23 @@ public Swagger2MarkupConfigBuilder withBasePathPrefix() {
486489
* @return this builder
487490
*/
488491
public Swagger2MarkupConfigBuilder withAnchorPrefix(String anchorPrefix) {
489-
Validate.notNull(anchorPrefix, "%s must no be null", "anchorPrefix");
492+
Validate.notNull(anchorPrefix, "%s must not be null", "anchorPrefix");
490493
config.anchorPrefix = anchorPrefix;
491494
return this;
492495
}
493496

497+
/**
498+
* Set the page break locations
499+
*
500+
* @param locations List of locations to create new pages
501+
* @return this builder
502+
*/
503+
public Swagger2MarkupConfigBuilder withPageBreaks(List<PageBreakLocations> locations) {
504+
Validate.notNull(locations, "%s must not be null", "locations");
505+
config.pageBreakLocations = locations;
506+
return this;
507+
}
508+
494509
/**
495510
* Specifies the line separator which should be used.
496511
*
@@ -540,6 +555,8 @@ static class DefaultSwagger2MarkupConfig implements Swagger2MarkupConfig {
540555
private String separatedOperationsFolder;
541556
private String separatedDefinitionsFolder;
542557

558+
private List<PageBreakLocations> pageBreakLocations;
559+
543560
private Pattern headerPattern;
544561

545562
private Swagger2MarkupProperties extensionsProperties;
@@ -718,5 +735,10 @@ public Swagger2MarkupProperties getExtensionsProperties() {
718735
public boolean isBasePathPrefixEnabled() {
719736
return basePathPrefixEnabled;
720737
}
738+
739+
@Override
740+
public List<PageBreakLocations> getPageBreakLocations() {
741+
return pageBreakLocations;
742+
}
721743
}
722744
}

src/main/java/io/github/swagger2markup/internal/component/PathOperationComponent.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import io.github.swagger2markup.GroupBy;
23+
import io.github.swagger2markup.PageBreakLocations;
2324
import io.github.swagger2markup.Swagger2MarkupConverter;
2425
import io.github.swagger2markup.internal.resolver.DocumentResolver;
2526
import io.github.swagger2markup.internal.type.ObjectType;
@@ -41,6 +42,7 @@
4142
import java.util.*;
4243

4344
import static io.github.swagger2markup.Labels.*;
45+
import static io.github.swagger2markup.PageBreakLocations.*;
4446
import static io.github.swagger2markup.internal.utils.MarkupDocBuilderUtils.copyMarkupDocBuilder;
4547
import static io.github.swagger2markup.internal.utils.MarkupDocBuilderUtils.markupDescription;
4648
import static io.github.swagger2markup.spi.PathsDocumentExtension.Position;
@@ -80,21 +82,46 @@ public static PathOperationComponent.Parameters parameters(PathOperation operati
8082
@Override
8183
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) {
8284
PathOperation operation = params.operation;
85+
List<PageBreakLocations> locations = config.getPageBreakLocations();
86+
8387
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEFORE, markupDocBuilder, operation));
88+
89+
if (locations.contains(BEFORE_OPERATION)) markupDocBuilder.pageBreak();
8490
buildOperationTitle(markupDocBuilder, operation);
91+
8592
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEGIN, markupDocBuilder, operation));
8693
buildDeprecatedSection(markupDocBuilder, operation);
94+
95+
if (locations.contains(BEFORE_OPERATION_DESCRIPTION)) markupDocBuilder.pageBreak();
8796
buildDescriptionSection(markupDocBuilder, operation);
97+
if (locations.contains(AFTER_OPERATION_DESCRIPTION)) markupDocBuilder.pageBreak();
98+
99+
if (locations.contains(BEFORE_OPERATION_PARAMETERS)) markupDocBuilder.pageBreak();
88100
inlineDefinitions(markupDocBuilder, buildParametersSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod());
101+
if (locations.contains(AFTER_OPERATION_PARAMETERS)) markupDocBuilder.pageBreak();
102+
89103
inlineDefinitions(markupDocBuilder, buildBodyParameterSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod());
104+
105+
if (locations.contains(BEFORE_OPERATION_RESPONSES)) markupDocBuilder.pageBreak();
90106
inlineDefinitions(markupDocBuilder, buildResponsesSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod());
107+
if (locations.contains(AFTER_OPERATION_RESPONSES)) markupDocBuilder.pageBreak();
108+
109+
if (locations.contains(BEFORE_OPERATION_CONSUMES)) markupDocBuilder.pageBreak();
91110
buildConsumesSection(markupDocBuilder, operation);
111+
if (locations.contains(AFTER_OPERATION_CONSUMES)) markupDocBuilder.pageBreak();
112+
113+
if (locations.contains(BEFORE_OPERATION_PRODUCES)) markupDocBuilder.pageBreak();
92114
buildProducesSection(markupDocBuilder, operation);
115+
if (locations.contains(AFTER_OPERATION_PRODUCES)) markupDocBuilder.pageBreak();
116+
93117
buildTagsSection(markupDocBuilder, operation);
94118
buildSecuritySchemeSection(markupDocBuilder, operation);
95-
buildExamplesSection(markupDocBuilder, operation);
119+
buildExamplesSection(markupDocBuilder, operation, locations);
96120
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_END, markupDocBuilder, operation));
97121
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_AFTER, markupDocBuilder, operation));
122+
123+
if (locations.contains(AFTER_OPERATION)) markupDocBuilder.pageBreak();
124+
98125
return markupDocBuilder;
99126
}
100127

@@ -317,17 +344,23 @@ private int getSectionTitleLevel() {
317344
*
318345
* @param operation the Swagger Operation
319346
*/
320-
private void buildExamplesSection(MarkupDocBuilder markupDocBuilder, PathOperation operation) {
347+
private void buildExamplesSection(MarkupDocBuilder markupDocBuilder, PathOperation operation, List<PageBreakLocations> locations) {
321348

322349
Map<String, Object> generatedRequestExampleMap = ExamplesUtil.generateRequestExampleMap(config.isGeneratedExamplesEnabled(), operation, definitions, definitionDocumentResolver, markupDocBuilder);
323350
Map<String, Object> generatedResponseExampleMap = ExamplesUtil.generateResponseExampleMap(config.isGeneratedExamplesEnabled(), operation, definitions, definitionDocumentResolver, markupDocBuilder);
324351

325-
exampleMap(markupDocBuilder, generatedRequestExampleMap, labels.getLabel(EXAMPLE_REQUEST), labels.getLabel(REQUEST));
326-
exampleMap(markupDocBuilder, generatedResponseExampleMap, labels.getLabel(EXAMPLE_RESPONSE), labels.getLabel(RESPONSE));
352+
boolean beforeExampleRequestBreak = locations.contains(BEFORE_OPERATION_EXAMPLE_REQUEST);
353+
boolean afterExampleRequestBreak = locations.contains(AFTER_OPERATION_EXAMPLE_REQUEST);
354+
boolean beforeExampleResponseBreak = locations.contains(BEFORE_OPERATION_EXAMPLE_RESPONSE);
355+
boolean afterExampleResponseBreak = locations.contains(AFTER_OPERATION_EXAMPLE_RESPONSE);
356+
357+
exampleMap(markupDocBuilder, generatedRequestExampleMap, labels.getLabel(EXAMPLE_REQUEST), labels.getLabel(REQUEST), beforeExampleRequestBreak, afterExampleRequestBreak);
358+
exampleMap(markupDocBuilder, generatedResponseExampleMap, labels.getLabel(EXAMPLE_RESPONSE), labels.getLabel(RESPONSE), beforeExampleResponseBreak, afterExampleResponseBreak);
327359
}
328360

329-
private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> exampleMap, String operationSectionTitle, String sectionTitle) {
361+
private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> exampleMap, String operationSectionTitle, String sectionTitle, boolean beforeBreak, boolean afterBreak) {
330362
if (exampleMap.size() > 0) {
363+
if (beforeBreak) markupDocBuilder.pageBreak();
331364
buildSectionTitle(markupDocBuilder, operationSectionTitle);
332365
for (Map.Entry<String, Object> entry : exampleMap.entrySet()) {
333366

@@ -376,6 +409,7 @@ private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> e
376409
markupDocBuilder.listingBlock(Json.pretty(entry.getValue()), "json");
377410
}
378411
}
412+
if (afterBreak) markupDocBuilder.pageBreak();
379413
}
380414
}
381415

0 commit comments

Comments
 (0)