Skip to content

Commit

Permalink
Merge pull request #1579 from swagger-api/issue-1578
Browse files Browse the repository at this point in the history
fix for #1578, swapped byte and binary properties
  • Loading branch information
fehguy committed Dec 23, 2015
2 parents 05475b8 + deda3fe commit 9f1414d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
},
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> _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<String>();
}
if (!_enum.contains(value)) {
_enum.add(value);
}
return this;
}

public ByteProperty _enum(List<String> 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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class StringProperty extends AbstractProperty implements Property {
protected String _default;

public enum Format {
BYTE("byte"),
URI("uri"),
URL("url");

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 9f1414d

Please sign in to comment.