DSC icon indicating copy to clipboard operation
DSC copied to clipboard

The `int()` function incorrectly handles non-integer numbers

Open michaeltlombardi opened this issue 1 year ago • 4 comments

Prerequisites

  • [X] Write a descriptive title.
  • [X] Make sure you are able to repro it on the latest version
  • [X] Search the existing issues.

Steps to reproduce

When using the int() function, I expect it to return the same value for a fractional number as the string representation of that fractional number. However, instead of returning 4 for 4.7, it returns 7.

Expected behavior

@'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Echo integer value
  type: Test/Echo
  properties:
    output:
      fromString: "[int('4.7')]"
      fromNumber: "[int(4.7)]"
'@ | dsc config get
results:
- name: Echo integer value
  type: Test/Echo
  result:
    actualState:
      output:
        fromString: 4
        fromNumber: 4
messages: []
hadErrors: false

Actual behavior

@'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Echo integer value
  type: Test/Echo
  properties:
    output:
      fromString: "[int('4.7')]"
      fromNumber: "[int(4.7)]"
'@ | dsc config get
results:
- name: Echo integer value
  type: Test/Echo
  result:
    actualState:
      output:
        fromString: 4
        fromNumber: 7
messages: []
hadErrors: false

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

Latest build from main

Visuals

No response

michaeltlombardi avatar Apr 09 '24 17:04 michaeltlombardi

The root cause of this issue is within the tree-sitter-dscexpression grammar, so this will apply to other functions as well. For numbers, the grammar only expects an int based on https://learn.microsoft.com/en-US/azure/azure-resource-manager/templates/data-types.

tgauth avatar Apr 10 '24 19:04 tgauth

Should we raise an error when a user inputs a number with a fractional part for the function then, instead of returning the fractional part as an integer? I'm open to the fix being either raising a specific error or correcting the grammar to parse the number and return the integer without the fractional part.

michaeltlombardi avatar Apr 10 '24 21:04 michaeltlombardi

@michaeltlombardi can you confirm what ARM templates do in this case?

SteveL-MSFT avatar May 16 '24 19:05 SteveL-MSFT

The documentation indicates that it converts a string or integer to an integer (supported parameter types are string and int - it doesn't say what happens if you give it a fractional number, but quick checking in a template indicates a validation error:

image

I would personally, as a user and developer, prefer to be able to coerce floats to ints with a function called int(), but if we need to remain compatible with ARM I'm fine with raising a validation error instead.

michaeltlombardi avatar May 21 '24 17:05 michaeltlombardi