Skip to content

Commit

Permalink
Fixes swagger-api#1368: boolean default value has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lugaru1234 committed Aug 20, 2015
1 parent 420582d commit 13d2fb2
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.swagger.parameter;

import io.swagger.matchers.SerializationMatchers;
import io.swagger.models.parameters.QueryParameter;

import org.testng.annotations.Test;

public class ParameterSerializationTest {

@Test(description = "should serialize string value")
public void testStringValue() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("false");
param.setType("string");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"string\",\"default\":\"false\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize boolean value")
public void testBooleanValue() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("false");
param.setType("boolean");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"boolean\",\"default\":false}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize long value")
public void testLongValue() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("1234");
param.setType("integer");
param.setFormat("1nt64");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"integer\",\"default\":1234,\"format\":\"1nt64\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize double value")
public void testDoubleValue() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("12.34");
param.setType("number");
param.setFormat("double");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"number\",\"default\":12.34,\"format\":\"double\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize incorrect boolean value as string")
public void testIncorrectBoolean() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("test");
param.setType("boolean");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"boolean\",\"default\":\"test\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize incorrect long value as string")
public void testIncorrectLong() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("test");
param.setType("integer");
param.setFormat("1nt64");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"integer\",\"default\":\"test\",\"format\":\"1nt64\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}

@Test(description = "should serialize incorrect double value as string")
public void testIncorrectDouble() {
final QueryParameter param = new QueryParameter();
param.setDefaultValue("test");
param.setType("number");
param.setFormat("double");
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"number\",\"default\":\"test\",\"format\":\"double\"}";
SerializationMatchers.assertEqualsToJson(param, json);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package io.swagger.models.parameters;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.BaseIntegerProperty;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.DecimalProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;

import java.util.List;

@JsonPropertyOrder({"name", "in", "description", "required", "type", "items", "collectionFormat", "default", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum"})
public abstract class AbstractSerializableParameter<T extends AbstractSerializableParameter<T>> extends AbstractParameter implements SerializableParameter {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSerializableParameter.class);
protected String type;
protected String format;
protected String collectionFormat;
Expand All @@ -21,7 +28,7 @@ public abstract class AbstractSerializableParameter<T extends AbstractSerializab
protected Boolean exclusiveMinimum;
protected Double minimum;

@JsonProperty("default")
@JsonIgnore
protected String defaultValue;

public T property(Property property) {
Expand Down Expand Up @@ -135,6 +142,30 @@ public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

public Object getDefault() {
if (defaultValue == null) {
return null;
}
try {
if (BaseIntegerProperty.TYPE.equals(type)) {
return Long.valueOf(defaultValue);
} else if (DecimalProperty.TYPE.equals(type)) {
return Double.valueOf(defaultValue);
} else if (BooleanProperty.TYPE.equals(type)) {
if ("true".equalsIgnoreCase(defaultValue) || "false".equalsIgnoreCase(defaultValue)) {
return Boolean.valueOf(defaultValue);
}
}
} catch (NumberFormatException e) {
LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
}
return defaultValue;
}

public void setDefault(Object defaultValue) {
this.defaultValue = defaultValue == null ? null : defaultValue.toString();
}

public Boolean isExclusiveMaximum() {
return exclusiveMaximum;
}
Expand Down

0 comments on commit 13d2fb2

Please sign in to comment.