You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/07/27 13:33:55 UTC
[juneau] branch master updated: Fix bug in OpenApi serialization of
numbers.
This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 2d2bb73 Fix bug in OpenApi serialization of numbers.
2d2bb73 is described below
commit 2d2bb73a0d86133955c5442bb7050548d7f7390c
Author: JamesBognar <ja...@apache.org>
AuthorDate: Fri Jul 27 09:33:41 2018 -0400
Fix bug in OpenApi serialization of numbers.
---
.../juneau/httppart/OpenApiPartSerializerTest.java | 99 ++++++++++++++++++++++
.../org/apache/juneau/httppart/HttpPartSchema.java | 67 ++++++++++-----
.../juneau/httppart/OpenApiPartParserSession.java | 2 +-
.../httppart/OpenApiPartSerializerSession.java | 7 +-
4 files changed, 150 insertions(+), 25 deletions(-)
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
index f19a56f..b6e5f9e 100644
--- a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
@@ -13,6 +13,7 @@
package org.apache.juneau.httppart;
import static org.junit.Assert.*;
+import static java.lang.String.*;
import static org.apache.juneau.internal.StringUtils.*;
import java.util.*;
@@ -1083,6 +1084,104 @@ public class OpenApiPartSerializerTest {
}
//-----------------------------------------------------------------------------------------------------------------
+ // No-schema tests
+ //-----------------------------------------------------------------------------------------------------------------
+ @Test
+ public void i01a_noSchemaTests_Integer() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Integer v : AList.create(new Integer(1), Integer.MAX_VALUE, Integer.MIN_VALUE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+ @Test
+ public void i01b_noSchemaTests_IntegerArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("1,2147483647,-2147483648", s.serialize(null, new Integer[]{new Integer(1), Integer.MAX_VALUE, Integer.MIN_VALUE}));
+ }
+
+ @Test
+ public void i02a_noSchemaTests_Short() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Short v : AList.create(new Short((short)1), Short.MAX_VALUE, Short.MIN_VALUE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+
+ @Test
+ public void i02b_noSchemaTests_ShortArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("1,32767,-32768,null", s.serialize(null, new Short[]{new Short((short)1), Short.MAX_VALUE, Short.MIN_VALUE, null}));
+ }
+
+ @Test
+ public void i03a_noSchemaTests_Long() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Long v : AList.create(new Long(1), Long.MAX_VALUE, Long.MIN_VALUE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+
+ @Test
+ public void i03b_noSchemaTests_LongArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("1,9223372036854775807,-9223372036854775808,null", s.serialize(null, new Long[]{new Long(1), Long.MAX_VALUE, Long.MIN_VALUE, null}));
+ }
+
+ @Test
+ public void i04a_noSchemaTests_Float() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Float v : AList.create(new Float(1f), Float.MAX_VALUE, Float.MIN_VALUE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+
+ @Test
+ public void i04b_noSchemaTests_FloatArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("1.0,3.4028235E38,1.4E-45", s.serialize(null, new Float[]{new Float(1f), Float.MAX_VALUE, Float.MIN_VALUE}));
+ }
+
+ @Test
+ public void i05a_noSchemaTests_Double() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Double v : AList.create(new Double(1d), Double.MAX_VALUE, Double.MIN_VALUE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+
+ @Test
+ public void i05b_noSchemaTests_DoubleArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("1.0,1.7976931348623157E308,4.9E-324", s.serialize(null, new Double[]{new Double(1), Double.MAX_VALUE, Double.MIN_VALUE}));
+ }
+
+ @Test
+ public void i06a_noSchemaTests_Boolean() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (Boolean v : AList.create(Boolean.TRUE, Boolean.FALSE))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+
+ @Test
+ public void i06b_noSchemaTests_BooleanArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("true,false,null", s.serialize(null, new Boolean[]{Boolean.TRUE, Boolean.FALSE, null}));
+ }
+
+ @Test
+ public void i07_noSchemaTests_Null() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("null", s.serialize(null, null));
+ }
+
+ @Test
+ public void i08a_noSchemaTests_String() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ for (String v : AList.create("foo", "", null))
+ assertEquals(valueOf(v), s.serialize(null, v));
+ }
+ @Test
+ public void i08b_noSchemaTests_StringArray() throws Exception {
+ HttpPartSerializer s = OpenApiPartSerializer.DEFAULT;
+ assertEquals("foo,,null", s.serialize(null, new String[]{"foo", "", null}));
+ }
+
+ //-----------------------------------------------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------------------------------------------
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java
index ee20f0c..e127e4d 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java
@@ -764,26 +764,6 @@ public class HttpPartSchema {
}
/**
- * Returns the <code>default</code> field of this schema.
- *
- * @return The default value for this schema, or <jk>null</jk> if not specified.
- * @see HttpPartSchemaBuilder#_default(String)
- */
- public String getDefault() {
- return _default;
- }
-
- /**
- * Returns the <code>collectionFormat</code> field of this schema.
- *
- * @return The <code>collectionFormat</code> field of this schema, or <jk>null</jk> if not specified.
- * @see HttpPartSchemaBuilder#collectionFormat(String)
- */
- public CollectionFormat getCollectionFormat() {
- return collectionFormat;
- }
-
- /**
* Returns the <code>type</code> field of this schema.
*
* @param cm
@@ -810,16 +790,61 @@ public class HttpPartSchema {
}
/**
+ * Returns the <code>default</code> field of this schema.
+ *
+ * @return The default value for this schema, or <jk>null</jk> if not specified.
+ * @see HttpPartSchemaBuilder#_default(String)
+ */
+ public String getDefault() {
+ return _default;
+ }
+
+ /**
+ * Returns the <code>collectionFormat</code> field of this schema.
+ *
+ * @return The <code>collectionFormat</code> field of this schema, or <jk>null</jk> if not specified.
+ * @see HttpPartSchemaBuilder#collectionFormat(String)
+ */
+ public CollectionFormat getCollectionFormat() {
+ return collectionFormat;
+ }
+
+ /**
* Returns the <code>format</code> field of this schema.
*
- * @return The <code>format</code> field of this schema, or <jk>null</jk> if not specified.
* @see HttpPartSchemaBuilder#format(String)
+ * @return The <code>format</code> field of this schema, or <jk>null</jk> if not specified.
*/
public Format getFormat() {
return format;
}
/**
+ * Returns the <code>format</code> field of this schema.
+ *
+ * @param cm
+ * The class meta of the object.
+ * <br>Used to auto-detect the format if the format was not specified.
+ * @return The <code>format</code> field of this schema, or <jk>null</jk> if not specified.
+ * @see HttpPartSchemaBuilder#format(String)
+ */
+ public Format getFormat(ClassMeta<?> cm) {
+ if (format != Format.NO_FORMAT)
+ return format;
+ if (cm.isNumber()) {
+ if (cm.isDecimal()) {
+ if (cm.isDouble())
+ return Format.DOUBLE;
+ return Format.FLOAT;
+ }
+ if (cm.isLong())
+ return Format.INT64;
+ return Format.INT32;
+ }
+ return format;
+ }
+
+ /**
* Returns the <code>maximum</code> field of this schema.
*
* @return The schema for child items of the object represented by this schema, or <jk>null</jk> if not defined.
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParserSession.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParserSession.java
index 087a1e8..971e720 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParserSession.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParserSession.java
@@ -84,7 +84,7 @@ public class OpenApiPartParserSession extends UonPartParserSession {
in = schema.getDefault();
} else {
HttpPartSchema.Type t = schema.getType(type);
- HttpPartSchema.Format f = schema.getFormat();
+ HttpPartSchema.Format f = schema.getFormat(type);
if (t == STRING) {
if (type.isObject()) {
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializerSession.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializerSession.java
index 0b1c473..62e820b 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializerSession.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializerSession.java
@@ -125,7 +125,7 @@ public class OpenApiPartSerializerSession extends UonPartSerializerSession {
if (type == null)
type = object();
HttpPartSchema.Type t = schema.getType(type);
- HttpPartSchema.Format f = schema.getFormat();
+ HttpPartSchema.Format f = schema.getFormat(type);
HttpPartSchema.CollectionFormat cf = schema.getCollectionFormat();
String out = null;
@@ -286,8 +286,9 @@ public class OpenApiPartSerializerSession extends UonPartSerializerSession {
return null;
if (s == null)
s = DEFAULT_SCHEMA;
- HttpPartSchema.Type t = s.getType();
- HttpPartSchema.Format f = s.getFormat();
+ ClassMeta cm = getClassMetaForObject(o);
+ HttpPartSchema.Type t = s.getType(cm);
+ HttpPartSchema.Format f = s.getFormat(cm);
HttpPartSchema.CollectionFormat cf = s.getCollectionFormat();
if (t == STRING) {