Even though an attribute in a Content Type is defined as Integer, when it is queried it is returned as a Double. Therefore, I need to cast it to Double and then convert it to Integer. The resulting code, in Java, is like this entry.getField[java.lang.Double]("propertyReferenceID").toInt
.
Is there any reason why this happens?
Seems to work just fine for me…
If I get https://cdn.contentful.com/spaces/<my-space-id>/content_types/<my-content-type-id>?access_token=<my-delivery-token>
it returns this:
{
...
"fields": [
...
{
"id": "price",
"name": "Price",
"type": "Number",
"localized": false,
"required": false,
"disabled": false,
"omitted": false
},
...
{
"id": "quantity",
"name": "Quantity",
"type": "Integer",
"localized": false,
"required": false,
"disabled": false,
"omitted": false
},
...
]
}
One is a Number
and one is an Integer
. Then if I get an entry of that content type like this: https://cdn.contentful.com/spaces/<my-space-id>/entries/<entry-id>?access_token=<my-delivery-token>
I get this:
{
...
"fields": {
...
"price": 12.99,
...
"quantity": 123
}
}
Do you have your field types set correctly? Can you do the requests like I showed and share the responses?
Hello everyone,
thanks Charlie for the API answer, let me shine some light on the issue from the Java SDK
perspective:
Since the Integer
type in Contentful is defined as a Number type without decimals. Values from -2^53 to 2^53
this will not fit into a Java Integer (ranging from Integer.MIN to Integer.MAX). The closest fit we could find is Double
, and therefore chose it to be used everywhere.
I hope that clarifies the decision, and your solution would be the preferred one.
Greetings,
Mario
It makes sense.
Thank you for your reply.
@mario thanks for the reply! Contentful Number can be of two types: Integer and Decimal. In my opinion Integer which can get to 2^53 should be mapped to Long instead of Double which represents a decimal value.
I agree with @robert.rusu. Having it as a Double is confusable and makes calculating a checksum to compare with original data more difficult, especially when handling many different types.
And querying the model first adds an extra call to the Contenful API.
Having it as a Long would have been perfectly valid.