Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The int() function incorrectly handles non-integer numbers #390

Closed
3 tasks done
michaeltlombardi opened this issue Apr 9, 2024 · 4 comments · Fixed by #438
Closed
3 tasks done

The int() function incorrectly handles non-integer numbers #390

michaeltlombardi opened this issue Apr 9, 2024 · 4 comments · Fixed by #438
Labels
Issue-Bug Something isn't working Need-Review

Comments

@michaeltlombardi
Copy link
Collaborator

michaeltlombardi commented Apr 9, 2024

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • 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 michaeltlombardi added Issue-Bug Something isn't working Need-Review labels Apr 9, 2024
@tgauth
Copy link
Collaborator

tgauth commented Apr 10, 2024

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.

@michaeltlombardi
Copy link
Collaborator Author

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.

@SteveL-MSFT
Copy link
Member

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

@michaeltlombardi
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Something isn't working Need-Review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants