From da411488d78592ba390c5e8afef0ccc10aace0d6 Mon Sep 17 00:00:00 2001 From: Lucas Soares Date: Fri, 29 Jun 2018 16:33:57 -0300 Subject: [PATCH 1/2] checking empty string in examples for #2783 --- .../parameters/AbstractSerializableParameter.java | 2 +- .../AbstractSerializableParameterTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java index 87604521aa..fe5b25f2d3 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java @@ -404,7 +404,7 @@ public void setAllowEmptyValue(Boolean allowEmptyValue) { @JsonProperty("x-example") public Object getExample() { - if (example == null) { + if (example == null || example.isEmpty()) { return null; } try { diff --git a/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java b/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java index 50ed6fba61..ee211c4385 100644 --- a/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java +++ b/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java @@ -4,6 +4,7 @@ import io.swagger.models.properties.BaseIntegerProperty; import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.DecimalProperty; +import io.swagger.models.properties.LongProperty; import io.swagger.models.properties.Property; import io.swagger.models.properties.StringProperty; import org.testng.annotations.BeforeMethod; @@ -335,6 +336,19 @@ public void testGetExampleWithDecimalProperty() { "The example value must not change when when set an array property"); } + @Test + public void testGetExampleWithEmptyString() { + // given + instance.setProperty(new LongProperty()); + example = ""; + + // when + instance.setExample(example); + + // then + assertEquals(instance.getExample(), null, "The example must be null if the value is an empty string"); + } + @Test public void testGetExampleWithBooleanProperty() { // given From 3e7ba14c387864e89db0d23d3ae3f96dfae553c1 Mon Sep 17 00:00:00 2001 From: frantuma Date: Mon, 16 Jul 2018 16:31:20 +0200 Subject: [PATCH 2/2] refs #2783 - avoid NumberFormatException warning with empty String in param example --- .../parameters/AbstractSerializableParameter.java | 2 +- .../parameters/AbstractSerializableParameterTest.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java index fe5b25f2d3..26beba92e4 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java @@ -405,7 +405,7 @@ public void setAllowEmptyValue(Boolean allowEmptyValue) { @JsonProperty("x-example") public Object getExample() { if (example == null || example.isEmpty()) { - return null; + return example; } try { if (BaseIntegerProperty.TYPE.equals(type)) { diff --git a/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java b/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java index ee211c4385..de6376c0a2 100644 --- a/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java +++ b/modules/swagger-models/src/test/java/io/swagger/models/parameters/AbstractSerializableParameterTest.java @@ -340,13 +340,20 @@ public void testGetExampleWithDecimalProperty() { public void testGetExampleWithEmptyString() { // given instance.setProperty(new LongProperty()); - example = ""; + example = null; // when instance.setExample(example); // then - assertEquals(instance.getExample(), null, "The example must be null if the value is an empty string"); + assertEquals(instance.getExample(), null, "The example must be null if the value is null"); + + // given + instance.setProperty(new LongProperty()); + example = ""; + + // then + assertEquals(instance.getExample(), null, "The example must be empty string if the value is an empty string"); } @Test