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

Cannot invoke C2D command with JSON argument when using typescript #1008

Closed
rerkcp opened this issue Aug 5, 2021 · 1 comment
Closed

Cannot invoke C2D command with JSON argument when using typescript #1008

rerkcp opened this issue Aug 5, 2021 · 1 comment
Assignees
Labels
bug investigation-required IoTSDK tracks all IoT SDK issues across the board

Comments

@rerkcp
Copy link

rerkcp commented Aug 5, 2021

Context

Description of the issue

Problem 1: cannot hand JS object to invokeCommand directly

const digitalTwinClient = new iothub.DigitalTwinClient(new iothub.IoTHubTokenCredentials(connectionString));
const argument = { Name: 'test', Config: 'someconfig' };
return await digitalTwinClient.invokeCommand(deviceId, 'command', argument);

This fails to compile in typescript, because the typescript bindings only allow the argument to be a string.

Problem 2: SDK adds a layer of escaping when argument is already translated to string containing JSON.

const digitalTwinClient = new iothub.DigitalTwinClient(new iothub.IoTHubTokenCredentials(connectionString));
const argument = { Name: 'test', Config: 'someconfig' };
return await digitalTwinClient.invokeCommand(deviceId, 'command', JSON.stringify(argument));

This compiles, but the device receives not the expected argument

{ "Name": "test", "Config": "someconfig:" }

but

"{ \"Name\": \"test\", \"Config\": \"someconfig:\" }"

which obviously is not the format the device expects the argument to be.

Workaround (by falling back to JS behavior):

const digitalTwinClient = new iothub.DigitalTwinClient(new iothub.IoTHubTokenCredentials(connectionString));
const argument = { Name: 'test', Config: 'someconfig' };
const dummyClient : any = digitalTwinClient;
return await client.invokeCommand(deviceId, 'command', argument);

This works, but is unintuitive and eliminates type safety for the invokeCommand and the response.

If a better workaround is available please state, but maybe this should also be something added to the typescript examples.

@rerkcp rerkcp added the bug label Aug 5, 2021
@github-actions github-actions bot added the IoTSDK tracks all IoT SDK issues across the board label Aug 5, 2021
@anthonyvercolano
Copy link
Contributor

Fixed in release 09/09/21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug investigation-required IoTSDK tracks all IoT SDK issues across the board
Projects
None yet
Development

No branches or pull requests

3 participants