diff --git a/README.adoc b/README.adoc index 28bc4893..834f8171 100644 --- a/README.adoc +++ b/README.adoc @@ -1,6 +1,6 @@ = Swagger2Markup :author: Robert Winkler -:version: 0.5.3 +:version: 0.6.0 :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: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"] @@ -43,7 +43,7 @@ The project is published in JCenter and Maven Central. io.github.robwin swagger2markup - 0.5.3 + 0.6.0 ---- @@ -55,7 +55,7 @@ repositories { jcenter() } -compile "io.github.robwin:swagger2markup:0.5.3" +compile "io.github.robwin:swagger2markup:0.6.0" ---- === Using Swagger2Markup diff --git a/RELEASENOTES.adoc b/RELEASENOTES.adoc index 871c4d82..b3f4c281 100644 --- a/RELEASENOTES.adoc +++ b/RELEASENOTES.adoc @@ -32,12 +32,16 @@ == Version 0.5.0 * Support for including hand-written descriptions instead of using Swagger Annotations for descriptions -== Version 0.5.1 +=== Version 0.5.1 * Bugfix: Definition name must be lowercase so that descriptions file can be found -== Version 0.5.2 +=== 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 \ No newline at end of file +=== Version 0.5.3 +* Fixed compiler warning: [options] bootstrap class path not set in conjunction with -source 1.7 + +== Version 0.6.0 +* Updated swagger-parser from v1.0.5 to v1.0.6 +* Support for default values in Parameters and Model properties \ No newline at end of file diff --git a/build.gradle b/build.gradle index 62bca51e..fceaaa62 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ buildscript { } } description = 'swagger2markup Build' -version = '0.5.3' +version = '0.6.0' group = 'io.github.robwin' apply plugin: 'java' @@ -54,7 +54,7 @@ dependencies { dependencyManagement { dependencies { dependency "io.github.robwin:markup-document-builder:0.1.4" - dependency "io.swagger:swagger-compat-spec-parser:1.0.5" + dependency "io.swagger:swagger-compat-spec-parser:1.0.6" dependency "commons-collections:commons-collections:3.2.1" dependency "commons-io:commons-io:2.4" dependency "junit:junit:4.11" diff --git a/src/main/java/io/github/robwin/swagger2markup/Swagger2MarkupConverter.java b/src/main/java/io/github/robwin/swagger2markup/Swagger2MarkupConverter.java index d52a8ca1..46d66be6 100644 --- a/src/main/java/io/github/robwin/swagger2markup/Swagger2MarkupConverter.java +++ b/src/main/java/io/github/robwin/swagger2markup/Swagger2MarkupConverter.java @@ -172,6 +172,9 @@ public class Swagger2MarkupConverter { */ Builder(String swaggerLocation){ swagger = new SwaggerParser().read(swaggerLocation); + if(swagger == null){ + throw new IllegalArgumentException("Failed to read the Swagger file. "); + } } /** 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 2117873c..2123ff6b 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 @@ -144,14 +144,18 @@ public class DefinitionsDocument extends MarkupDocument { private void propertiesSection(String definitionName, Model model) throws IOException { Map properties = model.getProperties(); List headerAndContent = new ArrayList<>(); - List header = Arrays.asList(NAME_COLUMN, DESCRIPTION_COLUMN, SCHEMA_COLUMN, REQUIRED_COLUMN); + List header = Arrays.asList(NAME_COLUMN, DESCRIPTION_COLUMN, REQUIRED_COLUMN, SCHEMA_COLUMN, DEFAULT_COLUMN); headerAndContent.add(StringUtils.join(header, DELIMITER)); if(MapUtils.isNotEmpty(properties)){ for (Map.Entry propertyEntry : properties.entrySet()) { Property property = propertyEntry.getValue(); - String type = PropertyUtils.getType(property, markupLanguage); String propertyName = propertyEntry.getKey(); - List content = Arrays.asList(propertyName, propertyDescription(definitionName, propertyName, property), type, Boolean.toString(property.getRequired())); + List content = Arrays.asList( + propertyName, + propertyDescription(definitionName, propertyName, property), + Boolean.toString(property.getRequired()), + PropertyUtils.getType(property, markupLanguage), + PropertyUtils.getDefaultValue(property)); headerAndContent.add(StringUtils.join(content, DELIMITER)); } this.markupDocBuilder.tableWithHeaderRow(headerAndContent); 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 669584b3..070d1aad 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 @@ -34,6 +34,7 @@ import java.nio.charset.Charset; public abstract class MarkupDocument { protected static final String DELIMITER = "|"; + protected static final String DEFAULT_COLUMN = "Default"; protected static final String REQUIRED_COLUMN = "Required"; protected static final String SCHEMA_COLUMN = "Schema"; protected static final String NAME_COLUMN = "Name"; 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 0b048efe..16152a7e 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 @@ -195,13 +195,18 @@ public class PathsDocument extends MarkupDocument { if(CollectionUtils.isNotEmpty(parameters)){ List headerAndContent = new ArrayList<>(); // Table header row - List header = Arrays.asList(TYPE_COLUMN, NAME_COLUMN, DESCRIPTION_COLUMN, REQUIRED_COLUMN, SCHEMA_COLUMN); + List header = Arrays.asList(TYPE_COLUMN, NAME_COLUMN, DESCRIPTION_COLUMN, REQUIRED_COLUMN, SCHEMA_COLUMN, DEFAULT_COLUMN); headerAndContent.add(StringUtils.join(header, DELIMITER)); for(Parameter parameter : parameters){ String type = ParameterUtils.getType(parameter, markupLanguage); String parameterType = WordUtils.capitalize(parameter.getIn() + PARAMETER); // Table content row - List content = Arrays.asList(parameterType, parameter.getName(), parameterDescription(operation, parameter), Boolean.toString(parameter.getRequired()), type); + List content = Arrays.asList( + parameterType, + parameter.getName(), + parameterDescription(operation, parameter), + Boolean.toString(parameter.getRequired()), type, + ParameterUtils.getDefaultValue(parameter)); headerAndContent.add(StringUtils.join(content, DELIMITER)); } this.markupDocBuilder.sectionTitleLevel3(PARAMETERS); 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 1db0b648..b329972f 100644 --- a/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java +++ b/src/main/java/io/github/robwin/swagger2markup/utils/ParameterUtils.java @@ -19,7 +19,10 @@ package io.github.robwin.swagger2markup.utils; import com.wordnik.swagger.models.Model; -import com.wordnik.swagger.models.parameters.*; +import com.wordnik.swagger.models.parameters.AbstractSerializableParameter; +import com.wordnik.swagger.models.parameters.BodyParameter; +import com.wordnik.swagger.models.parameters.Parameter; +import com.wordnik.swagger.models.parameters.RefParameter; import io.github.robwin.markup.builder.MarkupLanguage; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -38,60 +41,17 @@ public final class ParameterUtils { 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 enums = queryParameter.getEnum(); + else if(parameter instanceof AbstractSerializableParameter){ + AbstractSerializableParameter serializableParameter = (AbstractSerializableParameter)parameter; + List enums = serializableParameter.getEnum(); if(CollectionUtils.isNotEmpty(enums)){ type = "enum" + " (" + StringUtils.join(enums, ", ") + ")"; }else{ - type = getTypeWithFormat(queryParameter.getType(), queryParameter.getFormat()); + type = getTypeWithFormat(serializableParameter.getType(), serializableParameter.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 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 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 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; + String collectionFormat = serializableParameter.getCollectionFormat(); + type = collectionFormat + " " + PropertyUtils.getType(serializableParameter.getItems(), markupLanguage) + " " + type; } } else if(parameter instanceof RefParameter){ @@ -101,16 +61,27 @@ public final class ParameterUtils { default: return refParameter.getSimpleRef(); } } - return type; + return StringUtils.defaultString(type); } private static String getTypeWithFormat(String typeWithoutFormat, String format) { String type; if(StringUtils.isNotBlank(format)){ - type = typeWithoutFormat + " (" + format + ")"; + type = StringUtils.defaultString(typeWithoutFormat) + " (" + format + ")"; }else{ - type = typeWithoutFormat; + type = StringUtils.defaultString(typeWithoutFormat); } return type; } + + public static String getDefaultValue(Parameter parameter){ + Validate.notNull(parameter, "property must not be null!"); + String defaultValue = ""; + if(parameter instanceof AbstractSerializableParameter){ + AbstractSerializableParameter serializableParameter = (AbstractSerializableParameter)parameter; + defaultValue = serializableParameter.getDefaultValue(); + } + return StringUtils.defaultString(defaultValue); + } + } 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 68fd336b..900d7b55 100644 --- a/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java +++ b/src/main/java/io/github/robwin/swagger2markup/utils/PropertyUtils.java @@ -18,16 +18,14 @@ */ 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 com.wordnik.swagger.models.properties.*; 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; +import java.util.Objects; public final class PropertyUtils { @@ -55,11 +53,41 @@ public final class PropertyUtils { } else{ if(StringUtils.isNotBlank(property.getFormat())){ - type = property.getType() + " (" + property.getFormat() + ")"; + type = StringUtils.defaultString(property.getType()) + " (" + property.getFormat() + ")"; }else{ type = property.getType(); } } - return type; + return StringUtils.defaultString(type); + } + + public static String getDefaultValue(Property property){ + Validate.notNull(property, "property must not be null!"); + String defaultValue = ""; + if(property instanceof BooleanProperty){ + BooleanProperty booleanProperty = (BooleanProperty)property; + defaultValue = Objects.toString(booleanProperty.getDefault(), ""); + }else if(property instanceof StringProperty){ + StringProperty stringProperty = (StringProperty)property; + defaultValue = Objects.toString(stringProperty.getDefault(), ""); + }else if(property instanceof DoubleProperty){ + DoubleProperty doubleProperty = (DoubleProperty)property; + defaultValue = Objects.toString(doubleProperty.getDefault(), ""); + }else if(property instanceof FloatProperty){ + FloatProperty floatProperty = (FloatProperty)property; + defaultValue = Objects.toString(floatProperty.getDefault(), ""); + }else if(property instanceof IntegerProperty){ + IntegerProperty integerProperty = (IntegerProperty)property; + defaultValue = Objects.toString(integerProperty.getDefault(), ""); + } + else if(property instanceof LongProperty){ + LongProperty longProperty = (LongProperty)property; + defaultValue = Objects.toString(longProperty.getDefault(), ""); + } + else if(property instanceof UUIDProperty){ + UUIDProperty uuidProperty = (UUIDProperty)property; + defaultValue = Objects.toString(uuidProperty.getDefault(), ""); + } + return defaultValue; } } diff --git a/src/test/resources/json/swagger.json b/src/test/resources/json/swagger.json index 098b4f14..3954b395 100644 --- a/src/test/resources/json/swagger.json +++ b/src/test/resources/json/swagger.json @@ -563,14 +563,16 @@ "name": "username", "description": "The user name for login", "required": false, - "type": "string" + "type": "string", + "default": "testUser" }, { "in": "query", "name": "password", "description": "The password for login in clear text", "required": false, - "type": "string" + "type": "string", + "default": "testPassword" } ], "responses": { @@ -623,7 +625,8 @@ "name": "username", "description": "The name that needs to be fetched. Use user1 for testing.", "required": true, - "type": "string" + "type": "string", + "default": "testUser" } ], "responses": {