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

ConvertTo-Json invalidates bodies due to missing depth option #5

Open
JaminVP opened this issue Jun 1, 2022 · 2 comments
Open

ConvertTo-Json invalidates bodies due to missing depth option #5

JaminVP opened this issue Jun 1, 2022 · 2 comments

Comments

@JaminVP
Copy link

JaminVP commented Jun 1, 2022

Describe the bug
Updating an NPrinting filter using "Invoke-NPRequest -method Put -Path "filters/$filterId" -Data $filter" goes wrong in the situation where the filter represents a body coming as a response from Invoke-NPRequest -method Get -Path 'filters' and the filter contains at least one field value. This is due to an issue of unwanted data loss with powershell's ConvertTo-Json cmdlet:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2

The default -Depth of this cmdlet has the value 2, and if the object you're converting is deeper than this, the result is data loss. In the case of a filter with at least one field value, the "values" attribute of the fields becomes an empty string instead of an array.

To Reproduce
Try to update a filter description which has at least one field value by first fetching the body using Invoke-NPRequest -method Get -Path 'filters', slightly modify the description, and then "Invoke-NPRequest -method Put -Path "filters/$filterId" -Data $filter" . You should run into a HTTP 400 Bad Request error.

Expected behavior
I'd expect a body retrieved by Invoke-NPRequest -method Get -Path 'filters' to by default be usable in a PUT-request, but the ConvertTo-Json cmdlet breaks the body. I advise changing every call to ConvertTo-Json in the Invoke-NPRequest implementation to include the -Depth option with a value of 100 or so.

Environment:

  • OS: Windows Server
  • PowerShell Version 5.1.14393.5066
  • Nprint Version May21
@Nillth
Copy link
Member

Nillth commented Jun 1, 2022

It should identify if you manually convert the psobject to a Json string. E.g. do the following

Invoke-NPRequest -method Put -Path "filters/$($filterId)" -Data $($filter|convertto-json -depth 5)

@JaminVP
Copy link
Author

JaminVP commented Jun 2, 2022

I agree that this works, and I thank you for your fast feedback! However, I consider this to be a workaround. I would still suggest changing it within the module itself. My argument for doing so: If the module itself supports converting from Powershell internal structures to the expected Json structure by the NPrinting API, it would be nice if it always works and not sometimes result in the content being destroyed.

I must admit that I really struggled a bit in figuring out this issue. I had to change my local version of the module to write out the body that's being put in the HTTP Request before I saw that the array was suddenly missing. The NPrinting API also doesn't help much by simply returning a HTTP 400 error without further details. I can imagine that should the usage of NPrinting increase, and by consequence also the usage of this module, other people will also be confronted by this issue and pull their hair out.

Just in general I think it should be the the goal that any item retrieved by the Get should be valid as input for the Put calls, as this makes Development in many cases much easier. I've seen sometimes that NPrinting itself isn't compliant with this logic (changing connections for example in my experience), but in this case it's the module that's breaking the content.

Regardless of this issue I wish to thank you for making this module publicly available, as it will really accelerate our automation goals concerning templatized report generation using Qlik Sense / NPrinting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants