diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index b581beee..98e46310 100644 --- a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java @@ -43,6 +43,19 @@ public class ExampleBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(ExampleBuilder.class); + public static final String SAMPLE_EMAIL_PROPERTY_VALUE = "apiteam@swagger.io"; + 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 definitions) { return fromProperty(property, definitions, new HashSet()); } @@ -92,66 +105,120 @@ public static Example fromProperty(Property property, Map 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("apiteam@swagger.io"); } 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 { diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index 5bebd173..70d37203 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -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; @@ -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 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); } diff --git a/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java b/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java index 91ec4807..6f8b7bb2 100644 --- a/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java +++ b/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java @@ -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; @@ -49,7 +50,7 @@ public void verifyGetFloatResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveFloatResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "1.1"); + assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_FLOAT_PROPERTY_VALUE)); } /** @@ -60,7 +61,7 @@ public void verifyGetDoubleResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveDoubleResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "1.23"); + assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_DOUBLE_PROPERTY_VALUE)); } /** @@ -71,7 +72,11 @@ public void verifyGetUUIDResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveUUIDResponse", "GET", queryParams, null, new HashMap(), 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 + '"'; } /** @@ -82,7 +87,7 @@ public void verifyGetStringResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveStringResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "\"string\""); + assertEquals(str, quote(ExampleBuilder.SAMPLE_STRING_PROPERTY_VALUE)); } /** @@ -104,7 +109,7 @@ public void verifyGetDateResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveDateResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "\"2015-07-20\""); + assertEquals(str, quote(ExampleBuilder.SAMPLE_DATE_PROPERTY_VALUE)); } /** @@ -115,7 +120,7 @@ public void verifyGetDateTimeResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveDateTimeResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "\"2015-07-20T15:49:04-07:00\""); + assertEquals(str, quote(ExampleBuilder.SAMPLE_DATETIME_PROPERTY_VALUE)); } /** @@ -126,7 +131,7 @@ public void verifyGetBigDecimalResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveBigDecimalResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "1.5"); + assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_DECIMAL_PROPERTY_VALUE )); } /** @@ -137,7 +142,7 @@ public void verifyGetEmailResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/mockResponses/primitiveEmailResponse", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "\"apiteam@swagger.io\""); + assertEquals(str, quote(ExampleBuilder.SAMPLE_EMAIL_PROPERTY_VALUE)); } /** @@ -159,7 +164,7 @@ public void verifyBaseIntegerResponse() throws Exception { Map queryParams = new HashMap(); String str = client.invokeAPI("/issue-125", "GET", queryParams, null, new HashMap(), null, "application/json", null, new String[0]); - assertEquals(str, "0"); + assertEquals(str, String.valueOf(ExampleBuilder.SAMPLE_BASE_INTEGER_PROPERTY_VALUE)); } /**