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

[Typespec] [TS] Poller result type is "unknown" - typecasting is inconvenient #2127

Open
Tracked by #2126
HarshaNalluru opened this issue Nov 17, 2023 · 3 comments
Open
Tracked by #2126
Assignees
Labels
p0 priority 0 RLC

Comments

@HarshaNalluru
Copy link
Member

Having to typecast the result from poller is inconvenient.

const response = <DocumentClassifierBuildOperationDetailsOutput>(await (await poller).pollUntilDone()).body;
console.log(response.result?.classifierId);

Document Intelligence Typespec is here - https://github.com/Azure/azure-rest-api-specs/tree/main/specification/ai/DocumentIntelligence
JS PR for Document Intelligence preview - Azure/azure-sdk-for-js#27516

/cc @joheredi

Here's the full code snippet showing the complete picture

    let client = DocumentIntelligence(
      assertEnvironmentVariable("FORM_RECOGNIZER_ENDPOINT"),
      { key: assertEnvironmentVariable("FORM_RECOGNIZER_API_KEY") },
      recorder.configureClientOptions(options)
    );

    const initialResponse = await client.path("/documentClassifiers:build").post({
      body: {
        classifierId: recorder.variable(
          "customClassifierId",
          `customClassifier${getRandomNumber()}`
        ),
        description: "Custom classifier description",
        docTypes: {
          foo: {
            azureBlobSource: {
              containerUrl: containerSasUrl(),
            },
          },
          bar: {
            // Adding source kind fails with 400 Invalid Argument
            azureBlobSource: {
              containerUrl: containerSasUrl(),
            },
          },
        },
      },
    });

    if (isUnexpected(initialResponse)) {
      throw initialResponse.body.error;
    }
    const poller = getLongRunningPoller(client, initialResponse);
    const response = <DocumentClassifierBuildOperationDetailsOutput>(await (await poller).pollUntilDone()).body;
    console.log(response);
    //  {
    //    operationId: '31466834048_f3ee629e-73fb-48ab-993b-1d55d73ca460',
    //    kind: 'documentClassifierBuild',
    //    status: 'succeeded',
    //    createdDateTime: '2023-11-09T12:45:51Z',
    //    lastUpdatedDateTime: '2023-11-09T12:45:56Z',
    //    resourceLocation: 'https://endpoint/documentintelligence/documentClassifiers/customClassifier10978?api-version=2023-10-31-preview',
    //    percentCompleted: 100,
    //    result: {
    //      classifierId: 'customClassifier10978',
    //      createdDateTime: '2023-11-09T12:45:56Z',
    //      expirationDateTime: '2025-11-08T12:45:56Z',
    //      apiVersion: '2023-10-31-preview',
    //      docTypes: { foo: [Object], bar: [Object] },
    //      description: 'Custom classifier description'
    //    },
    //    apiVersion: '2023-10-31-preview'
    //  }
    assert.strictEqual(
      response.result?.classifierId,
      recorder.variable("customClassifierId"),
      "expected classifierId to match"
    );
@HarshaNalluru
Copy link
Member Author

Comment thread from typespec PR that maybe relevant to this issue https://github.com/Azure/azure-rest-api-specs-pr/pull/15526#discussion_r1384285898

@MaryGao
Copy link
Member

MaryGao commented Nov 20, 2023

I was wondering why the return type is unknown, we have a testing for poller and the final result should be relevant response types.

See the test case and my local screenshot

const poller = await getLongRunningPoller(client, initialResponse, {
intervalInMs: 0
});
const response = await poller.pollUntilDone();
if (isUnexpected(response)) {
assert.fail(`Unexpected status code ${response.status}`);
}
assert.equal(poller.getOperationState().status, "canceled");
assert.equal(response.status, "200");
assert.deepEqual(response.body.properties?.provisioningState, "Canceled");

image

@qiaozha qiaozha added P1 priority 1 RLC labels Nov 22, 2023
@MaryGao MaryGao added p0 priority 0 and removed P1 priority 1 labels Nov 15, 2024
@MaryGao
Copy link
Member

MaryGao commented Nov 15, 2024

@HarshaNalluru This is an issue during generation from TypeSpec because we usually would not define 200 explicitly and we do that in Swagger. I would fix this with the compensation logic to get final response from getLroMetadata.

Except this improvement we would meet another general issue that introduces by typescript's type system. I opened one to discuss this.

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

No branches or pull requests

3 participants