Skip to content

Commit f26b833

Browse files
authored
Merge pull request Azure#647 from chradek/operation-spec-content-type
[v6] add mediaType to OperationSpecs and make param ordering deterministic
2 parents a4df799 + f7f1e27 commit f26b833

File tree

345 files changed

+2663
-1626
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

345 files changed

+2663
-1626
lines changed

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@azure-tools/codemodel": "4.13.317",
3434
"@azure-tools/linq": "3.1.206",
3535
"@azure-tools/openapi": "3.0.209",
36-
"@azure/core-http": "^1.1.1",
36+
"@azure/core-http": "1.1.3-dev.20200520.1",
3737
"@azure/core-lro": "^1.0.1",
3838
"@azure/core-paging": "^1.0.0",
3939
"@azure/logger": "^1.0.0",

src/generators/operationGenerator.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
ChoiceSchema,
3737
SealedChoiceSchema,
3838
ConstantSchema,
39-
Parameter
39+
Parameter,
40+
ImplementationLocation
4041
} from "@azure-tools/codemodel";
4142
import { getLanguageMetadata } from "../utils/languageHelpers";
4243
import { shouldImportParameters } from "./utils/importUtils";
@@ -146,16 +147,25 @@ function buildSpec(spec: OperationSpecDetails): string {
146147
const urlParams = buildParameters(spec, "urlParameters");
147148
const headerParams = buildParameters(spec, "headerParameters");
148149
const contentType = buildContentType(spec);
150+
const mediaType = buildMediaType(spec);
151+
149152
const isXML = spec.isXML ? "isXML: true," : "";
150153

151154
return `{ path: "${spec.path}", httpMethod: "${
152155
spec.httpMethod
153156
}", responses: {${responses.join(
154157
", "
155-
)}},${requestBody}${queryParams}${urlParams}${headerParams}${isXML}${contentType}serializer
158+
)}},${requestBody}${queryParams}${urlParams}${headerParams}${isXML}${contentType}${mediaType}serializer
156159
}`;
157160
}
158161

162+
function buildMediaType({ requestBody }: OperationSpecDetails) {
163+
if (requestBody?.targetMediaType) {
164+
return `mediaType: '${requestBody.targetMediaType}',`;
165+
}
166+
return "";
167+
}
168+
159169
function buildContentType({ requestBody, isXML }: OperationSpecDetails) {
160170
return requestBody && isXML
161171
? "contentType: 'application/xml; charset=utf-8',"
@@ -415,6 +425,50 @@ type ParameterWithDescription = OptionalKind<
415425
}
416426
>;
417427

428+
/**
429+
* Sorts the list of operation parameters to match the order described by the CodeModel.
430+
* @param operation Details about an operation.
431+
* @param request Details about an operation overload.
432+
* @param parameterDeclarations List of required parameter declarations for the provided operation overload.
433+
*/
434+
function sortOperationParameters(
435+
operation: OperationDetails,
436+
request: OperationRequestDetails,
437+
parameterDeclarations: ParameterWithDescription[]
438+
): ParameterWithDescription[] {
439+
// Get a sorted list of parameter names for this operation/request.
440+
// Note that this may inlcude parameters that aren't displayed, e.g. constant types.
441+
const expectedParameterOrdering = [
442+
...operation.parameters,
443+
...(request.parameters ?? [])
444+
]
445+
// Only parameters that are implemented on the method should be considered.
446+
.filter(param => param.implementation === ImplementationLocation.Method)
447+
.map(param => getLanguageMetadata(param.language).name);
448+
449+
const orderedParameterDeclarations: typeof parameterDeclarations = [];
450+
for (const parameterName of expectedParameterOrdering) {
451+
const index = parameterDeclarations.findIndex(
452+
p => p.name === parameterName
453+
);
454+
if (index === -1) {
455+
// No matching parameter found.
456+
// Common cases where this occurs is if a parameter
457+
// is optional, or a constant.
458+
continue;
459+
}
460+
461+
orderedParameterDeclarations.push(
462+
...parameterDeclarations.splice(index, 1)
463+
);
464+
}
465+
466+
// push any remaining parameters into the ordered parameter list
467+
orderedParameterDeclarations.push(...parameterDeclarations);
468+
469+
return orderedParameterDeclarations;
470+
}
471+
418472
/**
419473
* Gets a list of parameter declarations for each overload the operation supports,
420474
* and the list of parameter declarations for the base operation.
@@ -489,6 +543,13 @@ function getOperationParameterSignatures(
489543
parameterDeclarations
490544
);
491545

546+
// Sort the parameter declarations to match the signature the CodeModel suggests.
547+
const orderedParameterDeclarations = sortOperationParameters(
548+
operation,
549+
request,
550+
parameterDeclarations
551+
);
552+
492553
// add optional parameter
493554
const optionalParameter = getOptionsParameter(
494555
operation,
@@ -498,9 +559,9 @@ function getOperationParameterSignatures(
498559
mediaType: hasMultipleOverloads ? requestMediaType : undefined
499560
}
500561
);
501-
parameterDeclarations.push(optionalParameter);
562+
orderedParameterDeclarations.push(optionalParameter);
502563

503-
overloadParameterDeclarations.push(parameterDeclarations);
564+
overloadParameterDeclarations.push(orderedParameterDeclarations);
504565
}
505566

506567
// Create the parameter declarations for the base method signature.

src/generators/static/packageFileGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function generatePackageJson(
2222
version: packageDetails.version,
2323
dependencies: {
2424
...(hasLRO && { "@azure/core-lro": "^1.0.1" }),
25-
"@azure/core-http": "^1.1.1",
25+
"@azure/core-http": "1.1.3-dev.20200520.1",
2626
tslib: "^1.9.3"
2727
},
2828
keywords: ["node", "azure", "typescript", "browser", "isomorphic"],

src/models/operationDetails.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface OperationDetails {
5656
fullName: string;
5757
description: string;
5858
apiVersions: string[];
59+
/**
60+
* Operation parameters that are shared across requests.
61+
*/
62+
parameters: Parameter[];
5963
requests: OperationRequestDetails[];
6064
responses: OperationResponseDetails[];
6165
typeDetails: TypeDetails;

src/transforms/operationTransforms.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ export async function transformOperation(
373373
? operation.apiVersions.map(v => v.version)
374374
: [],
375375
description: metadata.description,
376+
parameters: operation.parameters || [],
376377
requests,
377378
responses,
378379
mediaTypes,

src/transforms/parameterTransforms.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,13 @@ const extractOperationParameters = (codeModel: CodeModel) =>
138138
// Operations may have multiple requests, each with their own set of parameters.
139139
// This is known to be the case when an operation can consume multiple media types.
140140
// We need to ensure that the parameters from each request (method overload) is accounted for.
141-
const recordMediaType = requests.length > 1;
142141
const requestParams: OperationParameterDetails[] = [];
143142
requests.forEach(request => {
144143
request.parameters?.forEach(parameter => {
145144
requestParams.push({
146145
operationName,
147146
parameter,
148-
targetMediaType: recordMediaType
149-
? request.protocol.http?.knownMediaType
150-
: undefined
147+
targetMediaType: request.protocol.http?.knownMediaType
151148
});
152149
});
153150
});

test/integration/generated/additionalProperties/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"author": "Microsoft Corporation",
44
"description": "Test Infrastructure for AutoRest",
55
"version": "1.0.0-preview1",
6-
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
6+
"dependencies": {
7+
"@azure/core-http": "1.1.3-dev.20200520.1",
8+
"tslib": "^1.9.3"
9+
},
710
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
811
"license": "MIT",
912
"main": "./dist/additional-properties.js",

test/integration/generated/additionalProperties/src/operations/pets.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ const createAPTrueOperationSpec: coreHttp.OperationSpec = {
165165
requestBody: Parameters.createParameters,
166166
urlParameters: [Parameters.$host],
167167
headerParameters: [Parameters.contentType],
168+
mediaType: "json",
168169
serializer
169170
};
170171
const createCatAPTrueOperationSpec: coreHttp.OperationSpec = {
@@ -181,6 +182,7 @@ const createCatAPTrueOperationSpec: coreHttp.OperationSpec = {
181182
requestBody: Parameters.createParameters1,
182183
urlParameters: [Parameters.$host],
183184
headerParameters: [Parameters.contentType],
185+
mediaType: "json",
184186
serializer
185187
};
186188
const createAPObjectOperationSpec: coreHttp.OperationSpec = {
@@ -197,6 +199,7 @@ const createAPObjectOperationSpec: coreHttp.OperationSpec = {
197199
requestBody: Parameters.createParameters2,
198200
urlParameters: [Parameters.$host],
199201
headerParameters: [Parameters.contentType],
202+
mediaType: "json",
200203
serializer
201204
};
202205
const createAPStringOperationSpec: coreHttp.OperationSpec = {
@@ -213,6 +216,7 @@ const createAPStringOperationSpec: coreHttp.OperationSpec = {
213216
requestBody: Parameters.createParameters3,
214217
urlParameters: [Parameters.$host],
215218
headerParameters: [Parameters.contentType],
219+
mediaType: "json",
216220
serializer
217221
};
218222
const createAPInPropertiesOperationSpec: coreHttp.OperationSpec = {
@@ -229,6 +233,7 @@ const createAPInPropertiesOperationSpec: coreHttp.OperationSpec = {
229233
requestBody: Parameters.createParameters4,
230234
urlParameters: [Parameters.$host],
231235
headerParameters: [Parameters.contentType],
236+
mediaType: "json",
232237
serializer
233238
};
234239
const createAPInPropertiesWithAPStringOperationSpec: coreHttp.OperationSpec = {
@@ -245,5 +250,6 @@ const createAPInPropertiesWithAPStringOperationSpec: coreHttp.OperationSpec = {
245250
requestBody: Parameters.createParameters5,
246251
urlParameters: [Parameters.$host],
247252
headerParameters: [Parameters.contentType],
253+
mediaType: "json",
248254
serializer
249255
};

test/integration/generated/azureParameterGrouping/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"author": "Microsoft Corporation",
44
"description": "Test Infrastructure for AutoRest",
55
"version": "1.0.0-preview1",
6-
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
6+
"dependencies": {
7+
"@azure/core-http": "1.1.3-dev.20200520.1",
8+
"tslib": "^1.9.3"
9+
},
710
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
811
"license": "MIT",
912
"main": "./dist/azure-parameter-grouping.js",

test/integration/generated/azureParameterGrouping/src/operations/parameterGrouping.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const postRequiredOperationSpec: coreHttp.OperationSpec = {
114114
queryParameters: [Parameters.query],
115115
urlParameters: [Parameters.$host, Parameters.path],
116116
headerParameters: [Parameters.contentType, Parameters.customHeader],
117+
mediaType: "json",
117118
serializer
118119
};
119120
const postOptionalOperationSpec: coreHttp.OperationSpec = {

test/integration/generated/azureReport/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"author": "Microsoft Corporation",
44
"description": "Test Infrastructure for AutoRest",
55
"version": "1.0.0-preview1",
6-
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
6+
"dependencies": {
7+
"@azure/core-http": "1.1.3-dev.20200520.1",
8+
"tslib": "^1.9.3"
9+
},
710
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
811
"license": "MIT",
912
"main": "./dist/zzzAzureReport.js",

test/integration/generated/azureSpecialProperties/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"author": "Microsoft Corporation",
44
"description": "Test Infrastructure for AutoRest",
55
"version": "1.0.0-preview1",
6-
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
6+
"dependencies": {
7+
"@azure/core-http": "1.1.3-dev.20200520.1",
8+
"tslib": "^1.9.3"
9+
},
710
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
811
"license": "MIT",
912
"main": "./dist/azure-special-properties.js",

test/integration/generated/bodyArray/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"author": "Microsoft Corporation",
44
"description": "Test Infrastructure for AutoRest Swagger BAT",
55
"version": "1.0.0-preview1",
6-
"dependencies": { "@azure/core-http": "^1.1.1", "tslib": "^1.9.3" },
6+
"dependencies": {
7+
"@azure/core-http": "1.1.3-dev.20200520.1",
8+
"tslib": "^1.9.3"
9+
},
710
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
811
"license": "MIT",
912
"main": "./dist/body-array.js",

0 commit comments

Comments
 (0)