fix: policy expr type errors due to X.0 floating points in JSON being treated as ints #451
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #449 .
Pernicious bug in policy expression code. JSON has only "Number" type, doesn't distinguish between float and int. When a float that ends in
.0
gets serialized then injected into a policy expression, it is treated as an integer. Our policy expression evaluation logic for arithmetic/comparison requires operands beint + int
orfloat + float
, so when this bug occurs we get a type error because we doint + float
.This fix updates
binary_primitive_op
to detect when the operands areint+float
orfloat+int
and implicitly upcasts theint
to afloat
. It also removes other type checking frombinary_primitive_op
, as it was bad design. For example, although we updatedfn add
to allowDateTime + Span
, the type checking inbinary_primitive_op
did not allow this to occur and we had not tested to encounter this bug before.Added tests to ensure int+float arithmetic/comparisons return floats, and ensure
(add datetime span)
does not error.In a more correct AST design, we would do a first-pass eval of the AST and insert "cast" nodes around integer primitives to make them floats. This fix approach works but doesn't afford per-op control, highlighting the need for #387 .