Skip to content

Commit

Permalink
Integrate bodyComplex (Azure#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
joheredi authored Dec 13, 2019
1 parent 6c621ca commit 599fe84
Show file tree
Hide file tree
Showing 28 changed files with 4,142 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "node ./dist/src/main.js",
"test": "npm-run-all unit-test integration-test",
"unit-test": "mocha -r ts-node/register './test/unit/**/*spec.ts'",
"integration-test": "npm-run-all start-test-server generate-bodystring integration-test:alone stop-test-server",
"integration-test": "npm-run-all start-test-server -p generate-bodystring generate-bodycomplex -s integration-test:alone stop-test-server",
"integration-test:alone": "mocha -r ts-node/register './test/integration/**/*spec.ts'",
"start-test-server": "ts-node test/utils/start-server.ts",
"stop-test-server": "stop-autorest-testserver",
Expand Down
16 changes: 9 additions & 7 deletions src/generators/operationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ function generateOperation(
function buildSpec(spec: OperationSpecDetails): string {
const responses = buildResponses(spec);
const requestBody = buildRequestBody(spec);
return `{
path: "${spec.path}",
httpMethod: "${spec.httpMethod}",
responses: {${responses.join(", ")}},
${requestBody}
serializer
const queryParams = spec.queryParameters
? `queryParameters: ${JSON.stringify(spec.queryParameters)},`
: "";
return `{ path: "${spec.path}", httpMethod: "${
spec.httpMethod
}", responses: {${responses.join(
", "
)}},${requestBody}${queryParams}serializer
}`;
}

Expand Down Expand Up @@ -231,7 +233,7 @@ function addOperations(
const type =
primitiveTypes.indexOf(typeName) > -1
? typeName
: `Models.${typeName}`;
: `Models.${normalizeName(typeName, NameType.Class)}`;
return {
name: param.name,
description: param.description,
Expand Down
5 changes: 4 additions & 1 deletion src/models/operationDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { ParameterLocation, HttpMethod } from "@azure-tools/codemodel";
import { KnownMediaType } from "@azure-tools/codegen";
import { Mapper } from "@azure/core-http";
import { Mapper, OperationQueryParameter } from "@azure/core-http";

/**
* Details of an operation request parameter, transformed from Request.
Expand All @@ -15,6 +15,7 @@ export interface OperationRequestParameterDetails {
modelType?: string; // Could be a primitive or actual model type
mapper: Mapper | string;
location: ParameterLocation;
serializedName?: string;
}

/**
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface OperationSpecDetails {
httpMethod: string;
responses: OperationSpecResponses;
requestBody?: OperationSpecRequest;
queryParameters?: OperationQueryParameter[];
}

/**
Expand All @@ -76,6 +78,7 @@ export type OperationSpecResponses = {
};

export type OperationSpecRequest = {
queryParameters: OperationQueryParameter[];
parameterPath: string;
mapper: Mapper | string;
};
28 changes: 25 additions & 3 deletions src/transforms/operationTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
HttpMethods,
Mapper,
MapperType,
CompositeMapper
CompositeMapper,
OperationQueryParameter
} from "@azure/core-http";
import {
Operation,
Expand Down Expand Up @@ -35,11 +36,14 @@ export function transformOperationSpec(
operationDetails: OperationDetails
): OperationSpecDetails {
// Extract protocol information
const requestSpec = extractSpecRequest(operationDetails);
const httpInfo = extractHttpDetails(operationDetails.request);
const queryParameters = requestSpec && requestSpec.queryParameters;
return {
...httpInfo,
responses: extractSpecResponses(operationDetails),
requestBody: extractSpecRequest(operationDetails)
requestBody: requestSpec,
...(queryParameters && queryParameters.length && { queryParameters })
};
}

Expand Down Expand Up @@ -70,11 +74,16 @@ export function extractSpecRequest(
p => p.location === ParameterLocation.Body
);

const queryParams = (operationDetails.request.parameters || []).filter(
p => p.location === ParameterLocation.Query
);

if (parameters.length < 1) {
return undefined;
}

return {
queryParameters: queryParams.map(extractQueryParam),
parameterPath: parameters.map(p => p.name)[0],
mapper: parameters[0].mapper
};
Expand Down Expand Up @@ -175,7 +184,8 @@ export function transformOperationRequestParameter(
location: parameter.protocol.http
? parameter.protocol.http.in
: ParameterLocation.Body,
mapper: getBodyMapperFromSchema(parameter.schema, true)
mapper: getBodyMapperFromSchema(parameter.schema, true),
serializedName: metadata.serializedName
};
}

Expand Down Expand Up @@ -287,3 +297,15 @@ function mergeResponsesAndExceptions(operation: Operation) {

return responses;
}

function extractQueryParam(
parameter: OperationRequestParameterDetails
): OperationQueryParameter {
return {
parameterPath: parameter.name,
mapper: {
...(parameter.mapper as Mapper),
serializedName: parameter.serializedName
}
};
}
2 changes: 1 addition & 1 deletion src/utils/nameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function toCasing(value: string, casing: CasingConvention): string {
}

function getNameParts(name: string) {
let parts = name.split(/[-_ ]+/);
let parts = name.split(/[-._ ]+/);

return parts.length > 0 ? parts : [name];
}
3 changes: 2 additions & 1 deletion src/utils/schemaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function getTypeForSchema(schema: Schema): PropertyTypeDetails {
typeName = name;
break;
default:
throw new Error(`Unsupported schema type: ${schema.type}`);
typeName = "any";
break;
}

return {
Expand Down
30 changes: 30 additions & 0 deletions test/integration/bodyComplex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { fail, deepEqual } from "assert";
import { BodyComplexClient } from "./generated/bodyCOmplex/src/bodyComplexClient";

describe("Integration tests for BodyComplex", () => {
let client: BodyComplexClient;

beforeEach(() => {
client = new BodyComplexClient();
});

describe("Acceptance tests", () => {});

describe("Valid", () => {
it("should get a valid complex type", async () => {
const result = await client.basic.getValid();
deepEqual(result, { id: 2, name: "abc", color: "YELLOW" });
});
it("should put a valid complex type", async () => {
try {
await client.basic.putValid({
id: 2,
name: "abc",
color: "Magenta"
});
} catch (error) {
fail(`Unexpected error thrown trying to execute putValid ${error}`);
}
}).timeout(50000);
});
});
21 changes: 21 additions & 0 deletions test/integration/generated/bodyComplex/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 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/bodyComplex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Azure BodyComplexClient SDK for JavaScript

This package contains an isomorphic SDK for BodyComplexClient.

### Currently supported environments

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

### How to Install

```bash
npm install bodyString
```

### 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/bodyComplex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "bodyString",
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest",
"version": "1.0.0-preview1",
"dependencies": {
"@azure/core-arm": "^1.0.0",
"@azure/core-http": "^1.0.0",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/bodyString.js",
"module": "./esm/bodyComplexClient.js",
"types": "./esm/bodyComplexClient.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/bodyString.js.map'\" -o ./dist/bodyString.min.js ./dist/bodyString.js",
"prepack": "npm install && npm run build"
},
"sideEffects": false,
"autoPublish": true
}
39 changes: 39 additions & 0 deletions test/integration/generated/bodyComplex/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/bodyComplexClient.js",
external: ["@azure/core-http", "@azure/core-arm"],
output: {
file: "./dist/bodyString.js",
format: "umd",
name: "BodyString",
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;
51 changes: 51 additions & 0 deletions test/integration/generated/bodyComplex/src/bodyComplexClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 * as Models from "./models";
import * as Mappers from "./models/mappers";
import * as operations from "./operations";
import { BodyComplexClientContext } from "./bodyComplexClientContext";

class BodyComplexClient extends BodyComplexClientContext {
basic: operations.Basic;
primitive: operations.Primitive;
array: operations.Array;
dictionary: operations.Dictionary;
inheritance: operations.Inheritance;
polymorphism: operations.Polymorphism;
polymorphicrecursive: operations.Polymorphicrecursive;
readonlyproperty: operations.Readonlyproperty;
flattencomplex: operations.Flattencomplex;

/**
* Initializes a new instance of the BodyComplexClient class.
* @param options The parameter options
*/
constructor(options?: any) {
super(options);
this.basic = new operations.Basic(this);
this.primitive = new operations.Primitive(this);
this.array = new operations.Array(this);
this.dictionary = new operations.Dictionary(this);
this.inheritance = new operations.Inheritance(this);
this.polymorphism = new operations.Polymorphism(this);
this.polymorphicrecursive = new operations.Polymorphicrecursive(this);
this.readonlyproperty = new operations.Readonlyproperty(this);
this.flattencomplex = new operations.Flattencomplex(this);
}
}

// Operation Specifications

export {
BodyComplexClient,
BodyComplexClientContext,
Models as BodyComplexModels,
Mappers as BodyComplexMappers
};
export * from "./operations";
Loading

0 comments on commit 599fe84

Please sign in to comment.