-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from swagger-api/examplebuilder-improvements
Fixed example-value overrides and handling of invalid example values
- Loading branch information
Showing
3 changed files
with
164 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>()); | ||
} | ||
|
@@ -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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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)); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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 + '"'; | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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 )); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|