From 04a03af58fa7b5246b780944e6c127280cbd96fd Mon Sep 17 00:00:00 2001 From: Ole Lensmar Date: Wed, 24 Aug 2016 11:54:10 +0200 Subject: [PATCH 1/4] Fixed example-value overrides and handling of invalid example values --- .../inflector/examples/ExampleBuilder.java | 81 ++++++++++++++----- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index b581beee..6a728b38 100644 --- a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java @@ -92,14 +92,18 @@ 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 { + output = new StringExample("apiteam@swagger.io"); } - 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 { + output = new StringExample("3fa85f64-5717-4562-b3fc-2c963f66afa6"); + } } else if (property instanceof StringProperty) { if (example != null) { output = new StringExample(example.toString()); @@ -108,50 +112,91 @@ public static Example fromProperty(Property property, Map definit } } else if (property instanceof IntegerProperty) { if (example != null) { - output = new IntegerExample(Integer.parseInt(example.toString())); - } else { + try { + output = new IntegerExample(Integer.parseInt(example.toString())); + } + catch( NumberFormatException e ){} + } + + if( output == null ) { output = new IntegerExample(0); } } 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 ) { + output = new LongExample(0); } - 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(0); } - 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 ) { + output = new FloatExample(1.1f); } - 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 ){ + output = new DoubleExample(1.23); } - 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(true)); + else { + 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("2015-07-20"); + } } 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("2015-07-20T15:49:04-07:00"); + } } 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(1.5)); } - output = new DecimalExample(new BigDecimal(1.5)); } else if (property instanceof ObjectProperty) { if (example != null) { try { From 137f8340e874d16f157a6d9ec6e881e0fb301789 Mon Sep 17 00:00:00 2001 From: Ole Lensmar Date: Thu, 25 Aug 2016 22:51:59 +0200 Subject: [PATCH 2/4] introduces constants for sample values, added tests and fixed some bugs... --- .../inflector/examples/ExampleBuilder.java | 48 ++++++++++++----- .../test/examples/ExampleBuilderTest.java | 52 +++++++++++++++++++ .../responses/PrimitiveResponseTestIT.java | 22 ++++---- 3 files changed, 100 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index 6a728b38..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()); } @@ -95,20 +108,23 @@ public static Example fromProperty(Property property, Map definit output = new StringExample(example.toString()); } else { - output = new StringExample("apiteam@swagger.io"); + String defaultValue = ((EmailProperty)property).getDefault(); + output = new StringExample( defaultValue == null ? SAMPLE_EMAIL_PROPERTY_VALUE : defaultValue ); } } else if (property instanceof UUIDProperty) { if (example != null) { output = new StringExample(example.toString()); } else { - output = new StringExample("3fa85f64-5717-4562-b3fc-2c963f66afa6"); + 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) { @@ -119,7 +135,8 @@ public static Example fromProperty(Property property, Map definit } if( output == null ) { - output = new IntegerExample(0); + Integer defaultValue = ((IntegerProperty) property).getDefault(); + output = new IntegerExample( defaultValue == null ? SAMPLE_INT_PROPERTY_VALUE : defaultValue ); } } else if (property instanceof LongProperty) { if (example != null) { @@ -130,7 +147,8 @@ public static Example fromProperty(Property property, Map definit } if( output == null ) { - output = new LongExample(0); + Long defaultValue = ((LongProperty) property).getDefault(); + output = new LongExample( defaultValue == null ? SAMPLE_LONG_PROPERTY_VALUE : defaultValue ); } } else if (property instanceof BaseIntegerProperty) { if (example != null) { @@ -141,7 +159,7 @@ public static Example fromProperty(Property property, Map definit } if( output == null ) { - output = new IntegerExample(0); + output = new IntegerExample(SAMPLE_BASE_INTEGER_PROPERTY_VALUE); } } else if (property instanceof FloatProperty) { if (example != null) { @@ -152,7 +170,8 @@ public static Example fromProperty(Property property, Map definit } if( output == null ) { - output = new FloatExample(1.1f); + Float defaultValue = ((FloatProperty) property).getDefault(); + output = new FloatExample( defaultValue == null ? SAMPLE_FLOAT_PROPERTY_VALUE : defaultValue ); } } else if (property instanceof DoubleProperty) { if (example != null) { @@ -163,28 +182,31 @@ public static Example fromProperty(Property property, Map definit } if( output == null ){ - output = new DoubleExample(1.23); + Double defaultValue = ((DoubleProperty) property).getDefault(); + output = new DoubleExample( defaultValue == null ? SAMPLE_DOUBLE_PROPERTY_VALUE : defaultValue ); } } 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 { - output = new BooleanExample(Boolean.valueOf(true)); + Boolean defaultValue = ((BooleanProperty)property).getDefault(); + output = new BooleanExample( defaultValue == null ? SAMPLE_BOOLEAN_PROPERTY_VALUE : defaultValue.booleanValue()); } } else if (property instanceof DateProperty) { if (example != null) { output = new StringExample(example.toString()); } else { - output = new StringExample("2015-07-20"); + + output = new StringExample(SAMPLE_DATE_PROPERTY_VALUE); } } else if (property instanceof DateTimeProperty) { if (example != null) { output = new StringExample(example.toString()); } else { - output = new StringExample("2015-07-20T15:49:04-07:00"); + output = new StringExample(SAMPLE_DATETIME_PROPERTY_VALUE); } } else if (property instanceof DecimalProperty) { if (example != null) { @@ -195,7 +217,7 @@ public static Example fromProperty(Property property, Map definit } if( output == null ){ - output = new DecimalExample(new BigDecimal(1.5)); + output = new DecimalExample(new BigDecimal(SAMPLE_DECIMAL_PROPERTY_VALUE)); } } else if (property instanceof ObjectProperty) { if (example != null) { diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index 5bebd173..5a60e4a1 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -20,9 +20,13 @@ 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.FloatExample; +import io.swagger.inflector.examples.models.IntegerExample; +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; @@ -323,6 +327,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(T1 property, String invalidValue, Object defaultValue ) throws Exception { + testInvalidExample( property, invalidValue, defaultValue, null ); + } + + public void testInvalidExample(T1 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..597539dc 100644 --- a/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java +++ b/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java @@ -16,12 +16,16 @@ package io.swagger.test.integration.responses; +import io.swagger.inflector.examples.ExampleBuilder; +import io.swagger.inflector.examples.models.Example; import io.swagger.test.client.ApiClient; import io.swagger.test.client.ApiException; import java.util.HashMap; import java.util.Map; +import static com.google.common.io.CharSource.wrap; +import static jdk.nashorn.internal.parser.JSONParser.quote; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -49,7 +53,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 +64,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 +75,7 @@ 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)); } /** @@ -82,7 +86,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 +108,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 +119,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 +130,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 +141,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 +163,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)); } /** From 806a115f76161ad50054f2843c5869425b6377c5 Mon Sep 17 00:00:00 2001 From: Ole Lensmar Date: Thu, 25 Aug 2016 22:54:11 +0200 Subject: [PATCH 3/4] removed unnecessary generics --- .../test/examples/ExampleBuilderTest.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index 5a60e4a1..70d37203 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -24,8 +24,6 @@ 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.FloatExample; -import io.swagger.inflector.examples.models.IntegerExample; import io.swagger.inflector.examples.models.ObjectExample; import io.swagger.inflector.examples.models.StringExample; import io.swagger.inflector.processors.JsonExampleDeserializer; @@ -33,7 +31,18 @@ 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; @@ -349,11 +358,11 @@ public void testInvalidExample() throws Exception { ExampleBuilder.SAMPLE_BASE_INTEGER_PROPERTY_VALUE ); } - public void testInvalidExample(T1 property, String invalidValue, Object defaultValue ) throws Exception { + public void testInvalidExample(AbstractProperty property, String invalidValue, Object defaultValue ) throws Exception { testInvalidExample( property, invalidValue, defaultValue, null ); } - public void testInvalidExample(T1 property, String invalidValue, Object defaultValue, Object sampleValue ) throws Exception { + public void testInvalidExample(AbstractProperty property, String invalidValue, Object defaultValue, Object sampleValue ) throws Exception { property.setExample( invalidValue); Model model = new ModelImpl().property("test", property ); From e66c31aab746c962beda7c5d9d46e22c606c6da2 Mon Sep 17 00:00:00 2001 From: Ole Lensmar Date: Thu, 25 Aug 2016 22:57:28 +0200 Subject: [PATCH 4/4] replaced external quote with internal implementation --- .../integration/responses/PrimitiveResponseTestIT.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 597539dc..6f8b7bb2 100644 --- a/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java +++ b/src/test/java/io/swagger/test/integration/responses/PrimitiveResponseTestIT.java @@ -17,15 +17,12 @@ package io.swagger.test.integration.responses; import io.swagger.inflector.examples.ExampleBuilder; -import io.swagger.inflector.examples.models.Example; import io.swagger.test.client.ApiClient; import io.swagger.test.client.ApiException; import java.util.HashMap; import java.util.Map; -import static com.google.common.io.CharSource.wrap; -import static jdk.nashorn.internal.parser.JSONParser.quote; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -78,6 +75,10 @@ public void verifyGetUUIDResponse() throws Exception { assertEquals(str, quote(ExampleBuilder.SAMPLE_UUID_PROPERTY_VALUE)); } + private String quote(String string) { + return '"' + string + '"'; + } + /** * verifies that the return value is automatically created */