From 64fb2f69e44b32136d87a67e558d8b3ba50da60a Mon Sep 17 00:00:00 2001 From: b_sapir Date: Wed, 20 May 2015 17:31:17 +0300 Subject: [PATCH 1/7] Support binary input and output --- .../models/properties/PropertyBuilder.java | 94 +++++++------------ .../models/properties/StringProperty.java | 2 +- 2 files changed, 33 insertions(+), 63 deletions(-) diff --git a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java index ad70703181..c62e80389d 100644 --- a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java +++ b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java @@ -3,29 +3,33 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; public class PropertyBuilder { static Logger LOGGER = LoggerFactory.getLogger(PropertyBuilder.class); - public static Property build(String type, String format, Map args) { + public static Property build(String type, String format, Map args) { if(args == null) { - args = Collections.emptyMap(); + args = new HashMap(); } - List _enum = PropertyId.ENUM.findValue(args); - String title = PropertyId.TITLE.findValue(args); - String description = PropertyId.DESCRIPTION.findValue(args); - String _default = PropertyId.DEFAULT.findValue(args); - String pattern = PropertyId.PATTERN.findValue(args); - Integer minLength = PropertyId.MIN_LENGTH.findValue(args); - Integer maxLength = PropertyId.MAX_LENGTH.findValue(args); - Double minimum = PropertyId.MINIMUM.findValue(args); - Double maximum = PropertyId.MAXIMUM.findValue(args); - Boolean exclusiveMinimum = PropertyId.EXCLUSIVE_MINIMUM.findValue(args); - Boolean exclusiveMaximum = PropertyId.EXCLUSIVE_MAXIMUM.findValue(args); + List _enum = (List) args.get("enum"); + String title = (String)args.get("title"); + String description = (String)args.get("description"); + String _default = (String)args.get("default"); + String pattern = (String)args.get("pattern"); + String discriminator = (String)args.get("discriminator"); + Integer minItems = (Integer)args.get("minItems"); + Integer maxItems = (Integer)args.get("maxItems"); + Integer minProperties = (Integer)args.get("minProperties"); + Integer maxProperties = (Integer)args.get("maxProperties"); + Integer minLength = (Integer)args.get("minLength"); + Integer maxLength = (Integer)args.get("maxLength"); + Double minimum = (Double)args.get("minimum"); + Double maximum = (Double)args.get("maximum"); + Double exclusiveMinimum = (Double)args.get("exclusiveMinimum"); + Double exclusiveMaximum = (Double)args.get("exclusiveMaximum"); + Boolean uniqueItems = (Boolean)args.get("uniqueItems"); AbstractProperty property = null; if(BooleanProperty.isType(type, format)) { @@ -42,7 +46,7 @@ public static Property build(String type, String format, Map .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); } if(FloatProperty.isType(type, format)) { property = new FloatProperty() @@ -50,7 +54,7 @@ public static Property build(String type, String format, Map .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); } if(FileProperty.isType(type, format)) { property = new FileProperty(); @@ -60,14 +64,14 @@ public static Property build(String type, String format, Map .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); if(IntegerProperty.isType(type, format)) { property = new IntegerProperty() ._default(_default) .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); } if(LongProperty.isType(type, format)) { property = new LongProperty() @@ -75,7 +79,7 @@ public static Property build(String type, String format, Map .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); } if(RefProperty.isType(type, format)) property = new RefProperty(); @@ -100,12 +104,15 @@ public static Property build(String type, String format, Map .maxLength(maxLength) .pattern(pattern); } + if(ByteArrayProperty.isType(type, format)){ + property = new ByteArrayProperty(); + } // general properties if(property != null) { property .title(title) .description(description); - String example = PropertyId.EXAMPLE.findValue (args); + String example = (String)args.get("example"); if (example != null) { property.setExample(example); } @@ -119,9 +126,9 @@ public static Property build(String type, String format, Map .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMaximum); + .exclusiveMaximum(exclusiveMinimum); } - if(ObjectProperty.isType(type) && format == null) { + if("object".equals(type) && format == null) { // fall back to Map if type is object and format is missing LOGGER.debug("no format specified for object type, falling back to object"); property = new ObjectProperty(); @@ -130,41 +137,4 @@ public static Property build(String type, String format, Map LOGGER.debug("no property for " + type + ", " + format); return property; } - - public enum PropertyId { - ENUM("enum"), - TITLE("title"), - DESCRIPTION("description"), - DEFAULT("default"), - PATTERN("pattern"), - DESCRIMINATOR("discriminator"), - MIN_ITEMS("minItems"), - MAX_ITEMS("maxItems"), - MIN_PROPERTIES("minProperties"), - MAX_PROPERTIES("maxProperties"), - MIN_LENGTH("minLength"), - MAX_LENGTH("maxLength"), - MINIMUM("minimum"), - MAXIMUM("maximum"), - EXCLUSIVE_MINIMUM("exclusiveMinimum"), - EXCLUSIVE_MAXIMUM("exclusiveMaximum"), - UNIQUE_ITEMS("uniqueItems"), - EXAMPLE("example"), - TYPE("type"), - FORMAT("format"); - - private String propertyName; - - private PropertyId(String propertyName) { - this.propertyName = propertyName; - } - - public String getPropertyName() { - return propertyName; - } - - public T findValue(Map args) { - return (T) args.get(this); - } - } -} +} \ No newline at end of file diff --git a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/StringProperty.java b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/StringProperty.java index c0dc7b4cc8..b329496a2c 100644 --- a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/StringProperty.java +++ b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/StringProperty.java @@ -85,7 +85,7 @@ public void setDefault(String _default) { //TODO: implement additional formats public static boolean isType(String type, String format) { - boolean formatMatchStringType = "uri".equals(format) || "byte".equals(format) || "url".equals(format); + boolean formatMatchStringType = "uri".equals(format) || "url".equals(format); if("string".equals(type) && (format == null || formatMatchStringType)) return true; else return false; From 71a04d0418bd5d0bf0f528a120c70646dc58d3dc Mon Sep 17 00:00:00 2001 From: b_sapir Date: Wed, 20 May 2015 18:07:47 +0300 Subject: [PATCH 2/7] Support binary input and output --- .../models/properties/PropertyBuilder.java | 93 +++++++++++++------ 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java index c62e80389d..017469aa54 100644 --- a/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java +++ b/modules/swagger-models/src/main/java/com/wordnik/swagger/models/properties/PropertyBuilder.java @@ -3,33 +3,29 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Map; public class PropertyBuilder { static Logger LOGGER = LoggerFactory.getLogger(PropertyBuilder.class); - public static Property build(String type, String format, Map args) { + public static Property build(String type, String format, Map args) { if(args == null) { - args = new HashMap(); + args = Collections.emptyMap(); } - List _enum = (List) args.get("enum"); - String title = (String)args.get("title"); - String description = (String)args.get("description"); - String _default = (String)args.get("default"); - String pattern = (String)args.get("pattern"); - String discriminator = (String)args.get("discriminator"); - Integer minItems = (Integer)args.get("minItems"); - Integer maxItems = (Integer)args.get("maxItems"); - Integer minProperties = (Integer)args.get("minProperties"); - Integer maxProperties = (Integer)args.get("maxProperties"); - Integer minLength = (Integer)args.get("minLength"); - Integer maxLength = (Integer)args.get("maxLength"); - Double minimum = (Double)args.get("minimum"); - Double maximum = (Double)args.get("maximum"); - Double exclusiveMinimum = (Double)args.get("exclusiveMinimum"); - Double exclusiveMaximum = (Double)args.get("exclusiveMaximum"); - Boolean uniqueItems = (Boolean)args.get("uniqueItems"); + List _enum = PropertyId.ENUM.findValue(args); + String title = PropertyId.TITLE.findValue(args); + String description = PropertyId.DESCRIPTION.findValue(args); + String _default = PropertyId.DEFAULT.findValue(args); + String pattern = PropertyId.PATTERN.findValue(args); + Integer minLength = PropertyId.MIN_LENGTH.findValue(args); + Integer maxLength = PropertyId.MAX_LENGTH.findValue(args); + Double minimum = PropertyId.MINIMUM.findValue(args); + Double maximum = PropertyId.MAXIMUM.findValue(args); + Boolean exclusiveMinimum = PropertyId.EXCLUSIVE_MINIMUM.findValue(args); + Boolean exclusiveMaximum = PropertyId.EXCLUSIVE_MAXIMUM.findValue(args); AbstractProperty property = null; if(BooleanProperty.isType(type, format)) { @@ -46,7 +42,7 @@ public static Property build(String type, String format, Map arg .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); } if(FloatProperty.isType(type, format)) { property = new FloatProperty() @@ -54,7 +50,7 @@ public static Property build(String type, String format, Map arg .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); } if(FileProperty.isType(type, format)) { property = new FileProperty(); @@ -64,14 +60,14 @@ public static Property build(String type, String format, Map arg .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); if(IntegerProperty.isType(type, format)) { property = new IntegerProperty() ._default(_default) .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); } if(LongProperty.isType(type, format)) { property = new LongProperty() @@ -79,7 +75,7 @@ public static Property build(String type, String format, Map arg .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); } if(RefProperty.isType(type, format)) property = new RefProperty(); @@ -105,14 +101,14 @@ public static Property build(String type, String format, Map arg .pattern(pattern); } if(ByteArrayProperty.isType(type, format)){ - property = new ByteArrayProperty(); + property = new ByteArrayProperty(); } // general properties if(property != null) { property .title(title) .description(description); - String example = (String)args.get("example"); + String example = PropertyId.EXAMPLE.findValue (args); if (example != null) { property.setExample(example); } @@ -126,9 +122,9 @@ public static Property build(String type, String format, Map arg .minimum(minimum) .maximum(maximum) .exclusiveMinimum(exclusiveMinimum) - .exclusiveMaximum(exclusiveMinimum); + .exclusiveMaximum(exclusiveMaximum); } - if("object".equals(type) && format == null) { + if(ObjectProperty.isType(type) && format == null) { // fall back to Map if type is object and format is missing LOGGER.debug("no format specified for object type, falling back to object"); property = new ObjectProperty(); @@ -137,4 +133,41 @@ public static Property build(String type, String format, Map arg LOGGER.debug("no property for " + type + ", " + format); return property; } -} \ No newline at end of file + + public enum PropertyId { + ENUM("enum"), + TITLE("title"), + DESCRIPTION("description"), + DEFAULT("default"), + PATTERN("pattern"), + DESCRIMINATOR("discriminator"), + MIN_ITEMS("minItems"), + MAX_ITEMS("maxItems"), + MIN_PROPERTIES("minProperties"), + MAX_PROPERTIES("maxProperties"), + MIN_LENGTH("minLength"), + MAX_LENGTH("maxLength"), + MINIMUM("minimum"), + MAXIMUM("maximum"), + EXCLUSIVE_MINIMUM("exclusiveMinimum"), + EXCLUSIVE_MAXIMUM("exclusiveMaximum"), + UNIQUE_ITEMS("uniqueItems"), + EXAMPLE("example"), + TYPE("type"), + FORMAT("format"); + + private String propertyName; + + private PropertyId(String propertyName) { + this.propertyName = propertyName; + } + + public String getPropertyName() { + return propertyName; + } + + public T findValue(Map args) { + return (T) args.get(this); + } + } +} From 06d91feaeec486134b11804968ac94b6a655424a Mon Sep 17 00:00:00 2001 From: b_sapir Date: Sun, 14 Jun 2015 13:41:29 +0300 Subject: [PATCH 3/7] Manually merge changes that were done on the master branch in order to support the ByteArray type --- .../models/properties/ByteArrayProperty.java | 35 +++++++++++++++++++ .../models/properties/PropertyBuilder.java | 11 ++++++ .../models/properties/StringProperty.java | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java index e69de29bb2..bb922dbedd 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java @@ -0,0 +1,35 @@ +package io.swagger.models.properties; + +import io.swagger.models.Xml; + +import java.util.*; + +public class ByteArrayProperty extends AbstractProperty implements Property { + + protected byte[] _default; + + public ByteArrayProperty() { + super.type = "byte-array"; + } + + + + public ByteArrayProperty _default(byte[] _default) { + // this._default = _default; + return this; + } + + + public byte[] getDefault() { + return _default; + } + public void setDefault(byte[] _default) { + //this._default = _default; + } + + public static boolean isType(String type, String format) { + if("string".equals(type) && "byte".equals(format)) + return true; + else return false; + } +} \ No newline at end of file diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/PropertyBuilder.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/PropertyBuilder.java index 41fbdf96a7..c01e831dc8 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/PropertyBuilder.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/PropertyBuilder.java @@ -162,6 +162,17 @@ public Model toModel(Property property) { } }, + BYTE_ARRAY(ByteArrayProperty.class) { + @Override + protected boolean isType(String type, String format) { + return ByteArrayProperty.isType(type, format); + } + + @Override + protected ByteArrayProperty create() { + return new ByteArrayProperty(); + } + }, DATE(DateProperty.class) { @Override protected boolean isType(String type, String format) { diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java index 52c8df708e..4c4ac8aafb 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java @@ -22,7 +22,7 @@ public StringProperty(String format) { //TODO: implement additional formats public static boolean isType(String type, String format) { - boolean formatMatchStringType = "uri".equals(format) || "byte".equals(format) || "url".equals(format); + boolean formatMatchStringType = "uri".equals(format) || "url".equals(format); if ("string".equals(type) && (format == null || formatMatchStringType)) { return true; } else { From 3d3a4ba22492b93635d9a69eb60376ac142a6dd2 Mon Sep 17 00:00:00 2001 From: b_sapir Date: Sun, 14 Jun 2015 14:30:45 +0300 Subject: [PATCH 4/7] fix type and format strings for serialization --- .../models/properties/ByteArrayProperty.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java index bb922dbedd..3a86f66e00 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java @@ -6,25 +6,10 @@ public class ByteArrayProperty extends AbstractProperty implements Property { - protected byte[] _default; public ByteArrayProperty() { - super.type = "byte-array"; - } - - - - public ByteArrayProperty _default(byte[] _default) { - // this._default = _default; - return this; - } - - - public byte[] getDefault() { - return _default; - } - public void setDefault(byte[] _default) { - //this._default = _default; + super.type = "string"; + super.format = "byte"; } public static boolean isType(String type, String format) { @@ -32,4 +17,13 @@ public static boolean isType(String type, String format) { return true; else return false; } + public ByteArrayProperty xml(Xml xml) { + this.setXml(xml); + return this; + } + + public ByteArrayProperty example(String example) { + this.setExample(example); + return this; + } } \ No newline at end of file From a3c2f11f503ea804343939bb330a51113d26a89b Mon Sep 17 00:00:00 2001 From: b_sapir Date: Sun, 14 Jun 2015 14:31:19 +0300 Subject: [PATCH 5/7] Update test to include new ByteArray property type --- modules/swagger-core/src/test/scala/ModelSerializerTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-core/src/test/scala/ModelSerializerTest.scala b/modules/swagger-core/src/test/scala/ModelSerializerTest.scala index 61823917e4..72f0cf68c4 100644 --- a/modules/swagger-core/src/test/scala/ModelSerializerTest.scala +++ b/modules/swagger-core/src/test/scala/ModelSerializerTest.scala @@ -29,7 +29,7 @@ class ModelSerializerTest extends FlatSpec with Matchers { } it should "deserialize a model" in { - val json = """{"required":["intValue"],"properties":{"dateValue":{"type":"string","format":"date"},"longValue":{"type":"integer","format":"int64"},"dateTimeValue":{"type":"string","format":"date-time"},"intValue":{"type":"integer","format":"int32"}}}""" + val json = """{"required":["intValue"],"properties":{"dateValue":{"type":"string","format":"date"},"longValue":{"type":"integer","format":"int64"},"dateTimeValue":{"type":"string","format":"date-time"},"intValue":{"type":"integer","format":"int32"},"byteArrayValue":{"type":"string","format":"byte"}}}""" val p = m.readValue(json, classOf[Model]) m.writeValueAsString(p) should equal(json) From a18409eb69d9c69435bb2f0bbec7b2540b46616d Mon Sep 17 00:00:00 2001 From: b_sapir Date: Thu, 6 Aug 2015 17:50:59 +0300 Subject: [PATCH 6/7] Support binary input body params and binary response - use format "binary" in swagger file --- modules/swagger-core/src/test/scala/ModelSerializerTest.scala | 2 +- .../java/io/swagger/models/properties/ByteArrayProperty.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/swagger-core/src/test/scala/ModelSerializerTest.scala b/modules/swagger-core/src/test/scala/ModelSerializerTest.scala index 72f0cf68c4..aa98199ef6 100644 --- a/modules/swagger-core/src/test/scala/ModelSerializerTest.scala +++ b/modules/swagger-core/src/test/scala/ModelSerializerTest.scala @@ -29,7 +29,7 @@ class ModelSerializerTest extends FlatSpec with Matchers { } it should "deserialize a model" in { - val json = """{"required":["intValue"],"properties":{"dateValue":{"type":"string","format":"date"},"longValue":{"type":"integer","format":"int64"},"dateTimeValue":{"type":"string","format":"date-time"},"intValue":{"type":"integer","format":"int32"},"byteArrayValue":{"type":"string","format":"byte"}}}""" + val json = """{"required":["intValue"],"properties":{"dateValue":{"type":"string","format":"date"},"longValue":{"type":"integer","format":"int64"},"dateTimeValue":{"type":"string","format":"date-time"},"intValue":{"type":"integer","format":"int32"},"byteArrayValue":{"type":"string","format":"binary"}}}""" val p = m.readValue(json, classOf[Model]) m.writeValueAsString(p) should equal(json) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java index 3a86f66e00..5806081dc1 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteArrayProperty.java @@ -9,11 +9,11 @@ public class ByteArrayProperty extends AbstractProperty implements Property { public ByteArrayProperty() { super.type = "string"; - super.format = "byte"; + super.format = "binary"; } public static boolean isType(String type, String format) { - if("string".equals(type) && "byte".equals(format)) + if("string".equals(type) && "binary".equals(format)) return true; else return false; } From 62678fb9aabc6c249b701781141e8d0df0a7c1e0 Mon Sep 17 00:00:00 2001 From: b_sapir Date: Thu, 6 Aug 2015 19:04:16 +0300 Subject: [PATCH 7/7] Remove unnecessary comment --- .../main/java/io/swagger/models/properties/StringProperty.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java index 8b30d3f612..b3377e62fb 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/StringProperty.java @@ -50,7 +50,6 @@ public StringProperty(String format) { super.format = format; } - //TODO: implement additional formats public static boolean isType(String type, String format) { return TYPE.equals(type) && (format == null || Format.fromName(format) != null); }