# Api returns int for text value?

**URL:** <https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366>\
**Category:** Support\
**Created:** [October 24, 2018, 8:08pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366 "2018-10-24T20:08:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusufiga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/yusufiga/32/134_2.png) [@yusufiga](https://discourse.getcockpit.com/u/yusufiga)\
**Post date:** [October 24, 2018, 8:08pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/1 "2018-10-24T20:08:16Z")

</div>

Hi,

I have defined a field called Postcode and set the type text. When I enter postcode 85221 and save the collection even though this field defined as text api converts it to int inside the json. Is this expacted behaviour? How can I force api to return this value as string.

Thanks

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 26, 2018, 2:11pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/2 "2018-10-26T14:11:22Z")

</div>

I looked into the database and it happens on save. So it’s not an output problem.

Just tested with commenting out these two lines:

> <https://github.com/agentejo/cockpit/blob/next/lib/MongoLite/Collection.php#L80>

  

> <https://github.com/agentejo/cockpit/blob/next/lib/MongoLite/Collection.php#L137>

Now int values are stored as strings and the api output is string, too. But I don’t understand why the checks are there. `//JSON_NUMERIC_CHECK - without destroying values with leading zeros` sounds important.

---

<div class="post-metadata">

**Author:** ![yusufiga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/yusufiga/32/134_2.png) [@yusufiga](https://discourse.getcockpit.com/u/yusufiga)\
**Post date:** [October 28, 2018, 7:47pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/3 "2018-10-28T19:47:37Z")

</div>

Thanks for pointing out the correct places. Yes it sounds important but also I believe it should not convert text field values to int values? Maybe the field type check would prevent this misbehavior?

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 30, 2018, 8:46am UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/4 "2018-10-30T08:46:48Z")

</div>

No, it really shoudn’t. Some tests in a simple text field:

```auto
42e0 --> 42
42e4 --> 420000
 42 --> 42
  42 --> 42
.42 --> 0.42
4.2 --> 4.2
-42 --> -42
+42 --> 42
4 2 --> "4 2"
42- --> "42-"
+-42 --> "+-42"

```

> [@yusufiga](#):
>
> Maybe the field type check would prevent this misbehavior?

There is no number field/component to check against, but it looks like there was one in the past - [https://github.com/agentejo/cockpit/blob/next/modules/Collections/bootstrap.php#L311](https://github.com/agentejo/cockpit/blob/next/modules/Collections/bootstrap.php#L311)

@artur Is there a reason for this type conversion?

---

<div class="post-metadata">

**Author:** ![yusufiga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/yusufiga/32/134_2.png) [@yusufiga](https://discourse.getcockpit.com/u/yusufiga)\
**Post date:** [October 30, 2018, 3:23pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/5 "2018-10-30T15:23:53Z")

</div>

I created an issue on Github and pointed this discussion. [https://github.com/agentejo/cockpit/issues/907](https://github.com/agentejo/cockpit/issues/907)

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 25, 2018, 7:17pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/6 "2018-11-25T19:17:33Z")

</div>

Fixed in [https://github.com/agentejo/cockpit/commit/4a9057541de1b557bdc92e9ea1497a3609732d16](https://github.com/agentejo/cockpit/commit/4a9057541de1b557bdc92e9ea1497a3609732d16)

@artur Can you explain, why the check was there? It looked important and it looked like you wrote the MongoLite class yourself in the past. I just want to understand the reason, if something else could break in the future or if other related issues come up in the future.

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 25, 2018, 7:29pm UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/7 "2018-11-25T19:29:39Z")

</div>

And I have to correct myself:

> [@raffaelj](#):
>
> There is no number field/component to check against, […]

> [@Number field type](https://discourse.getcockpit.com/t/number-field-type/393/2):
>
> use the `text` type and add this to the field options
> 
> ```auto
> {
> "type": "number"
> }
> 
> ```

See: [Number field type](https://discourse.getcockpit.com/t/number-field-type/393)

Using this variant and adding “42e3” as an entry leads to “42e3” in the database and the input field in the backend converts its appearance to “42000”.

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [November 27, 2018, 9:55am UTC](https://discourse.getcockpit.com/t/api-returns-int-for-text-value/366/8 "2018-11-27T09:55:44Z")

</div>

shouldn’t break anything, I didn’t know back then that the number casting would have such side effects
