diff --git a/build.gradle b/build.gradle
index 775a57c8..fcb470ea 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,8 +4,8 @@ buildscript {
mavenCentral()
}
dependencies {
- classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.2'
- classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.8'
+ classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
+ classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.10.1'
classpath 'io.spring.gradle:dependency-management-plugin:0.5.5.RELEASE'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
classpath 'org.asciidoctor:asciidoctorj:1.5.2'
@@ -55,6 +55,7 @@ dependencies {
testCompile 'org.asciidoctor:asciidoctorj:1.5.4'
testCompile 'ch.qos.logback:logback-classic'
testCompile 'org.assertj:assertj-core'
+ testCompile 'com.sksamuel.diff:diff'
}
dependencyManagement {
@@ -67,6 +68,7 @@ dependencyManagement {
dependency "org.slf4j:slf4j-api:1.7.12"
dependency "ch.qos.logback:logback-classic:1.1.2"
dependency "org.assertj:assertj-core:2.2.0"
+ dependency "com.sksamuel.diff:diff:1.1.11"
}
}
diff --git a/src/main/java/io/github/robwin/swagger2markup/builder/document/DefinitionsDocument.java b/src/main/java/io/github/robwin/swagger2markup/builder/document/DefinitionsDocument.java
index 7a1f88c9..c8cce0ad 100644
--- a/src/main/java/io/github/robwin/swagger2markup/builder/document/DefinitionsDocument.java
+++ b/src/main/java/io/github/robwin/swagger2markup/builder/document/DefinitionsDocument.java
@@ -40,6 +40,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
+import static io.github.robwin.swagger2markup.utils.IOUtils.normalizeName;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
diff --git a/src/main/java/io/github/robwin/swagger2markup/builder/document/MarkupDocument.java b/src/main/java/io/github/robwin/swagger2markup/builder/document/MarkupDocument.java
index aef08a57..94ae1a7f 100644
--- a/src/main/java/io/github/robwin/swagger2markup/builder/document/MarkupDocument.java
+++ b/src/main/java/io/github/robwin/swagger2markup/builder/document/MarkupDocument.java
@@ -28,8 +28,8 @@ import io.github.robwin.swagger2markup.type.Type;
import io.github.robwin.swagger2markup.utils.IOUtils;
import io.github.robwin.swagger2markup.utils.PropertyUtils;
import io.swagger.models.properties.Property;
+import io.swagger.util.Json;
import org.apache.commons.collections.MapUtils;
-import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,7 +38,6 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.*;
-import java.util.regex.Pattern;
import static org.apache.commons.lang3.StringUtils.defaultString;
@@ -47,15 +46,12 @@ import static org.apache.commons.lang3.StringUtils.defaultString;
*/
public abstract class MarkupDocument {
- private static final Pattern NAME_FORBIDDEN_PATTERN = Pattern.compile("[^0-9A-Za-z-_]+");
-
protected final String DEFAULT_COLUMN;
protected final String EXAMPLE_COLUMN;
protected final String REQUIRED_COLUMN;
protected final String SCHEMA_COLUMN;
protected final String NAME_COLUMN;
protected final String DESCRIPTION_COLUMN;
-
protected final String SCOPES_COLUMN;
protected final String DESCRIPTION;
protected final String PRODUCES;
@@ -118,21 +114,6 @@ public abstract class MarkupDocument {
markupDocBuilder.writeToFile(file, charset);
}
- /**
- * Create a normalized name from an arbitrary string.
- * Paths separators are replaced, so this function can't be applied on a whole path, but must be called on each path sections.
- *
- * @param name current name of the file
- * @return a normalized filename
- */
- public static String normalizeName(String name) {
- String fileName = NAME_FORBIDDEN_PATTERN.matcher(name).replaceAll("_");
- fileName = fileName.replaceAll(String.format("([%1$s])([%1$s]+)", "-_"), "$1");
- fileName = StringUtils.strip(fileName, "_-");
- fileName = fileName.trim();
- return fileName;
- }
-
/**
* Build a generic property table for any ObjectType
*
@@ -175,13 +156,15 @@ public abstract class MarkupDocument {
}
}
+ Object example = PropertyUtils.getExample(globalContext.config.isGeneratedExamplesEnabled(), property, markupDocBuilder);
+
List content = Arrays.asList(
propertyName,
propertyDescriptor.getDescription(property, propertyName),
Boolean.toString(property.getRequired()),
propertyType.displaySchema(docBuilder),
PropertyUtils.getDefaultValue(property),
- PropertyUtils.getExample(property, markupDocBuilder)
+ example != null ? Json.pretty(example) : ""
);
cells.add(content);
}
diff --git a/src/main/java/io/github/robwin/swagger2markup/builder/document/PathsDocument.java b/src/main/java/io/github/robwin/swagger2markup/builder/document/PathsDocument.java
index e8490e97..cb367497 100644
--- a/src/main/java/io/github/robwin/swagger2markup/builder/document/PathsDocument.java
+++ b/src/main/java/io/github/robwin/swagger2markup/builder/document/PathsDocument.java
@@ -49,6 +49,7 @@ import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;
+import static io.github.robwin.swagger2markup.utils.IOUtils.normalizeName;
import static io.github.robwin.swagger2markup.utils.TagUtils.convertTagsListToMap;
import static io.github.robwin.swagger2markup.utils.TagUtils.getTagDescription;
import static org.apache.commons.lang3.StringUtils.defaultString;
@@ -66,6 +67,7 @@ public class PathsDocument extends MarkupDocument {
private final String PARAMETERS;
private final String BODY_PARAMETER;
private final String RESPONSES;
+ private final String HEADERS_COLUMN;
private final String EXAMPLE_REQUEST;
private final String EXAMPLE_RESPONSE;
@@ -91,6 +93,7 @@ public class PathsDocument extends MarkupDocument {
PARAMETERS = labels.getString("parameters");
BODY_PARAMETER = labels.getString("body_parameter");
RESPONSES = labels.getString("responses");
+ HEADERS_COLUMN = labels.getString("headers_column");
EXAMPLE_REQUEST = labels.getString("example_request");
EXAMPLE_RESPONSE = labels.getString("example_response");
SECURITY = labels.getString("security");
diff --git a/src/main/java/io/github/robwin/swagger2markup/utils/ExamplesUtil.java b/src/main/java/io/github/robwin/swagger2markup/utils/ExamplesUtil.java
index 834fb7d1..d6cb87a8 100644
--- a/src/main/java/io/github/robwin/swagger2markup/utils/ExamplesUtil.java
+++ b/src/main/java/io/github/robwin/swagger2markup/utils/ExamplesUtil.java
@@ -58,7 +58,7 @@ public class ExamplesUtil {
example = generateExampleForRefModel(generateMissingExamples, simpleRef, definitions, markupDocBuilder);
}
if (example == null && generateMissingExamples) {
- example = PropertyUtils.exampleFromType(schema.getType(), schema, markupDocBuilder);
+ example = PropertyUtils.generateExample(schema, markupDocBuilder);
}
}
}
@@ -117,11 +117,11 @@ public class ExamplesUtil {
if (item != null) {
abstractSerializableParameterExample = item.getExample();
if (abstractSerializableParameterExample == null) {
- abstractSerializableParameterExample = PropertyUtils.exampleFromType(item.getType(), item, markupDocBuilder);
+ abstractSerializableParameterExample = PropertyUtils.generateExample(item, markupDocBuilder);
}
}
if (abstractSerializableParameterExample == null) {
- abstractSerializableParameterExample = PropertyUtils.exampleFromType(((AbstractSerializableParameter) parameter).getType(), null, markupDocBuilder);
+ abstractSerializableParameterExample = ParameterUtils.generateExample((AbstractSerializableParameter)parameter);
}
}
if (parameter instanceof PathParameter) {
@@ -221,7 +221,7 @@ public class ExamplesUtil {
}
if (exampleObject == null) {
Property valueProperty = property.getValue();
- exampleObject = PropertyUtils.exampleFromType(valueProperty.getType(), valueProperty, markupDocBuilder);
+ exampleObject = PropertyUtils.generateExample(valueProperty, markupDocBuilder);
}
}
exampleMap.put(property.getKey(), exampleObject);
@@ -238,7 +238,7 @@ public class ExamplesUtil {
if (valueProperty.getExample() != null) {
return valueProperty.getExample();
}
- exampleMap.put("string", PropertyUtils.exampleFromType(valueProperty.getType(), valueProperty, markupDocBuilder));
+ exampleMap.put("string", PropertyUtils.generateExample(valueProperty, markupDocBuilder));
return exampleMap;
}
@@ -256,7 +256,7 @@ public class ExamplesUtil {
} else if (itemProperty instanceof RefProperty) {
return new Object[]{generateExampleForRefModel(true, ((RefProperty) itemProperty).getSimpleRef(), definitions, markupDocBuilder)};
} else {
- return new Object[]{PropertyUtils.exampleFromType(itemProperty.getType(), itemProperty, markupDocBuilder)};
+ return new Object[]{PropertyUtils.generateExample(itemProperty, markupDocBuilder)};
}
}
}
@@ -276,29 +276,8 @@ public class ExamplesUtil {
} else if (property instanceof RefProperty) {
return new Object[]{generateExampleForRefModel(true, ((RefProperty) property).getSimpleRef(), definitions, markupDocBuilder)};
} else {
- return new Object[]{PropertyUtils.exampleFromType(property.getType(), property, markupDocBuilder)};
+ return new Object[]{PropertyUtils.generateExample(property, markupDocBuilder)};
}
}
- public static Object convertStringToType(String value, String type) {
- if (value == null) {
- return null;
- }
- try {
- switch (type) {
- case "integer":
- return new Integer(value);
- case "number":
- return new Float(value);
- case "boolean":
- return new Boolean(value);
- case "string":
- return value;
- default:
- return value;
- }
- } catch (NumberFormatException e) {
- throw new RuntimeException(String.format("Value '%s' cannot be converted to '%s'", value, type), e);
- }
- }
}
diff --git a/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java b/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java
index edf412f5..a38f6948 100644
--- a/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java
+++ b/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java
@@ -16,6 +16,7 @@
package io.github.robwin.swagger2markup.utils;
import com.google.common.base.Function;
+import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.swagger2markup.type.*;
import io.swagger.models.Model;
import io.swagger.models.parameters.AbstractSerializableParameter;
@@ -87,4 +88,24 @@ public final class ParameterUtils {
return defaultString(defaultValue);
}
+ /**
+ * Generate a default example value for parameter.
+ *
+ * @param parameter parameter
+ * @return a generated example for the parameter
+ */
+ public static Object generateExample(AbstractSerializableParameter parameter) {
+ switch (parameter.getType()) {
+ case "integer":
+ return 0;
+ case "number":
+ return 0.0;
+ case "boolean":
+ return true;
+ case "string":
+ return "string";
+ default:
+ return parameter.getType();
+ }
+ }
}
diff --git a/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java b/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java
index 357ae9ae..b83a9e64 100644
--- a/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java
+++ b/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java
@@ -20,7 +20,6 @@ import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.swagger2markup.type.*;
import io.swagger.models.properties.*;
import io.swagger.models.refs.RefFormat;
-import io.swagger.util.Json;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.Validate;
@@ -106,37 +105,47 @@ public final class PropertyUtils {
/**
* Return example display string for the given {@code property}.
*
- * @param property property
+ * @param property property
* @param markupDocBuilder doc builder
* @return property example display string
*/
- public static String getExample(Property property, MarkupDocBuilder markupDocBuilder) {
+ public static Object getExample(boolean generateMissingExamples, Property property, MarkupDocBuilder markupDocBuilder) {
Validate.notNull(property, "property must not be null");
- Object examplesValue;
+ Object examplesValue = null;
if (property.getExample() != null) {
examplesValue = property.getExample();
} else if (property instanceof MapProperty) {
- Map exampleMap = new HashMap<>();
Property additionalProperty = ((MapProperty) property).getAdditionalProperties();
if (additionalProperty.getExample() != null) {
examplesValue = additionalProperty.getExample();
- } else {
- exampleMap.put("string", exampleFromType(additionalProperty.getType(), additionalProperty, markupDocBuilder));
- examplesValue = Json.pretty(exampleMap);
+ } else if (generateMissingExamples) {
+ Map exampleMap = new HashMap<>();
+ exampleMap.put("string", generateExample(additionalProperty, markupDocBuilder));
+ examplesValue = exampleMap;
}
} else if (property instanceof ArrayProperty) {
- List