Auto-generated examples are disabled by default. Swagger examples are always rendered.
This commit is contained in:
@@ -102,7 +102,7 @@ public class PathsDocument extends MarkupDocument {
|
||||
DEPRECATED_OPERATION = labels.getString("operation.deprecated");
|
||||
UNKNOWN = labels.getString("unknown");
|
||||
|
||||
if (config.isExamplesEnabled()) {
|
||||
if (config.isGeneratedExamplesEnabled()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Include examples is enabled.");
|
||||
}
|
||||
@@ -593,26 +593,19 @@ public class PathsDocument extends MarkupDocument {
|
||||
*/
|
||||
private void examplesSection(PathOperation operation, MarkupDocBuilder docBuilder) {
|
||||
|
||||
if (globalContext.config.isExamplesEnabled()) {
|
||||
Optional<Map<String, Object>> generatedRequestExampleMap;
|
||||
Optional<Map<String, Object>> generatedResponseExampleMap;
|
||||
Map<String, Object> generatedRequestExampleMap = ExamplesUtil.generateRequestExampleMap(globalContext.config.isGeneratedExamplesEnabled(), operation, globalContext.swagger.getDefinitions(), markupDocBuilder);
|
||||
Map<String, Object> generatedResponseExampleMap = ExamplesUtil.generateResponseExampleMap(globalContext.config.isGeneratedExamplesEnabled(), operation.getOperation(), globalContext.swagger.getDefinitions(), markupDocBuilder);
|
||||
|
||||
generatedRequestExampleMap = ExamplesUtil.generateRequestExampleMap(operation, globalContext.swagger.getDefinitions(), markupDocBuilder);
|
||||
generatedResponseExampleMap = ExamplesUtil.generateResponseExampleMap(operation.getOperation(), globalContext.swagger.getDefinitions(), markupDocBuilder);
|
||||
|
||||
exampleMap(generatedRequestExampleMap, EXAMPLE_REQUEST, REQUEST, docBuilder);
|
||||
exampleMap(generatedResponseExampleMap, EXAMPLE_RESPONSE, RESPONSE, docBuilder);
|
||||
}
|
||||
exampleMap(generatedRequestExampleMap, EXAMPLE_REQUEST, REQUEST, docBuilder);
|
||||
exampleMap(generatedResponseExampleMap, EXAMPLE_RESPONSE, RESPONSE, docBuilder);
|
||||
}
|
||||
|
||||
private void exampleMap(Optional<Map<String, Object>> exampleMap, String operationSectionTitle, String sectionTile, MarkupDocBuilder docBuilder){
|
||||
if (exampleMap.isPresent()) {
|
||||
private void exampleMap(Map<String, Object> exampleMap, String operationSectionTitle, String sectionTile, MarkupDocBuilder docBuilder) {
|
||||
if (exampleMap.size() > 0) {
|
||||
addOperationSectionTitle(operationSectionTitle, docBuilder);
|
||||
if (exampleMap.get().size() > 0) {
|
||||
for (Map.Entry<String, Object> entry : exampleMap.get().entrySet()) {
|
||||
docBuilder.sectionTitleLevel4(sectionTile + " " + entry.getKey() + " :");
|
||||
docBuilder.listing(Json.pretty(entry.getValue()));
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : exampleMap.entrySet()) {
|
||||
addOperationSectionTitle(sectionTile + " " + entry.getKey(), docBuilder);
|
||||
docBuilder.listing(Json.pretty(entry.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class Swagger2MarkupConfig {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Swagger2MarkupConfig.class);
|
||||
|
||||
private MarkupLanguage markupLanguage;
|
||||
private boolean examplesEnabled;
|
||||
private boolean generatedExamplesEnabled;
|
||||
private boolean schemasEnabled;
|
||||
private URI schemasUri;
|
||||
private boolean operationDescriptionsEnabled;
|
||||
@@ -142,8 +142,8 @@ public class Swagger2MarkupConfig {
|
||||
return markupLanguage;
|
||||
}
|
||||
|
||||
public boolean isExamplesEnabled() {
|
||||
return examplesEnabled;
|
||||
public boolean isGeneratedExamplesEnabled() {
|
||||
return generatedExamplesEnabled;
|
||||
}
|
||||
|
||||
public boolean isSchemasEnabled() {
|
||||
@@ -327,7 +327,7 @@ public class Swagger2MarkupConfig {
|
||||
safeProperties.putAll(properties);
|
||||
|
||||
config.markupLanguage = MarkupLanguage.valueOf(safeProperties.getProperty(PROPERTIES_PREFIX + "markupLanguage"));
|
||||
config.examplesEnabled = Boolean.valueOf(safeProperties.getProperty(PROPERTIES_PREFIX + "examplesEnabled"));
|
||||
config.generatedExamplesEnabled = Boolean.valueOf(safeProperties.getProperty(PROPERTIES_PREFIX + "generatedExamplesEnabled"));
|
||||
config.schemasEnabled = Boolean.valueOf(safeProperties.getProperty(PROPERTIES_PREFIX + "schemasEnabled"));
|
||||
if (safeProperties.containsKey(PROPERTIES_PREFIX + "schemasUri"))
|
||||
config.schemasUri = URI.create(safeProperties.getProperty(PROPERTIES_PREFIX + "schemasUri"));
|
||||
@@ -408,12 +408,12 @@ public class Swagger2MarkupConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* Include examples into the Paths document
|
||||
* Include generated examples into the Paths document
|
||||
*
|
||||
* @return this builder
|
||||
*/
|
||||
public Builder withExamples() {
|
||||
config.examplesEnabled = true;
|
||||
public Builder withGeneratedExamples() {
|
||||
config.generatedExamplesEnabled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.github.robwin.swagger2markup.utils;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import io.github.robwin.markup.builder.MarkupDocBuilder;
|
||||
import io.github.robwin.swagger2markup.PathOperation;
|
||||
import io.swagger.models.*;
|
||||
@@ -27,32 +26,33 @@ public class ExamplesUtil {
|
||||
* @param operation the Swagger Operation
|
||||
* @return map containing response examples.
|
||||
*/
|
||||
public static Optional<Map<String, Object>> generateResponseExampleMap(Operation operation, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
public static Map<String, Object> generateResponseExampleMap(boolean generateMissingExamples, Operation operation, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
Map<String, Object> examples = new HashMap<>();
|
||||
Map<String, Response> responses = operation.getResponses();
|
||||
for (Map.Entry<String, Response> responseEntry : responses.entrySet()) {
|
||||
Response response = responseEntry.getValue();
|
||||
Object example = response.getExamples();
|
||||
if (example != null) {
|
||||
examples.put(responseEntry.getKey(), example);
|
||||
} else {
|
||||
if (example == null) {
|
||||
Property schema = response.getSchema();
|
||||
example = schema != null ? schema.getExample() : null;
|
||||
if (example == null && schema instanceof RefProperty) {
|
||||
String simpleRef = ((RefProperty) schema).getSimpleRef();
|
||||
example = generateExampleForRefModel(simpleRef, definitions, markupDocBuilder);
|
||||
}
|
||||
if (example == null && schema != null) {
|
||||
examples.put(responseEntry.getKey(), PropertyUtils.exampleFromType(schema.getType(), schema, markupDocBuilder));
|
||||
} else if (example != null) {
|
||||
examples.put(responseEntry.getKey(), example);
|
||||
if (schema != null) {
|
||||
example = schema.getExample();
|
||||
|
||||
if (example == null && schema instanceof RefProperty) {
|
||||
String simpleRef = ((RefProperty) schema).getSimpleRef();
|
||||
example = generateExampleForRefModel(generateMissingExamples, simpleRef, definitions, markupDocBuilder);
|
||||
}
|
||||
if (example == null && generateMissingExamples) {
|
||||
example = PropertyUtils.exampleFromType(schema.getType(), schema, markupDocBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (example != null)
|
||||
examples.put(responseEntry.getKey(), example);
|
||||
|
||||
}
|
||||
if (examples.size() == 0) {
|
||||
return Optional.absent();
|
||||
}
|
||||
return Optional.of(examples);
|
||||
|
||||
return examples;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,13 +61,14 @@ public class ExamplesUtil {
|
||||
* @param pathOperation the Swagger Operation
|
||||
* @return an Optional with the example content
|
||||
*/
|
||||
public static Optional<Map<String, Object>> generateRequestExampleMap(PathOperation pathOperation, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
public static Map<String, Object> generateRequestExampleMap(boolean generateMissingExamples, PathOperation pathOperation, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
Operation operation = pathOperation.getOperation();
|
||||
List<Parameter> parameters = operation.getParameters();
|
||||
Map<String, Object> examples = new HashMap<>();
|
||||
|
||||
//Path example should always be included:
|
||||
examples.put("path", pathOperation.getPath());
|
||||
// Path example should always be included (if generateMissingExamples):
|
||||
if (generateMissingExamples)
|
||||
examples.put("path", pathOperation.getPath());
|
||||
for (Parameter parameter : parameters) {
|
||||
Object example = null;
|
||||
if (parameter instanceof BodyParameter) {
|
||||
@@ -76,62 +77,64 @@ public class ExamplesUtil {
|
||||
Model schema = ((BodyParameter) parameter).getSchema();
|
||||
if (schema instanceof RefModel) {
|
||||
String simpleRef = ((RefModel) schema).getSimpleRef();
|
||||
example = generateExampleForRefModel(simpleRef, definitions, markupDocBuilder);
|
||||
} else if (schema instanceof ComposedModel) {
|
||||
example = exampleMapForProperties(getPropertiesForComposedModel(
|
||||
(ComposedModel) schema, definitions), definitions, markupDocBuilder);
|
||||
} else if (schema instanceof ArrayModel) {
|
||||
example = generateExampleForArrayModel((ArrayModel) schema, definitions, markupDocBuilder);
|
||||
} else {
|
||||
example = schema.getExample();
|
||||
if (example == null) {
|
||||
example = exampleMapForProperties(schema.getProperties(), definitions, markupDocBuilder);
|
||||
example = generateExampleForRefModel(generateMissingExamples, simpleRef, definitions, markupDocBuilder);
|
||||
} else if (generateMissingExamples) {
|
||||
if (schema instanceof ComposedModel) {
|
||||
example = exampleMapForProperties(getPropertiesForComposedModel(
|
||||
(ComposedModel) schema, definitions), definitions, markupDocBuilder);
|
||||
} else if (schema instanceof ArrayModel) {
|
||||
example = generateExampleForArrayModel((ArrayModel) schema, definitions, markupDocBuilder);
|
||||
} else {
|
||||
example = schema.getExample();
|
||||
if (example == null) {
|
||||
example = exampleMapForProperties(schema.getProperties(), definitions, markupDocBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (parameter instanceof AbstractSerializableParameter) {
|
||||
Object abstractSerializableParameterExample;
|
||||
abstractSerializableParameterExample = ((AbstractSerializableParameter) parameter).getExample();
|
||||
if (abstractSerializableParameterExample == null) {
|
||||
Property item = ((AbstractSerializableParameter) parameter).getItems();
|
||||
if (item != null) {
|
||||
abstractSerializableParameterExample = convertStringToType((String) item.getExample(), item.getType());
|
||||
if (generateMissingExamples) {
|
||||
Object abstractSerializableParameterExample;
|
||||
abstractSerializableParameterExample = ((AbstractSerializableParameter) parameter).getExample();
|
||||
if (abstractSerializableParameterExample == null) {
|
||||
Property item = ((AbstractSerializableParameter) parameter).getItems();
|
||||
if (item != null) {
|
||||
abstractSerializableParameterExample = convertStringToType(item.getExample(), item.getType());
|
||||
if (abstractSerializableParameterExample == null) {
|
||||
abstractSerializableParameterExample = PropertyUtils.exampleFromType(item.getType(), item, markupDocBuilder);
|
||||
}
|
||||
}
|
||||
if (abstractSerializableParameterExample == null) {
|
||||
abstractSerializableParameterExample = PropertyUtils.exampleFromType(item.getType(), item, markupDocBuilder);
|
||||
abstractSerializableParameterExample = PropertyUtils.exampleFromType(((AbstractSerializableParameter) parameter).getType(), null, markupDocBuilder);
|
||||
}
|
||||
}
|
||||
if (abstractSerializableParameterExample == null) {
|
||||
abstractSerializableParameterExample = PropertyUtils.exampleFromType(((AbstractSerializableParameter) parameter).getType(), null, markupDocBuilder);
|
||||
if (parameter instanceof PathParameter) {
|
||||
String pathExample = (String) examples.get("path");
|
||||
pathExample = pathExample.replace('{' + parameter.getName() + '}', String.valueOf(abstractSerializableParameterExample));
|
||||
example = pathExample;
|
||||
} else {
|
||||
example = abstractSerializableParameterExample;
|
||||
}
|
||||
}
|
||||
if (parameter instanceof PathParameter) {
|
||||
String pathExample = (String) examples.get("path");
|
||||
MarkupDocBuilder italicString = markupDocBuilder.copy().italicText(String.valueOf(abstractSerializableParameterExample));
|
||||
pathExample = pathExample.replace('{' + parameter.getName() + '}', '*' + italicString.toString() + "*");
|
||||
example = pathExample;
|
||||
} else {
|
||||
example = abstractSerializableParameterExample;
|
||||
}
|
||||
if (parameter instanceof QueryParameter) {
|
||||
//noinspection unchecked
|
||||
Map<String, Object> queryExampleMap = (Map<String, Object>) examples.get("query");
|
||||
if (queryExampleMap == null) {
|
||||
queryExampleMap = new HashMap<>();
|
||||
if (parameter instanceof QueryParameter) {
|
||||
//noinspection unchecked
|
||||
Map<String, Object> queryExampleMap = (Map<String, Object>) examples.get("query");
|
||||
if (queryExampleMap == null) {
|
||||
queryExampleMap = new HashMap<>();
|
||||
}
|
||||
queryExampleMap.put(parameter.getName(), abstractSerializableParameterExample);
|
||||
example = queryExampleMap;
|
||||
}
|
||||
queryExampleMap.put(parameter.getName(), abstractSerializableParameterExample);
|
||||
example = queryExampleMap;
|
||||
}
|
||||
} else if (parameter instanceof RefParameter) {
|
||||
String simpleRef = ((RefParameter) parameter).getSimpleRef();
|
||||
example = generateExampleForRefModel(simpleRef, definitions, markupDocBuilder);
|
||||
example = generateExampleForRefModel(generateMissingExamples, simpleRef, definitions, markupDocBuilder);
|
||||
}
|
||||
examples.put(parameter.getIn(), example);
|
||||
|
||||
if (example != null)
|
||||
examples.put(parameter.getIn(), example);
|
||||
}
|
||||
|
||||
if (examples.size() == 0) {
|
||||
return Optional.absent();
|
||||
}
|
||||
return Optional.of(examples);
|
||||
return examples;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,12 +143,12 @@ public class ExamplesUtil {
|
||||
* @param simpleRef the simple reference string
|
||||
* @return returns an Object or Map of examples
|
||||
*/
|
||||
public static Object generateExampleForRefModel(String simpleRef, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
public static Object generateExampleForRefModel(boolean generateMissingExamples, String simpleRef, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
Model model = definitions.get(simpleRef);
|
||||
Object example = null;
|
||||
if (model != null) {
|
||||
example = model.getExample();
|
||||
if (example == null) {
|
||||
if (example == null && generateMissingExamples) {
|
||||
if (model instanceof ComposedModel) {
|
||||
example = exampleMapForProperties(getPropertiesForComposedModel((ComposedModel) model, definitions), definitions, markupDocBuilder);
|
||||
} else {
|
||||
@@ -190,11 +193,11 @@ public class ExamplesUtil {
|
||||
*/
|
||||
public static Map<String, Object> exampleMapForProperties(Map<String, Property> properties, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
Map<String, Object> exampleMap = new HashMap<>();
|
||||
for (Map.Entry<String,Property> property : properties.entrySet()) {
|
||||
for (Map.Entry<String, Property> property : properties.entrySet()) {
|
||||
Object exampleObject = convertStringToType(property.getValue().getExample(), property.getValue().getType());
|
||||
if (exampleObject == null) {
|
||||
if (property.getValue() instanceof RefProperty) {
|
||||
exampleObject = generateExampleForRefModel(((RefProperty) property.getValue()).getSimpleRef(), definitions, markupDocBuilder);
|
||||
exampleObject = generateExampleForRefModel(true, ((RefProperty) property.getValue()).getSimpleRef(), definitions, markupDocBuilder);
|
||||
} else if (property.getValue() instanceof ArrayProperty) {
|
||||
exampleObject = generateExampleForArrayProperty((ArrayProperty) property.getValue(), definitions, markupDocBuilder);
|
||||
} else if (property.getValue() instanceof MapProperty) {
|
||||
@@ -227,17 +230,17 @@ public class ExamplesUtil {
|
||||
if (model.getExample() != null) {
|
||||
return model.getExample();
|
||||
} else if (model.getProperties() != null) {
|
||||
return new Object[] {exampleMapForProperties(model.getProperties(), definitions, markupDocBuilder)};
|
||||
return new Object[]{exampleMapForProperties(model.getProperties(), definitions, markupDocBuilder)};
|
||||
} else {
|
||||
Property itemProperty = model.getItems();
|
||||
if (itemProperty.getExample() != null) {
|
||||
return new Object[] { convertStringToType(itemProperty.getExample(), itemProperty.getType()) };
|
||||
return new Object[]{convertStringToType(itemProperty.getExample(), itemProperty.getType())};
|
||||
} else if (itemProperty instanceof ArrayProperty) {
|
||||
return new Object[] { generateExampleForArrayProperty((ArrayProperty) itemProperty, definitions, markupDocBuilder) };
|
||||
return new Object[]{generateExampleForArrayProperty((ArrayProperty) itemProperty, definitions, markupDocBuilder)};
|
||||
} else if (itemProperty instanceof RefProperty) {
|
||||
return new Object[] { generateExampleForRefModel(((RefProperty) itemProperty).getSimpleRef(), definitions, markupDocBuilder) };
|
||||
return new Object[]{generateExampleForRefModel(true, ((RefProperty) itemProperty).getSimpleRef(), definitions, markupDocBuilder)};
|
||||
} else {
|
||||
return new Object[] { PropertyUtils.exampleFromType(itemProperty.getType(), itemProperty, markupDocBuilder) };
|
||||
return new Object[]{PropertyUtils.exampleFromType(itemProperty.getType(), itemProperty, markupDocBuilder)};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,13 +254,13 @@ public class ExamplesUtil {
|
||||
public static Object[] generateExampleForArrayProperty(ArrayProperty value, Map<String, Model> definitions, MarkupDocBuilder markupDocBuilder) {
|
||||
Property property = value.getItems();
|
||||
if (property.getExample() != null) {
|
||||
return new Object[] {convertStringToType(property.getExample(), property.getType())};
|
||||
return new Object[]{convertStringToType(property.getExample(), property.getType())};
|
||||
} else if (property instanceof ArrayProperty) {
|
||||
return new Object[] {generateExampleForArrayProperty((ArrayProperty) property, definitions, markupDocBuilder)};
|
||||
return new Object[]{generateExampleForArrayProperty((ArrayProperty) property, definitions, markupDocBuilder)};
|
||||
} else if (property instanceof RefProperty) {
|
||||
return new Object[] {generateExampleForRefModel(((RefProperty) property).getSimpleRef(), definitions, markupDocBuilder)};
|
||||
return new Object[]{generateExampleForRefModel(true, ((RefProperty) property).getSimpleRef(), definitions, markupDocBuilder)};
|
||||
} else {
|
||||
return new Object[] {PropertyUtils.exampleFromType(property.getType(), property, markupDocBuilder)};
|
||||
return new Object[]{PropertyUtils.exampleFromType(property.getType(), property, markupDocBuilder)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,7 +277,7 @@ public class ExamplesUtil {
|
||||
case "boolean":
|
||||
return new Boolean(value);
|
||||
case "string":
|
||||
return value;
|
||||
return value;
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -27,10 +27,7 @@ import io.swagger.util.Json;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
|
||||
@@ -43,7 +40,7 @@ public final class PropertyUtils {
|
||||
* @return the type of the property
|
||||
*/
|
||||
public static Type getType(Property property, Function<String, String> definitionDocumentResolver) {
|
||||
Validate.notNull(property, "property must not be null!");
|
||||
Validate.notNull(property, "property must not be null");
|
||||
Type type;
|
||||
if (property instanceof RefProperty) {
|
||||
RefProperty refProperty = (RefProperty) property;
|
||||
@@ -82,7 +79,7 @@ public final class PropertyUtils {
|
||||
* @return the default value of the property, or otherwise an empty String
|
||||
*/
|
||||
public static String getDefaultValue(Property property) {
|
||||
Validate.notNull(property, "property must not be null!");
|
||||
Validate.notNull(property, "property must not be null");
|
||||
String defaultValue = "";
|
||||
if (property instanceof BooleanProperty) {
|
||||
BooleanProperty booleanProperty = (BooleanProperty) property;
|
||||
@@ -109,8 +106,15 @@ public final class PropertyUtils {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return example display string for the given {@code property}.
|
||||
*
|
||||
* @param property property
|
||||
* @param markupDocBuilder doc builder
|
||||
* @return property example display string
|
||||
*/
|
||||
public static String getExample(Property property, MarkupDocBuilder markupDocBuilder) {
|
||||
Validate.notNull(property, "parameter must not be null!");
|
||||
Validate.notNull(property, "property must not be null");
|
||||
Object examplesValue;
|
||||
if (property.getExample() != null) {
|
||||
examplesValue = property.getExample();
|
||||
@@ -124,10 +128,12 @@ public final class PropertyUtils {
|
||||
examplesValue = Json.pretty(exampleMap);
|
||||
}
|
||||
} else if (property instanceof ArrayProperty) {
|
||||
List<Object> exampleArray = new ArrayList<>();
|
||||
Property itemProperty = ((ArrayProperty) property).getItems();
|
||||
examplesValue = "[ " + exampleFromType(itemProperty.getType(), itemProperty, markupDocBuilder) + " ]";
|
||||
exampleArray.add(exampleFromType(itemProperty.getType(), itemProperty, markupDocBuilder));
|
||||
examplesValue = Json.pretty(exampleArray);
|
||||
} else {
|
||||
examplesValue = exampleFromType(property.getType(), property, markupDocBuilder);
|
||||
examplesValue = Json.pretty(exampleFromType(property.getType(), property, markupDocBuilder));
|
||||
}
|
||||
return String.valueOf(examplesValue);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
swagger2markup.markupLanguage=ASCIIDOC
|
||||
swagger2markup.examplesEnabled=false
|
||||
swagger2markup.generatedExamplesEnabled=false
|
||||
swagger2markup.schemasEnabled=false
|
||||
swagger2markup.operationDescriptionsEnabled=false
|
||||
swagger2markup.definitionDescriptionsEnabled=false
|
||||
|
||||
@@ -110,7 +110,6 @@ public class Swagger2MarkupConverterTest {
|
||||
|
||||
//When
|
||||
Swagger2MarkupConfig config = Swagger2MarkupConfig.ofDefaults()
|
||||
.withExamples()
|
||||
.build();
|
||||
|
||||
Swagger2MarkupConverter.from(swaggerJsonString)
|
||||
@@ -120,12 +119,142 @@ public class Swagger2MarkupConverterTest {
|
||||
|
||||
//Then
|
||||
String[] directories = outputDirectory.toFile().list();
|
||||
assertThat(new String(Files.readAllBytes(outputDirectory.resolve("paths.adoc"))))
|
||||
.contains("==== Example HTTP response");
|
||||
assertThat(new String(Files.readAllBytes(outputDirectory.resolve("definitions.adoc"))))
|
||||
.contains("|name||true|string||doggie");
|
||||
String orderExample = "----\n" +
|
||||
"{\n" +
|
||||
" \"id\" : 99,\n" +
|
||||
" \"petId\" : 122,\n" +
|
||||
" \"quantity\" : 2,\n" +
|
||||
" \"shipDate\" : \"2016-02-22T23:02:05Z\",\n" +
|
||||
" \"status\" : \"PENDING\",\n" +
|
||||
" \"complete\" : true\n" +
|
||||
"}\n" +
|
||||
"----\n";
|
||||
String petResponseExample = "----\n" +
|
||||
"{\n" +
|
||||
" \"application/json\" : {\n" +
|
||||
" \"name\" : \"Puma\",\n" +
|
||||
" \"type\" : 22,\n" +
|
||||
" \"color\" : \"Black\",\n" +
|
||||
" \"gender\" : \"Female\",\n" +
|
||||
" \"breed\" : \"Mixed\"\n" +
|
||||
" }\n" +
|
||||
"}\n" +
|
||||
"----\n";
|
||||
|
||||
String pathsDocument = new String(Files.readAllBytes(outputDirectory.resolve("paths.adoc")));
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Response 405\n" + petResponseExample);
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Request body\n" + orderExample);
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Response 200\n" + orderExample);
|
||||
|
||||
String definitionsDocument = new String(Files.readAllBytes(outputDirectory.resolve("definitions.adoc")));
|
||||
assertThat(definitionsDocument)
|
||||
.contains("|id||false|integer(int64)||77");
|
||||
assertThat(definitionsDocument).contains("|pictures||false|string(byte) array||[ \"string\" ]");
|
||||
assertThat(definitionsDocument).contains("|shipDate||false|string(date-time)||\"string\"");
|
||||
assertThat(definitionsDocument)
|
||||
.doesNotContain("99");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwagger2AsciiDocConversionWithGeneratedExamples() throws IOException {
|
||||
//Given
|
||||
String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/json/swagger_examples.json"));
|
||||
Path outputDirectory = Paths.get("build/docs/asciidoc/generated");
|
||||
FileUtils.deleteQuietly(outputDirectory.toFile());
|
||||
|
||||
//When
|
||||
Swagger2MarkupConfig config = Swagger2MarkupConfig.ofDefaults()
|
||||
.withGeneratedExamples()
|
||||
.build();
|
||||
|
||||
Swagger2MarkupConverter.from(swaggerJsonString)
|
||||
.withConfig(config)
|
||||
.build()
|
||||
.intoFolder(outputDirectory);
|
||||
|
||||
//Then
|
||||
String[] directories = outputDirectory.toFile().list();
|
||||
String petGeneratedExample = "----\n" +
|
||||
"{\n" +
|
||||
" \"tags\" : [ {\n" +
|
||||
" \"id\" : 0,\n" +
|
||||
" \"name\" : \"string\"\n" +
|
||||
" } ],\n" +
|
||||
" \"id\" : 0,\n" +
|
||||
" \"nicknames\" : {\n" +
|
||||
" \"string\" : \"string\"\n" +
|
||||
" },\n" +
|
||||
" \"category\" : {\n" +
|
||||
" \"id\" : 123,\n" +
|
||||
" \"name\" : \"Canines\"\n" +
|
||||
" },\n" +
|
||||
" \"weight\" : 0.0,\n" +
|
||||
" \"status\" : \"string\",\n" +
|
||||
" \"name\" : \"doggie\",\n" +
|
||||
" \"photoUrls\" : [ \"string\" ]\n" +
|
||||
"}\n" +
|
||||
"----\n";
|
||||
String petResponseExample = "----\n" +
|
||||
"{\n" +
|
||||
" \"application/json\" : {\n" +
|
||||
" \"name\" : \"Puma\",\n" +
|
||||
" \"type\" : 22,\n" +
|
||||
" \"color\" : \"Black\",\n" +
|
||||
" \"gender\" : \"Female\",\n" +
|
||||
" \"breed\" : \"Mixed\"\n" +
|
||||
" }\n" +
|
||||
"}\n" +
|
||||
"----\n";
|
||||
String pathsDocument = new String(Files.readAllBytes(outputDirectory.resolve("paths.adoc")));
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Request body\n" + petGeneratedExample);
|
||||
assertThat(pathsDocument)
|
||||
.contains("== Request path\n" + "----\n" +
|
||||
"\"/pets\"\n" +
|
||||
"----");
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Request query\n" +
|
||||
"----\n" +
|
||||
"{\n" +
|
||||
" \"status\" : \"string\"\n" +
|
||||
"}\n" +
|
||||
"----\n");
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Response 405\n" + petResponseExample);
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Response 200\n" +
|
||||
"----\n" +
|
||||
"\"array\"\n" +
|
||||
"----");
|
||||
assertThat(pathsDocument)
|
||||
.contains("==== Request path\n" +
|
||||
"----\n" +
|
||||
"\"/pets/0\"\n" +
|
||||
"----");
|
||||
|
||||
String definitionsDocument = new String(Files.readAllBytes(outputDirectory.resolve("definitions.adoc")));
|
||||
assertThat(definitionsDocument)
|
||||
.contains("|id||false|integer(int64)||77");
|
||||
assertThat(definitionsDocument)
|
||||
.contains("|name||true|string||doggie");
|
||||
assertThat(definitionsDocument)
|
||||
.contains("|nicknames||false|object||{\n" +
|
||||
" \"string\" : \"string\"\n" +
|
||||
"}");
|
||||
assertThat(definitionsDocument)
|
||||
.contains("[options=\"header\", cols=\".^1h,.^6,.^1,.^1,.^1,.^1\"]\n" +
|
||||
"|===\n" +
|
||||
"|Name|Description|Required|Schema|Default|Example\n" +
|
||||
"|id||false|integer(int64)||0\n" +
|
||||
"|===\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSwagger2AsciiDocConversionAsString() throws IOException, URISyntaxException {
|
||||
//Given
|
||||
|
||||
@@ -45,7 +45,7 @@ public class Swagger2MarkupConfigTest {
|
||||
assertThat(config.getOperationDescriptionsUri()).isNull();
|
||||
assertThat(config.isDefinitionDescriptionsEnabled()).isFalse();
|
||||
assertThat(config.getDefinitionDescriptionsUri()).isNull();
|
||||
assertThat(config.isExamplesEnabled()).isFalse();
|
||||
assertThat(config.isGeneratedExamplesEnabled()).isFalse();
|
||||
assertThat(config.getInlineSchemaDepthLevel()).isEqualTo(0);
|
||||
assertThat(config.getInterDocumentCrossReferencesPrefix()).isNull();
|
||||
assertThat(config.getMarkupLanguage()).isEqualTo(MarkupLanguage.ASCIIDOC);
|
||||
@@ -91,7 +91,7 @@ public class Swagger2MarkupConfigTest {
|
||||
assertThat(config.getOperationDescriptionsUri()).isEqualTo(URI.create("operationDescriptions"));
|
||||
assertThat(config.isDefinitionDescriptionsEnabled()).isTrue();
|
||||
assertThat(config.getDefinitionDescriptionsUri()).isEqualTo(URI.create("definitionDescriptions"));
|
||||
assertThat(config.isExamplesEnabled()).isTrue();
|
||||
assertThat(config.isGeneratedExamplesEnabled()).isTrue();
|
||||
assertThat(config.getInlineSchemaDepthLevel()).isEqualTo(2);
|
||||
assertThat(config.getInterDocumentCrossReferencesPrefix()).isEqualTo("xrefPrefix");
|
||||
assertThat(config.getMarkupLanguage()).isEqualTo(MarkupLanguage.MARKDOWN);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
swagger2markup.markupLanguage=MARKDOWN
|
||||
swagger2markup.examplesEnabled=true
|
||||
swagger2markup.generatedExamplesEnabled=true
|
||||
swagger2markup.schemasUri=schemas
|
||||
swagger2markup.schemasEnabled=true
|
||||
swagger2markup.operationDescriptionsEnabled=true
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user