Skip to content

Commit

Permalink
Merge pull request Azure#647 from chradek/operation-spec-content-type
Browse files Browse the repository at this point in the history
 [v6] add mediaType to OperationSpecs and make param ordering deterministic
  • Loading branch information
chradek authored May 20, 2020
2 parents a4df799 + f7f1e27 commit f26b833
Show file tree
Hide file tree
Showing 345 changed files with 2,663 additions and 1,626 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@azure-tools/codemodel": "4.13.317",
"@azure-tools/linq": "3.1.206",
"@azure-tools/openapi": "3.0.209",
"@azure/core-http": "^1.1.1",
"@azure/core-http": "1.1.3-dev.20200520.1",
"@azure/core-lro": "^1.0.1",
"@azure/core-paging": "^1.0.0",
"@azure/logger": "^1.0.0",
Expand Down
69 changes: 65 additions & 4 deletions src/generators/operationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
ChoiceSchema,
SealedChoiceSchema,
ConstantSchema,
Parameter
Parameter,
ImplementationLocation
} from "@azure-tools/codemodel";
import { getLanguageMetadata } from "../utils/languageHelpers";
import { shouldImportParameters } from "./utils/importUtils";
Expand Down Expand Up @@ -146,16 +147,25 @@ function buildSpec(spec: OperationSpecDetails): string {
const urlParams = buildParameters(spec, "urlParameters");
const headerParams = buildParameters(spec, "headerParameters");
const contentType = buildContentType(spec);
const mediaType = buildMediaType(spec);

const isXML = spec.isXML ? "isXML: true," : "";

return `{ path: "${spec.path}", httpMethod: "${
spec.httpMethod
}", responses: {${responses.join(
", "
)}},${requestBody}${queryParams}${urlParams}${headerParams}${isXML}${contentType}serializer
)}},${requestBody}${queryParams}${urlParams}${headerParams}${isXML}${contentType}${mediaType}serializer
}`;
}

function buildMediaType({ requestBody }: OperationSpecDetails) {
if (requestBody?.targetMediaType) {
return `mediaType: '${requestBody.targetMediaType}',`;
}
return "";
}

function buildContentType({ requestBody, isXML }: OperationSpecDetails) {
return requestBody && isXML
? "contentType: 'application/xml; charset=utf-8',"
Expand Down Expand Up @@ -415,6 +425,50 @@ type ParameterWithDescription = OptionalKind<
}
>;

/**
* Sorts the list of operation parameters to match the order described by the CodeModel.
* @param operation Details about an operation.
* @param request Details about an operation overload.
* @param parameterDeclarations List of required parameter declarations for the provided operation overload.
*/
function sortOperationParameters(
operation: OperationDetails,
request: OperationRequestDetails,
parameterDeclarations: ParameterWithDescription[]
): ParameterWithDescription[] {
// Get a sorted list of parameter names for this operation/request.
// Note that this may inlcude parameters that aren't displayed, e.g. constant types.
const expectedParameterOrdering = [
...operation.parameters,
...(request.parameters ?? [])
]
// Only parameters that are implemented on the method should be considered.
.filter(param => param.implementation === ImplementationLocation.Method)
.map(param => getLanguageMetadata(param.language).name);

const orderedParameterDeclarations: typeof parameterDeclarations = [];
for (const parameterName of expectedParameterOrdering) {
const index = parameterDeclarations.findIndex(
p => p.name === parameterName
);
if (index === -1) {
// No matching parameter found.
// Common cases where this occurs is if a parameter
// is optional, or a constant.
continue;
}

orderedParameterDeclarations.push(
...parameterDeclarations.splice(index, 1)
);
}

// push any remaining parameters into the ordered parameter list
orderedParameterDeclarations.push(...parameterDeclarations);

return orderedParameterDeclarations;
}

/**
* Gets a list of parameter declarations for each overload the operation supports,
* and the list of parameter declarations for the base operation.
Expand Down Expand Up @@ -489,6 +543,13 @@ function getOperationParameterSignatures(
parameterDeclarations
);

// Sort the parameter declarations to match the signature the CodeModel suggests.
const orderedParameterDeclarations = sortOperationParameters(
operation,
request,
parameterDeclarations
);

// add optional parameter
const optionalParameter = getOptionsParameter(
operation,
Expand All @@ -498,9 +559,9 @@ function getOperationParameterSignatures(
mediaType: hasMultipleOverloads ? requestMediaType : undefined
}
);
parameterDeclarations.push(optionalParameter);
orderedParameterDeclarations.push(optionalParameter);

overloadParameterDeclarations.push(parameterDeclarations);
overloadParameterDeclarations.push(orderedParameterDeclarations);
}

// Create the parameter declarations for the base method signature.
Expand Down
2 changes: 1 addition & 1 deletion src/generators/static/packageFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function generatePackageJson(
version: packageDetails.version,
dependencies: {
...(hasLRO && { "@azure/core-lro": "^1.0.1" }),
"@azure/core-http": "^1.1.1",
"@azure/core-http": "1.1.3-dev.20200520.1",
tslib: "^1.9.3"
},
keywords: ["node", "azure", "typescript", "browser", "isomorphic"],
Expand Down
4 changes: 4 additions & 0 deletions src/models/operationDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export interface OperationDetails {
fullName: string;
description: string;
apiVersions: string[];
/**
* Operation parameters that are shared across requests.
*/
parameters: Parameter[];
requests: OperationRequestDetails[];
responses: OperationResponseDetails[];
typeDetails: TypeDetails;
Expand Down
1 change: 1 addition & 0 deletions src/transforms/operationTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export async function transformOperation(
? operation.apiVersions.map(v => v.version)
: [],
description: metadata.description,
parameters: operation.parameters || [],
requests,
responses,
mediaTypes,
Expand Down
5 changes: 1 addition & 4 deletions src/transforms/parameterTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,13 @@ const extractOperationParameters = (codeModel: CodeModel) =>
// Operations may have multiple requests, each with their own set of parameters.
// This is known to be the case when an operation can consume multiple media types.
// We need to ensure that the parameters from each request (method overload) is accounted for.
const recordMediaType = requests.length > 1;
const requestParams: OperationParameterDetails[] = [];
requests.forEach(request => {
request.parameters?.forEach(parameter => {
requestParams.push({
operationName,
parameter,
targetMediaType: recordMediaType
? request.protocol.http?.knownMediaType
: undefined
targetMediaType: request.protocol.http?.knownMediaType
});
});
});
Expand Down
5 changes: 4 additions & 1 deletion test/integration/generated/additionalProperties/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
"dependencies": {
"@azure/core-http": "1.1.3-dev.20200520.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/additional-properties.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const createAPTrueOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
const createCatAPTrueOperationSpec: coreHttp.OperationSpec = {
Expand All @@ -181,6 +182,7 @@ const createCatAPTrueOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters1,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
const createAPObjectOperationSpec: coreHttp.OperationSpec = {
Expand All @@ -197,6 +199,7 @@ const createAPObjectOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters2,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
const createAPStringOperationSpec: coreHttp.OperationSpec = {
Expand All @@ -213,6 +216,7 @@ const createAPStringOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters3,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
const createAPInPropertiesOperationSpec: coreHttp.OperationSpec = {
Expand All @@ -229,6 +233,7 @@ const createAPInPropertiesOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters4,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
const createAPInPropertiesWithAPStringOperationSpec: coreHttp.OperationSpec = {
Expand All @@ -245,5 +250,6 @@ const createAPInPropertiesWithAPStringOperationSpec: coreHttp.OperationSpec = {
requestBody: Parameters.createParameters5,
urlParameters: [Parameters.$host],
headerParameters: [Parameters.contentType],
mediaType: "json",
serializer
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
"dependencies": {
"@azure/core-http": "1.1.3-dev.20200520.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/azure-parameter-grouping.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const postRequiredOperationSpec: coreHttp.OperationSpec = {
queryParameters: [Parameters.query],
urlParameters: [Parameters.$host, Parameters.path],
headerParameters: [Parameters.contentType, Parameters.customHeader],
mediaType: "json",
serializer
};
const postOptionalOperationSpec: coreHttp.OperationSpec = {
Expand Down
5 changes: 4 additions & 1 deletion test/integration/generated/azureReport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
"dependencies": {
"@azure/core-http": "1.1.3-dev.20200520.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/zzzAzureReport.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
"dependencies": {
"@azure/core-http": "1.1.3-dev.20200520.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/azure-special-properties.js",
Expand Down
5 changes: 4 additions & 1 deletion test/integration/generated/bodyArray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"author": "Microsoft Corporation",
"description": "Test Infrastructure for AutoRest Swagger BAT",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
"dependencies": {
"@azure/core-http": "1.1.3-dev.20200520.1",
"tslib": "^1.9.3"
},
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/body-array.js",
Expand Down
Loading

0 comments on commit f26b833

Please sign in to comment.