Skip to content

Commit

Permalink
Merge pull request Azure#582 from chradek/multi-content-type
Browse files Browse the repository at this point in the history
[v6] Adds support for operations with multiple content types
  • Loading branch information
chradek authored Mar 18, 2020
2 parents 8ca5458 + f5ab6b3 commit 685e2e7
Show file tree
Hide file tree
Showing 35 changed files with 1,436 additions and 159 deletions.
112 changes: 46 additions & 66 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"unit-test": "mocha -r ts-node/register './test/unit/**/*spec.ts'",
"integration-test": "start-server-and-test start-test-server:v1 http://localhost:3000 generate-and-test",
"integration-test:new": "npm-run-all start-test-server generate-and-test integration-test:alone stop-test-server",
"generate-and-test": "npm-run-all -s build -p generate-bodystring generate-bodycomplex generate-url generate-customurl generate-xmlservice generate-header generate-paging -s integration-test:alone",
"generate-and-test": "npm-run-all -s build -p generate-bodystring generate-bodycomplex generate-url generate-customurl generate-xmlservice generate-header generate-paging generate-mediatypes -s integration-test:alone",
"integration-test:alone": "mocha -r ts-node/register './test/integration/**/*spec.ts'",
"start-test-server": "ts-node test/utils/start-server.ts",
"start-test-server:v1": "start-autorest-express node",
Expand All @@ -21,7 +21,9 @@
"generate-customurl": "autorest --add-credentials=false --typescript --output-folder=./test/integration/generated/customUrl --use=. --title=CustomUrlClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/custom-baseUrl.json --package-name=custom-url --package-version=1.0.0-preview1",
"generate-header": "autorest --add-credentials=false --typescript --output-folder=./test/integration/generated/header --use=. --title=HeaderClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/header.json --package-name=header --package-version=1.0.0-preview1",
"generate-xmlservice": "autorest --add-credentials=false --typescript --output-folder=./test/integration/generated/xmlservice --use=. --title=XmlServiceClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/xml-service.json --package-name=xmlservice --package-version=1.0.0-preview1",
"generate-paging": "autorest --typescript --add-credentials=false --output-folder=./test/integration/generated/paging --use=. --title=PagingClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/paging.json --package-name=pagingservice --package-version=1.0.0-preview1"
"generate-paging": "autorest --typescript --add-credentials=false --output-folder=./test/integration/generated/paging --use=. --title=PagingClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/paging.json --package-name=pagingservice --package-version=1.0.0-preview1",
"generate-report": "autorest --typescript --add-credentials=false --output-folder=./test/integration/generated/report --use=. --title=ReportClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/report.json --package-name=zzzReport --package-version=1.0.0-preview1",
"generate-mediatypes": "autorest --typescript --add-credentials=false --output-folder=./test/integration/generated/mediaTypes --use=. --title=MediaTypesClient --input-file=node_modules/@microsoft.azure/autorest.testserver/swagger/media_types.json --package-name=media-types-service --package-version=1.0.0-preview1"
},
"files": [
"dist/**",
Expand All @@ -31,9 +33,9 @@
"dependencies": {
"autorest": "^3.0.6187",
"@azure-tools/async-io": "3.0.203",
"@azure-tools/autorest-extension-base": "3.1.206",
"@azure-tools/codegen": "2.1.218",
"@azure-tools/codemodel": "3.3.295",
"@azure-tools/autorest-extension-base": "3.1.237",
"@azure-tools/codegen": "2.4.259",
"@azure-tools/codemodel": "3.4.302",
"@azure-tools/linq": "3.1.206",
"@azure-tools/openapi": "3.0.209",
"@azure/core-http": "^1.0.0",
Expand All @@ -48,7 +50,7 @@
"devDependencies": {
"@autorest/test-server": "^3.0.26",
"@azure/abort-controller": "^1.0.1",
"@microsoft.azure/autorest.testserver": "^2.10.9",
"@microsoft.azure/autorest.testserver": "^2.10.10",
"@types/chai": "^4.2.8",
"@types/express": "^4.17.2",
"@types/js-yaml": "3.12.1",
Expand Down
71 changes: 55 additions & 16 deletions src/generators/modelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "../models/operationDetails";
import { ParameterDetails } from "../models/parameterDetails";
import { ImplementationLocation } from "@azure-tools/codemodel";
import { KnownMediaType } from "@azure-tools/codegen";

export function generateModels(clientDetails: ClientDetails, project: Project) {
const modelsIndexFile = project.createSourceFile(
Expand Down Expand Up @@ -63,7 +64,7 @@ const writeClientModels = (
"",
clientOptionalParams,
modelsIndexFile,
"coreHttp.ServiceClientOptions"
{ baseClass: "coreHttp.ServiceClientOptions" }
);
};

Expand All @@ -89,7 +90,8 @@ const writeOperationModels = (
operationGroupName,
operationName,
optionalParams,
modelsIndexFile
modelsIndexFile,
{ mediaTypes: operation.mediaTypes }
);
writeResponseTypes(operation, modelsIndexFile);
});
Expand Down Expand Up @@ -358,30 +360,67 @@ function writeUniontypes({ objects }: ClientDetails, modelsFile: SourceFile) {
});
}

interface WriteOptionalParametersOptions {
baseClass?: string;
mediaTypes?: Set<KnownMediaType>;
}

function writeOptionalParameters(
operationGroupName: string,
operationName: string,
optionalParams: ParameterDetails[],
modelsIndexFile: SourceFile,
baseClass?: string
{ baseClass, mediaTypes }: WriteOptionalParametersOptions
) {
if (!optionalParams || !optionalParams.length) {
return;
}

modelsIndexFile.addInterface({
name: `${operationGroupName}${operationName}OptionalParams`,
docs: ["Optional parameters."],
isExported: true,
extends: [baseClass || "coreHttp.OperationOptions"],
properties: optionalParams.map<PropertySignatureStructure>(p => ({
name: p.name,
hasQuestionToken: true,
type: p.typeDetails.typeName,
docs: p.description ? [p.description] : undefined,
kind: StructureKind.PropertySignature
}))
});
const mediaTypesCount = mediaTypes?.size ?? 0;
if (mediaTypesCount > 1) {
// Create an optional params for each media type.
const interfaceNames: string[] = [];
for (const mediaType of mediaTypes!.values()) {
const name = `${operationGroupName}${operationName}$${mediaType}OptionalParams`;
interfaceNames.push(name);
modelsIndexFile.addInterface({
name: name,
docs: ["Optional parameters."],
isExported: true,
extends: [baseClass || "coreHttp.OperationOptions"],
properties: optionalParams
.filter(p => p.targetMediaType === mediaType)
.map<PropertySignatureStructure>(p => ({
name: p.name,
hasQuestionToken: true,
type: p.typeDetails.typeName,
docs: p.description ? [p.description] : undefined,
kind: StructureKind.PropertySignature
}))
});
}

modelsIndexFile.addTypeAlias({
name: `${operationGroupName}${operationName}OptionalParams`,
docs: ["Optional parameters."],
isExported: true,
type: interfaceNames.join(" | ")
});
} else {
modelsIndexFile.addInterface({
name: `${operationGroupName}${operationName}OptionalParams`,
docs: ["Optional parameters."],
isExported: true,
extends: [baseClass || "coreHttp.OperationOptions"],
properties: optionalParams.map<PropertySignatureStructure>(p => ({
name: p.name,
hasQuestionToken: true,
type: p.typeDetails.typeName,
docs: p.description ? [p.description] : undefined,
kind: StructureKind.PropertySignature
}))
});
}
}

/**
Expand Down
Loading

0 comments on commit 685e2e7

Please sign in to comment.