Skip to content

Commit

Permalink
Merge pull request #144 from swagger-api/examplebuilder-improvements
Browse files Browse the repository at this point in the history
Fixed example-value overrides and handling of invalid example values
  • Loading branch information
fehguy authored Sep 20, 2016
2 parents 05a1ec1 + e66c31a commit db63342
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 31 deletions.
109 changes: 88 additions & 21 deletions src/main/java/io/swagger/inflector/examples/ExampleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
public class ExampleBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(ExampleBuilder.class);

public static final String SAMPLE_EMAIL_PROPERTY_VALUE = "[email protected]";
public static final String SAMPLE_UUID_PROPERTY_VALUE = "3fa85f64-5717-4562-b3fc-2c963f66afa6";
public static final String SAMPLE_STRING_PROPERTY_VALUE = "string";
public static final int SAMPLE_INT_PROPERTY_VALUE = 0;
public static final int SAMPLE_LONG_PROPERTY_VALUE = 0;
public static final int SAMPLE_BASE_INTEGER_PROPERTY_VALUE = 0;
public static final float SAMPLE_FLOAT_PROPERTY_VALUE = 1.1f;
public static final double SAMPLE_DOUBLE_PROPERTY_VALUE = 1.1f;
public static final boolean SAMPLE_BOOLEAN_PROPERTY_VALUE = true;
public static final String SAMPLE_DATE_PROPERTY_VALUE = "2015-07-20";
public static final String SAMPLE_DATETIME_PROPERTY_VALUE = "2015-07-20T15:49:04-07:00";
public static final double SAMPLE_DECIMAL_PROPERTY_VALUE = 1.5;

public static Example fromProperty(Property property, Map<String, Model> definitions) {
return fromProperty(property, definitions, new HashSet<String>());
}
Expand Down Expand Up @@ -92,66 +105,120 @@ public static Example fromProperty(Property property, Map<String, Model> definit
}
} else if (property instanceof EmailProperty) {
if (example != null) {
return new StringExample(example.toString());
output = new StringExample(example.toString());
}
else {
String defaultValue = ((EmailProperty)property).getDefault();
output = new StringExample( defaultValue == null ? SAMPLE_EMAIL_PROPERTY_VALUE : defaultValue );
}
output = new StringExample("[email protected]");
} else if (property instanceof UUIDProperty) {
if (example != null) {
output = new StringExample(example.toString());
}
output = new StringExample("3fa85f64-5717-4562-b3fc-2c963f66afa6");
else {
String defaultValue = ((UUIDProperty)property).getDefault();
output = new StringExample( defaultValue == null ? SAMPLE_UUID_PROPERTY_VALUE : defaultValue );
}
} else if (property instanceof StringProperty) {
if (example != null) {
output = new StringExample(example.toString());
} else {
output = new StringExample("string");
String defaultValue = ((StringProperty)property).getDefault();
output = new StringExample( defaultValue == null ? SAMPLE_STRING_PROPERTY_VALUE : defaultValue );
}
} else if (property instanceof IntegerProperty) {
if (example != null) {
output = new IntegerExample(Integer.parseInt(example.toString()));
} else {
output = new IntegerExample(0);
try {
output = new IntegerExample(Integer.parseInt(example.toString()));
}
catch( NumberFormatException e ){}
}

if( output == null ) {
Integer defaultValue = ((IntegerProperty) property).getDefault();
output = new IntegerExample( defaultValue == null ? SAMPLE_INT_PROPERTY_VALUE : defaultValue );
}
} else if (property instanceof LongProperty) {
if (example != null) {
output = new LongExample(Long.parseLong(example.toString()));
try {
output = new LongExample(Long.parseLong(example.toString()));
}
catch( NumberFormatException e ) {}
}

if( output == null ) {
Long defaultValue = ((LongProperty) property).getDefault();
output = new LongExample( defaultValue == null ? SAMPLE_LONG_PROPERTY_VALUE : defaultValue );
}
output = new LongExample(0);
} else if (property instanceof BaseIntegerProperty) {
if (example != null) {
output = new IntegerExample(Integer.parseInt(example.toString()));
try {
output = new IntegerExample(Integer.parseInt(example.toString()));
}
catch( NumberFormatException e ){}
}

if( output == null ) {
output = new IntegerExample(SAMPLE_BASE_INTEGER_PROPERTY_VALUE);
}
output = new IntegerExample(0);
} else if (property instanceof FloatProperty) {
if (example != null) {
output = new FloatExample(Float.parseFloat(example.toString()));
try {
output = new FloatExample(Float.parseFloat(example.toString()));
}
catch( NumberFormatException e ){}
}

if( output == null ) {
Float defaultValue = ((FloatProperty) property).getDefault();
output = new FloatExample( defaultValue == null ? SAMPLE_FLOAT_PROPERTY_VALUE : defaultValue );
}
output = new FloatExample(1.1f);
} else if (property instanceof DoubleProperty) {
if (example != null) {
output = new DoubleExample(Double.parseDouble(example.toString()));
try {
output = new DoubleExample(Double.parseDouble(example.toString()));
}
catch( NumberFormatException e ){}
}

if( output == null ){
Double defaultValue = ((DoubleProperty) property).getDefault();
output = new DoubleExample( defaultValue == null ? SAMPLE_DOUBLE_PROPERTY_VALUE : defaultValue );
}
output = new DoubleExample(1.23);
} else if (property instanceof BooleanProperty) {
if (example != null) {
output = new BooleanExample(Boolean.valueOf(Boolean.parseBoolean(example.toString())));
output = new BooleanExample(Boolean.valueOf(example.toString()));
}
else {
Boolean defaultValue = ((BooleanProperty)property).getDefault();
output = new BooleanExample( defaultValue == null ? SAMPLE_BOOLEAN_PROPERTY_VALUE : defaultValue.booleanValue());
}
output = new BooleanExample(Boolean.valueOf(true));
} else if (property instanceof DateProperty) {
if (example != null) {
output = new StringExample(example.toString());
}
output = new StringExample("2015-07-20");
else {

output = new StringExample(SAMPLE_DATE_PROPERTY_VALUE);
}
} else if (property instanceof DateTimeProperty) {
if (example != null) {
output = new StringExample(example.toString());
}
output = new StringExample("2015-07-20T15:49:04-07:00");
else {
output = new StringExample(SAMPLE_DATETIME_PROPERTY_VALUE);
}
} else if (property instanceof DecimalProperty) {
if (example != null) {
output = new DecimalExample(new BigDecimal(example.toString()));
try {
output = new DecimalExample(new BigDecimal(example.toString()));
}
catch( NumberFormatException e ){}
}

if( output == null ){
output = new DecimalExample(new BigDecimal(SAMPLE_DECIMAL_PROPERTY_VALUE));
}
output = new DecimalExample(new BigDecimal(1.5));
} else if (property instanceof ObjectProperty) {
if (example != null) {
try {
Expand Down
63 changes: 62 additions & 1 deletion src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,29 @@
import io.swagger.converter.ModelConverters;
import io.swagger.inflector.examples.ExampleBuilder;
import io.swagger.inflector.examples.XmlExampleSerializer;
import io.swagger.inflector.examples.models.AbstractExample;
import io.swagger.inflector.examples.models.ArrayExample;
import io.swagger.inflector.examples.models.DoubleExample;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.examples.models.ObjectExample;
import io.swagger.inflector.examples.models.StringExample;
import io.swagger.inflector.processors.JsonExampleDeserializer;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Xml;
import io.swagger.models.properties.*;
import io.swagger.models.properties.AbstractProperty;
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.DoubleProperty;
import io.swagger.models.properties.FloatProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.test.models.User;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
Expand Down Expand Up @@ -323,6 +336,54 @@ public void testIssue127() throws Exception {
"}");
}

@Test
public void testInvalidExample() throws Exception {
testInvalidExample( new IntegerProperty(), "asd",
ExampleBuilder.SAMPLE_INT_PROPERTY_VALUE, 123 );

testInvalidExample( new LongProperty(), "asd",
ExampleBuilder.SAMPLE_LONG_PROPERTY_VALUE, 123 );

testInvalidExample( new FloatProperty(), "asd",
ExampleBuilder.SAMPLE_FLOAT_PROPERTY_VALUE, 2.1f );

testInvalidExample( new DoubleProperty(), "asd",
ExampleBuilder.SAMPLE_DOUBLE_PROPERTY_VALUE, 3.1f );

// base types that don't implement setting a sample value
testInvalidExample( new DecimalProperty(), "asd",
ExampleBuilder.SAMPLE_DECIMAL_PROPERTY_VALUE );

testInvalidExample( new BaseIntegerProperty(), "asd",
ExampleBuilder.SAMPLE_BASE_INTEGER_PROPERTY_VALUE );
}

public void testInvalidExample(AbstractProperty property, String invalidValue, Object defaultValue ) throws Exception {
testInvalidExample( property, invalidValue, defaultValue, null );
}

public void testInvalidExample(AbstractProperty property, String invalidValue, Object defaultValue, Object sampleValue ) throws Exception {
property.setExample( invalidValue);

Model model = new ModelImpl().property("test", property );

Map<String, Model> definitions = new HashMap<>();
definitions.put("Test", model);

// validate that the internal default value is returned if an invalid value is set
ObjectExample rep = (ObjectExample) ExampleBuilder.fromProperty(new RefProperty("Test"), definitions);
AbstractExample example = (AbstractExample) rep.get( "test" );
assertEquals( String.valueOf(defaultValue), example.asString() );

// validate that a specified default value is returned if an invalid value is set
if( sampleValue != null ) {
property.setDefault(String.valueOf(sampleValue));
rep = (ObjectExample) ExampleBuilder.fromProperty(new RefProperty("Test"), definitions);
example = (AbstractExample) rep.get("test");
assertEquals(String.valueOf(sampleValue), example.asString());
}
}

private void assertEqualsIgnoreLineEnding(String actual, String expected) {
assertEquals(actual.replace("\n", System.getProperty("line.separator")), expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.swagger.test.integration.responses;

import io.swagger.inflector.examples.ExampleBuilder;
import io.swagger.test.client.ApiClient;
import io.swagger.test.client.ApiException;

Expand Down Expand Up @@ -49,7 +50,7 @@ public void verifyGetFloatResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveFloatResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "1.1");
assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_FLOAT_PROPERTY_VALUE));
}

/**
Expand All @@ -60,7 +61,7 @@ public void verifyGetDoubleResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveDoubleResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "1.23");
assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_DOUBLE_PROPERTY_VALUE));
}

/**
Expand All @@ -71,7 +72,11 @@ public void verifyGetUUIDResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveUUIDResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "\"3fa85f64-5717-4562-b3fc-2c963f66afa6\"");
assertEquals(str, quote(ExampleBuilder.SAMPLE_UUID_PROPERTY_VALUE));
}

private String quote(String string) {
return '"' + string + '"';
}

/**
Expand All @@ -82,7 +87,7 @@ public void verifyGetStringResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveStringResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "\"string\"");
assertEquals(str, quote(ExampleBuilder.SAMPLE_STRING_PROPERTY_VALUE));
}

/**
Expand All @@ -104,7 +109,7 @@ public void verifyGetDateResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveDateResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "\"2015-07-20\"");
assertEquals(str, quote(ExampleBuilder.SAMPLE_DATE_PROPERTY_VALUE));
}

/**
Expand All @@ -115,7 +120,7 @@ public void verifyGetDateTimeResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveDateTimeResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "\"2015-07-20T15:49:04-07:00\"");
assertEquals(str, quote(ExampleBuilder.SAMPLE_DATETIME_PROPERTY_VALUE));
}

/**
Expand All @@ -126,7 +131,7 @@ public void verifyGetBigDecimalResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveBigDecimalResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "1.5");
assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_DECIMAL_PROPERTY_VALUE ));
}

/**
Expand All @@ -137,7 +142,7 @@ public void verifyGetEmailResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/mockResponses/primitiveEmailResponse", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "\"[email protected]\"");
assertEquals(str, quote(ExampleBuilder.SAMPLE_EMAIL_PROPERTY_VALUE));
}

/**
Expand All @@ -159,7 +164,7 @@ public void verifyBaseIntegerResponse() throws Exception {
Map<String, String> queryParams = new HashMap<String, String>();

String str = client.invokeAPI("/issue-125", "GET", queryParams, null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "0");
assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_BASE_INTEGER_PROPERTY_VALUE));
}

/**
Expand Down

0 comments on commit db63342

Please sign in to comment.