Core: Doctrine Decimal Type forced as string

Created on 24 Nov 2016  Â·  7Comments  Â·  Source: api-platform/core

With latest RC doctrine decimal type

/**
* @Groups({"cart"})
* @ORM\Column(type="decimal", name="public_price")
*/
private $publicPrice;

Became

"publicPrice": "string"

POST or PUT float for publicPrice generate an error :

{
"@context": "/contexts/Error",
"@type": "Error",
"hydra:title": "An error occurred",
"hydra:description": "The type of the \"publicPrice\" attribute must be \"string\", \"double\" given.",

Is it a new feature ?

It worked fine with v2.0.0-beta.3

Sorry if my request is dumb and it's just a configuration option.

invalid question

Most helpful comment

I find this behavior pretty intrusive from api-platform. We all know float type can lead to precision errors. But it should be the developper choice, not forced by the API. There are cases where floats are trully the best type for a variable, when not dealing with monetary values.

For example, I have a variable which can take any value from 0 to 2, with 2 decimal digits and which is clearly a decimal value. How am I supposed to make it work with api-platform ? map it as a string and cast it to float everytime I have to use it ? map it to integer and divide it by 100 every time I use it ?

All 7 comments

Yes it's expected if you want to keep numbers precision. #777

The behaviour is correct.

I find this behavior pretty intrusive from api-platform. We all know float type can lead to precision errors. But it should be the developper choice, not forced by the API. There are cases where floats are trully the best type for a variable, when not dealing with monetary values.

For example, I have a variable which can take any value from 0 to 2, with 2 decimal digits and which is clearly a decimal value. How am I supposed to make it work with api-platform ? map it as a string and cast it to float everytime I have to use it ? map it to integer and divide it by 100 every time I use it ?

@VaN-dev This is how the Doctrine decimal type is designed to be used. Symfony PropertyInfo is just reflecting that in the type information it provides, and API Platform enforces strict typing.

Yes, please cast it back and forth if that's what you really need. (I doubt so...)

Remember that you can always use the float type instead of decimal type, if you don't care about rounding errors.

So i just have to update my mapping from

fields:
    cutMarkup:
        type: decimal
        precision: 3
        scale: 2
        nullable: false

to

fields:
    cutMarkup:
        type: float
        precision: 3
        scale: 2
        nullable: false

and everything should work fine ?

The type float has no precision and scale options. It's a floating-point
number.

On 8 Dec 2016 17:58, "David" notifications@github.com wrote:

SO i just have to update my mapping from

fields:
cutMarkup:
type: decimal
precision: 3
scale: 2
nullable: false

to

fields:
cutMarkup:
type: float
precision: 3
scale: 2
nullable: false

and everything should work fine ?

—
You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
https://github.com/api-platform/core/issues/856#issuecomment-265700451,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAhf65X7zPiQYkVgh5fht8qh5QdMlbwUks5rF9S6gaJpZM4K7brw
.

Was this page helpful?
0 / 5 - 0 ratings