Skip to content

Commit

Permalink
[v6] Fix LRO Generation issues (#634)
Browse files Browse the repository at this point in the history
* Fix LRO Generation issues

* Update generated smoke tests

* re-generate smoke
  • Loading branch information
joheredi authored May 5, 2020
1 parent f1f3614 commit e837a35
Show file tree
Hide file tree
Showing 45 changed files with 1,689 additions and 56 deletions.
25 changes: 22 additions & 3 deletions src/generators/clientFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
CodeBlockWriter
} from "ts-morph";
import { ClientDetails } from "../models/clientDetails";
import { addOperationSpecs, writeOperations } from "./operationGenerator";
import {
addOperationSpecs,
writeOperations,
writeGetOperationOptions
} from "./operationGenerator";
import {
getModelsName,
getMappersName,
Expand Down Expand Up @@ -59,6 +63,17 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
moduleSpecifier: "@azure/core-http"
});

const hasLRO = clientDetails.operationGroups.some(og =>
og.operations.some(o => o.isLRO)
);

if (hasInlineOperations && hasLRO) {
clientFile.addImportDeclaration({
namedImports: ["LROPoller", "shouldDeserializeLRO"],
moduleSpecifier: "./lro"
});
}

if (hasImportedOperations) {
clientFile.addImportDeclaration({
namespaceImport: "operations",
Expand Down Expand Up @@ -97,7 +112,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
});

writeConstructor(clientDetails, clientClass);
writeClientOperations(clientFile, clientClass, clientDetails);
writeClientOperations(clientFile, clientClass, clientDetails, hasLRO);

clientFile.addExportDeclaration({
leadingTrivia: (writer: CodeBlockWriter) =>
Expand Down Expand Up @@ -194,14 +209,18 @@ function getOperationGroupsDeclarationDetails(
function writeClientOperations(
file: SourceFile,
classDeclaration: ClassDeclaration,
clientDetails: ClientDetails
clientDetails: ClientDetails,
hasLRO: boolean
) {
const topLevelGroup = clientDetails.operationGroups.find(og => og.isTopLevel);
const hasMappers = !!clientDetails.mappers.length;
// Add top level operation groups as client properties
// TODO: Switch to named model imports in client File
const importedModels = new Set<string>();
if (!!topLevelGroup) {
if (hasLRO) {
writeGetOperationOptions(classDeclaration);
}
writeOperations(
topLevelGroup,
classDeclaration,
Expand Down
36 changes: 28 additions & 8 deletions src/generators/operationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function generateOperation(
);
}

function writeGetOperationOptions(operationGroupClass: ClassDeclaration) {
export function writeGetOperationOptions(
operationGroupClass: ClassDeclaration
) {
operationGroupClass.addMethod({
scope: Scope.Private,
name: "getOperationOptions<TOptions extends coreHttp.OperationOptions>",
Expand Down Expand Up @@ -317,6 +319,17 @@ function getOptionsParameter(
};
}

function getReturnType(
operation: OperationDetails,
importedModels: Set<string>
) {
const responseName = getResponseType(operation, importedModels);

return operation.isLRO
? `Promise<LROPoller<${responseName}>>`
: `Promise<${responseName}>`;
}

/**
* Adds an Operation group class to the generated file
*/
Expand Down Expand Up @@ -578,11 +591,12 @@ export function writeOperations(
operationGroupClass
);
const responseName = getResponseType(operation, importedModels);
const returnType = getReturnType(operation, importedModels);

const operationMethod = operationGroupClass.addMethod({
name: normalizeName(operation.name, NameType.Property),
parameters: baseMethodParameters,
returnType: `Promise<${responseName}>`,
returnType,
docs: [
generateOperationJSDoc(baseMethodParameters, operation.description)
],
Expand All @@ -603,7 +617,7 @@ export function writeOperations(
for (const overload of overloadParameterDeclarations) {
operationMethod.addOverload({
parameters: overload,
returnType: `Promise<${responseName}>`,
returnType,
docs: [generateOperationJSDoc(overload, operation.description)]
});
}
Expand Down Expand Up @@ -649,7 +663,8 @@ function writeNoOverloadsOperationBody(
responseName,
operationSpecName,
operationMethod,
finalStateVia
finalStateVia,
isInline
);
} else {
operationMethod.addStatements(
Expand All @@ -667,15 +682,18 @@ function writeLROOperationBody(
responseName: string,
operationSpecName: string,
methodDeclaration: MethodDeclaration,
finalStateVia?: string
finalStateVia?: string,
isInline?: boolean
) {
const finalStateStr = finalStateVia
? `finalStateVia: "${finalStateVia.toLowerCase()}"`
: "";

const operationBody = `
const args: coreHttp.OperationArguments = {${sendParams}};
const sendOperation = (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => this.client.sendOperationRequest(args, spec) as Promise<${responseName}>;
const sendOperation = (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => this${
isInline ? "" : ".client"
}.sendOperationRequest(args, spec) as Promise<${responseName}>;
const initialOperationResult = await sendOperation(args, ${operationSpecName});
return new LROPoller({
Expand Down Expand Up @@ -779,12 +797,14 @@ function writeMultiMediaTypeOperationBody(
: "";

statements += `
const sendOperation = (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => this.client.sendOperationRequest(args, spec) as Promise<${responseName}>;
const sendOperation = (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => this${
isInline ? "" : ".client"
}.sendOperationRequest(args, spec) as Promise<${responseName}>;
const initialOperationResult = await sendOperation(operationArguments, operationSpec);
return new LROPoller({
initialOperationArguments: operationArguments,
initialOperationSpec: operationSpec
initialOperationSpec: operationSpec,
initialOperationResult,
sendOperation,
${finalStateStr}
Expand Down
7 changes: 1 addition & 6 deletions src/lro/bodyPollingStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
LROStrategy,
BaseResult,
LROOperationStep,
LROResponseInfo
} from "./models";
import { LROStrategy, BaseResult, LROOperationStep } from "./models";
import { OperationSpec } from "@azure/core-http";
import { terminalStates } from "./constants";
import { SendOperationFn } from "./lroPoller";
Expand Down
2 changes: 1 addition & 1 deletion src/lro/lroPoller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Poller, PollOperationState } from "@azure/core-lro";
import { Poller } from "@azure/core-lro";
import {
OperationSpec,
OperationArguments,
Expand Down
1 change: 0 additions & 1 deletion src/lro/operation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseResult, LROOperationState, LROOperation } from "./models";
import { createBodyPollingStrategy } from "./bodyPollingStrategy";

/**
* Creates a copy of the operation from a given State
Expand Down
1 change: 0 additions & 1 deletion src/lro/passthroughStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LROStrategy, BaseResult, LROOperationStep } from "./models";
import { SendOperationFn } from "./lroPoller";

/**
* Creates a polling strategy based on BodyPolling which uses the provisioning state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import {
LROStrategy,
BaseResult,
LROOperationStep,
LROResponseInfo
} from "./models";
import { LROStrategy, BaseResult, LROOperationStep } from "./models";
import { OperationSpec } from "@azure/core-http";
import { terminalStates } from "./constants";
import { SendOperationFn } from "./lroPoller";
Expand Down
2 changes: 1 addition & 1 deletion test/integration/generated/lro/src/lro/lroPoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import { Poller, PollOperationState } from "@azure/core-lro";
import { Poller } from "@azure/core-lro";
import {
OperationSpec,
OperationArguments,
Expand Down
1 change: 0 additions & 1 deletion test/integration/generated/lro/src/lro/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { BaseResult, LROOperationState, LROOperation } from "./models";
import { createBodyPollingStrategy } from "./bodyPollingStrategy";

/**
* Creates a copy of the operation from a given State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { LROStrategy, BaseResult, LROOperationStep } from "./models";
import { SendOperationFn } from "./lroPoller";

/**
* Creates a polling strategy based on BodyPolling which uses the provisioning state
Expand Down
21 changes: 21 additions & 0 deletions test/integration/generated/mediaTypesV3Lro/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions test/integration/generated/mediaTypesV3Lro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Azure MediaTypesV3LROClient SDK for JavaScript

This package contains an isomorphic SDK for MediaTypesV3LROClient.

### Currently supported environments

- Node.js version 8.x.x or higher
- Browser JavaScript

### How to Install

```bash
npm install media-types-v3-lro-client
```

### How to use

#### Sample code

Refer the sample code in the [azure-sdk-for-js-samples](https://github.com/Azure/azure-sdk-for-js-samples) repository.

## Related projects

- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)


![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcdn%2Farm-cdn%2FREADME.png)
50 changes: 50 additions & 0 deletions test/integration/generated/mediaTypesV3Lro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "media-types-v3-lro-client",
"author": "Microsoft Corporation",
"description": "A generated SDK for MediaTypesV3LROClient.",
"version": "1.0.0-preview1",
"dependencies": {
"@azure/core-lro": "^1.0.1",
"@azure/core-http": "^1.1.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/media-types-v3-lro-client.js",
"module": "./esm/mediaTypesV3LROClient.js",
"types": "./esm/mediaTypesV3LROClient.d.ts",
"devDependencies": {
"typescript": "^3.1.1",
"rollup": "^0.66.2",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"uglify-js": "^3.4.9"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js",
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-sdk-for-js.git"
},
"bugs": { "url": "https://github.com/Azure/azure-sdk-for-js/issues" },
"files": [
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"esm/**/*.js",
"esm/**/*.js.map",
"esm/**/*.d.ts",
"esm/**/*.d.ts.map",
"src/**/*.ts",
"README.md",
"rollup.config.js",
"tsconfig.json"
],
"scripts": {
"build": "tsc && rollup -c rollup.config.js && npm run minify",
"minify": "uglifyjs -c -m --comments --source-map \"content='./dist/media-types-v3-lro-client.js.map'\" -o ./dist/media-types-v3-lro-client.min.js ./dist/media-types-v3-lro-client.js",
"prepack": "npm install && npm run build"
},
"sideEffects": false,
"autoPublish": true
}
39 changes: 39 additions & 0 deletions test/integration/generated/mediaTypesV3Lro/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import rollup from "rollup";
import nodeResolve from "rollup-plugin-node-resolve";
import sourcemaps from "rollup-plugin-sourcemaps";

/**
* @type {rollup.RollupFileOptions}
*/
const config = {
input: "./esm/mediaTypesV3LROClient.js",
external: ["@azure/core-http", "@azure/core-arm"],
output: {
file: "./dist/media-types-v3-lro-client.js",
format: "umd",
name: "MediaTypesV3LroClient",
sourcemap: true,
globals: {
"@azure/core-http": "coreHttp",
"@azure/core-arm": "coreArm"
},
banner: `/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/ `
},
plugins: [nodeResolve({ module: true }), sourcemaps()]
};

export default config;
Loading

0 comments on commit e837a35

Please sign in to comment.