diff --git a/modules/swagger-core/src/main/java/io/swagger/util/PrimitiveType.java b/modules/swagger-core/src/main/java/io/swagger/util/PrimitiveType.java index 8c49809878..237c592c27 100644 --- a/modules/swagger-core/src/main/java/io/swagger/util/PrimitiveType.java +++ b/modules/swagger-core/src/main/java/io/swagger/util/PrimitiveType.java @@ -1,19 +1,7 @@ package io.swagger.util; import com.fasterxml.jackson.databind.type.TypeFactory; -import io.swagger.models.properties.BaseIntegerProperty; -import io.swagger.models.properties.BooleanProperty; -import io.swagger.models.properties.DateProperty; -import io.swagger.models.properties.DateTimeProperty; -import io.swagger.models.properties.DecimalProperty; -import io.swagger.models.properties.DoubleProperty; -import io.swagger.models.properties.FloatProperty; -import io.swagger.models.properties.IntegerProperty; -import io.swagger.models.properties.LongProperty; -import io.swagger.models.properties.ObjectProperty; -import io.swagger.models.properties.Property; -import io.swagger.models.properties.StringProperty; -import io.swagger.models.properties.UUIDProperty; +import io.swagger.models.properties.*; import java.lang.reflect.Type; import java.util.Collections; @@ -49,8 +37,17 @@ public StringProperty createProperty() { */ BYTE(Byte.class, "byte") { @Override - public StringProperty createProperty() { - return new StringProperty(StringProperty.Format.BYTE); + public ByteArrayProperty createProperty() { + return new ByteArrayProperty(); + } + }, + /** + * Binary + */ + BINARY(Byte.class, "binary") { + @Override + public BinaryProperty createProperty() { + return new BinaryProperty(); } }, /** diff --git a/modules/swagger-core/src/test/java/io/swagger/ByteConverterTest.java b/modules/swagger-core/src/test/java/io/swagger/ByteConverterTest.java index b043684b1a..ef27bd4cf5 100644 --- a/modules/swagger-core/src/test/java/io/swagger/ByteConverterTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/ByteConverterTest.java @@ -4,10 +4,17 @@ import io.swagger.matchers.SerializationMatchers; import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BinaryProperty; +import io.swagger.models.properties.ByteArrayProperty; +import io.swagger.util.Json; import org.testng.annotations.Test; import java.util.Map; +import static org.testng.Assert.assertEquals; + public class ByteConverterTest { @Test @@ -30,6 +37,54 @@ public void testByte() { SerializationMatchers.assertEqualsToJson(models, json); } + @Test + public void testByteProperty() { + Model model = new ModelImpl() + .property("byteProperty", new ByteArrayProperty()); + + assertEquals(Json.pretty(model), "{\n" + + " \"properties\" : {\n" + + " \"byteProperty\" : {\n" + + " \"type\" : \"string\",\n" + + " \"format\" : \"byte\"\n" + + " }\n" + + " }\n" + + "}"); + } + + @Test + public void testDeserializeByteProperty() throws Exception { + String json = "{\n" + + " \"properties\" : {\n" + + " \"byteProperty\" : {\n" + + " \"type\" : \"string\",\n" + + " \"format\" : \"byte\"\n" + + " }\n" + + " }\n" + + "}"; + + Model model = Json.mapper().readValue(json, Model.class); + Json.prettyPrint(model); + } + + @Test + public void testByteArray() { + Model model = new ModelImpl() + .property("byteArray", new ArrayProperty(new BinaryProperty())); + + assertEquals(Json.pretty(model), "{\n" + + " \"properties\" : {\n" + + " \"byteArray\" : {\n" + + " \"type\" : \"array\",\n" + + " \"items\" : {\n" + + " \"type\" : \"string\",\n" + + " \"format\" : \"binary\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"); + } + class ByteConverterModel { public Byte[] myBytes = new Byte[0]; } diff --git a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteProperty.java b/modules/swagger-models/src/main/java/io/swagger/models/properties/BinaryProperty.java similarity index 74% rename from modules/swagger-models/src/main/java/io/swagger/models/properties/ByteProperty.java rename to modules/swagger-models/src/main/java/io/swagger/models/properties/BinaryProperty.java index c80d984b92..c1e20c5acd 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/properties/ByteProperty.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/properties/BinaryProperty.java @@ -2,73 +2,52 @@ import io.swagger.models.Xml; -import java.util.ArrayList; import java.util.List; -public class ByteProperty extends AbstractProperty implements Property { +public class BinaryProperty extends AbstractProperty implements Property { private static final String TYPE = "string"; - - private static final String FORMAT = "byte"; - protected List _enum; protected Integer minLength = null, maxLength = null; protected String pattern = null; protected String _default; - public ByteProperty() { - super.type = TYPE; - super.format = FORMAT; - } - - public ByteProperty _enum(String value) { - if (this._enum == null) { - this._enum = new ArrayList(); - } - if (!_enum.contains(value)) { - _enum.add(value); - } - return this; - } - - public ByteProperty _enum(List value) { - this._enum = value; - return this; + public BinaryProperty() { + super.type = "string"; + super.format = "binary"; } public static boolean isType(String type, String format) { - if (TYPE.equals(type) && FORMAT.equals(format)) { + if ("string".equals(type) && "binary".equals(format)) return true; - } else { - return false; - } + else return false; } - public ByteProperty xml(Xml xml) { + public BinaryProperty xml(Xml xml) { this.setXml(xml); return this; } - public ByteProperty minLength(Integer minLength) { + public BinaryProperty minLength(Integer minLength) { this.setMinLength(minLength); return this; } - public ByteProperty maxLength(Integer maxLength) { + public BinaryProperty maxLength(Integer maxLength) { this.setMaxLength(maxLength); return this; } - public ByteProperty pattern(String pattern) { + public BinaryProperty pattern(String pattern) { this.setPattern(pattern); return this; } - public ByteProperty _default(String _default) { + public BinaryProperty _default(String _default) { this._default = _default; return this; } - public ByteProperty vendorExtension(String key, Object obj) { + public BinaryProperty vendorExtension(String key, Object obj) { this.setVendorExtension(key, obj); return this; } @@ -130,10 +109,10 @@ public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } - if (!(obj instanceof ByteProperty)) { + if (!(obj instanceof BinaryProperty)) { return false; } - ByteProperty other = (ByteProperty) obj; + BinaryProperty other = (BinaryProperty) obj; if (_default == null) { if (other._default != null) { return false; 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 496a7ed05e..c6ba49a347 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 @@ -2,18 +2,16 @@ import io.swagger.models.Xml; -import java.util.*; - public class ByteArrayProperty extends AbstractProperty implements Property { public ByteArrayProperty() { super.type = "string"; - super.format = "binary"; + super.format = "byte"; } public static boolean isType(String type, String format) { - if ("string".equals(type) && "binary".equals(format)) + if ("string".equals(type) && "byte".equals(format)) return true; else return false; } 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 750f788d27..8fb677dcad 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 @@ -156,6 +156,17 @@ protected ByteArrayProperty create() { return new ByteArrayProperty(); } }, + BINARY(BinaryProperty.class) { + @Override + protected boolean isType(String type, String format) { + return BinaryProperty.isType(type, format); + } + + @Override + protected BinaryProperty create() { + return new BinaryProperty(); + } + }, 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 0dbb65ced1..920ab1b3ea 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 @@ -17,7 +17,6 @@ public class StringProperty extends AbstractProperty implements Property { protected String _default; public enum Format { - BYTE("byte"), URI("uri"), URL("url"); diff --git a/modules/swagger-models/src/test/java/io/swagger/StringPropertyTest.java b/modules/swagger-models/src/test/java/io/swagger/StringPropertyTest.java index 19bf1ae6e1..45be6930df 100644 --- a/modules/swagger-models/src/test/java/io/swagger/StringPropertyTest.java +++ b/modules/swagger-models/src/test/java/io/swagger/StringPropertyTest.java @@ -1,21 +1,17 @@ package io.swagger; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotEquals; - import io.swagger.models.properties.StringProperty; - import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; + public class StringPropertyTest { private static final String PROP_1 = "prop1"; private static final String PROP_2 = "prop2"; @Test public void testEquals() { - assertNotEquals(new StringProperty(StringProperty.Format.BYTE), new StringProperty(StringProperty.Format.URL)); - assertEquals(new StringProperty(StringProperty.Format.BYTE), new StringProperty(StringProperty.Format.BYTE)); - final StringProperty prop1 = new StringProperty(); prop1.setName(PROP_1); prop1.setRequired(true); diff --git a/modules/swagger-models/src/test/java/io/swagger/models/properties/PropertyBuilderTest.java b/modules/swagger-models/src/test/java/io/swagger/models/properties/PropertyBuilderTest.java index d32f3dc276..3c0ff8b699 100644 --- a/modules/swagger-models/src/test/java/io/swagger/models/properties/PropertyBuilderTest.java +++ b/modules/swagger-models/src/test/java/io/swagger/models/properties/PropertyBuilderTest.java @@ -1,17 +1,15 @@ package io.swagger.models.properties; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.Iterator; -import java.util.List; - +import io.swagger.models.properties.PropertyBuilder.PropertyId; +import io.swagger.models.properties.StringProperty.Format; import org.testng.Assert; - import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import io.swagger.models.properties.PropertyBuilder.PropertyId; -import io.swagger.models.properties.StringProperty.Format; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.Iterator; +import java.util.List; public class PropertyBuilderTest { @@ -57,8 +55,8 @@ public Object[][] createDataFromSpec() { {"number", "float", FloatProperty.class}, {"number", "double", DoubleProperty.class}, {"string", null, StringProperty.class}, - {"string", "byte", StringProperty.class}, // are this and the next one correct? - {"string", "binary", ByteArrayProperty.class}, + {"string", "byte", ByteArrayProperty.class}, + {"string", "binary", BinaryProperty.class}, {"boolean", null, BooleanProperty.class}, {"string", "date", DateProperty.class}, {"string", "date-time", DateTimeProperty.class},