Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support request body #637

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions powershell/autorest-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@ pipeline:
psnamer-v2:
input: csnamer-v2

llcsharp-v2:
add-azure-completers-v2:
input: psnamer-v2

llcsharp-v2:
input: add-azure-completers-v2

powershell-v2:
input: psnamer-v2
input: add-azure-completers-v2
# --- extension powershell ---

# creates high-level commands
Expand Down
207 changes: 106 additions & 101 deletions powershell/cmdlets/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1592,23 +1592,29 @@ export class NewCmdletClass extends Class {

public get headerComment(): string {
const header = super.headerComment;
const ops = '';
let ops = '';

for (const httpOperation of values(this.operation.callGraph)) {
// ops = `${ops}\n[OpenAPI] ${httpOperation.operationId}=>${httpOperation.method.toUpperCase()}:"${httpOperation.path}"`;
// skip-for-time-being
// if (this.debugMode) {
// const m = (httpOperation.extensions && httpOperation.extensions['x-ms-metadata']) || (httpOperation.pathExtensions ? httpOperation.pathExtensions['x-ms-metadata'] : undefined);
// if (m) {
// ops = `${ops}\n [METADATA]\n${serialize(m)}`;
// }

// ops = `${ops}\n [DETAILS]`;
// ops = `${ops}\n verb: ${this.operation.details.csharp.verb}`;
// ops = `${ops}\n subjectPrefix: ${this.operation.details.csharp.subjectPrefix}`;
// ops = `${ops}\n subject: ${this.operation.details.csharp.subject}`;
// ops = `${ops}\n variant: ${this.operation.details.csharp.name}`;
// }
const request = httpOperation.requests?.[0];
if (!request) {
continue;
}
const httpMethod = request.protocol.http?.method ?? '';
const httpPath = request.protocol.http?.path ?? '';
ops = `${ops}\n[OpenAPI] ${httpOperation.language.default.name}=>${httpMethod.toUpperCase()}:"${httpPath}"`;
if (this.debugMode) {
// x-ms-metadata seems no longer exists
// const m = (httpOperation.extensions && httpOperation.extensions['x-ms-metadata']) || (httpOperation.pathExtensions ? httpOperation.pathExtensions['x-ms-metadata'] : undefined);
// if (m) {
// ops = `${ops}\n [METADATA]\n${serialize(m)}`;
// }

ops = `${ops}\n [DETAILS]`;
ops = `${ops}\n verb: ${this.operation.details.csharp.verb}`;
ops = `${ops}\n subjectPrefix: ${this.operation.details.csharp.subjectPrefix}`;
ops = `${ops}\n subject: ${this.operation.details.csharp.subject}`;
ops = `${ops}\n variant: ${this.operation.details.csharp.name}`;
}
}

return ops ? `${header}\n${docComment(xmlize('remarks', ops))}` : header;
Expand Down Expand Up @@ -1837,7 +1843,7 @@ export class NewCmdletClass extends Class {
name: p.language.csharp?.name,
param: values($this.properties).
where(each => each.metadata.parameterDefinition).
first(each => each.metadata.parameterDefinition.schema === p.schema),
first(each => each.metadata.parameterDefinition.language.csharp?.serializedName === p.language.csharp?.serializedName), // xichen: Is it safe enough to use serializedName?
isPathParam: $this.isViaIdentity && p.protocol.http?.in === ParameterLocation.Path
};

Expand Down Expand Up @@ -2086,72 +2092,71 @@ export class NewCmdletClass extends Class {
const actualCall = function* () {
yield $this.eventListener.signal(Events.CmdletBeforeAPICall);
const idOpParams = operationParameters.filter(each => !each.isPathParam);
// skip-for-time-being, viaidentity
// const idschema = values($this.state.project.model.schemas).first(each => each.details.default.uid === 'universal-parameter-type');


// if ($this.isViaIdentity) {
// const identityFromPathParams = function* () {
// yield '// try to call with PATH parameters from Input Object';

// if (idschema) {
// const allVPs = getAllPublicVirtualProperties(idschema.details.csharp.virtualProperties);
// const props = [...values(idschema.properties)];

// const idOpParams = operationParameters.map(each => {
// const pascalName = pascalCase(`${each.name}`);

// if (!each.isPathParam) {
// return {
// name: undefined,
// value: valueOf(each.expression)
// };
// }
// const match = props.find(p => pascalCase(p.serializedName) === pascalName);
// if (match) {

// const defaultOfType = $this.state.project.schemaDefinitionResolver.resolveTypeDeclaration(<Schema>match.schema, true, $this.state).defaultOfType;
// // match up vp name
// const vp = allVPs.find(pp => pascalCase(pp.property.serializedName) === pascalName);
// if (vp) {
// return {
// name: `InputObject.${vp.name}`,
// value: `InputObject.${vp.name} ?? ${defaultOfType}`
// };
// }
// // fall back!

// console.error(`Unable to match identity parameter '${each.name}' member to appropriate virtual parameter. (Guessing '${pascalCase(match.details.csharp.name)}').`);
// return {
// name: `InputObject.${pascalCase(match.details.csharp.name)}`,
// value: `InputObject.${pascalCase(match.details.csharp.name)} ?? ${defaultOfType}`
// };
// }
// console.error(`Unable to match idenity parameter '${each.name}' member to appropriate virtual parameter. (Guessing '${pascalName}')`);
// return {
// name: `InputObject.${pascalName}`,
// value: `InputObject.${pascalName}`
// };
// });
// for (const opParam of idOpParams) {
// if (opParam.name) {
// yield If(IsNull(opParam.name), `ThrowTerminatingError( new ${ErrorRecord}(new global::System.Exception("InputObject has null value for ${opParam.name}"),string.Empty, ${ErrorCategory('InvalidArgument')}, InputObject) );`);
// }
// }
// yield `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.details.csharp.name}`, ...[...idOpParams.map(each => toExpression(each.value)), ...callbackMethods, dotnet.This, pipeline]).implementation}`;

// }
// };

// if (idschema && values(idschema.properties).first(each => each.details.csharp.uid === 'universal-parameter:resource identity')) {
// yield If('InputObject?.Id != null', `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.details.csharp.name}ViaIdentity`, ...[toExpression('InputObject.Id'), ...idOpParams.map(each => each.expression), ...callbackMethods, dotnet.This, pipeline]).implementation}`);
// yield Else(identityFromPathParams);
// } else {
// yield identityFromPathParams;
// }
// } else {
yield `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.language.csharp?.name}`, ...[...operationParameters.map(each => each.expression), ...callbackMethods, dotnet.This, pipeline]).implementation}`;
//}
const idschema = values($this.state.project.model.schemas.objects).first(each => each.language.default.uid === 'universal-parameter-type');


if ($this.isViaIdentity) {
const identityFromPathParams = function* () {
yield '// try to call with PATH parameters from Input Object';

if (idschema) {
const allVPs = NewGetAllPublicVirtualProperties(idschema.language.csharp?.virtualProperties);
const props = [...values(idschema.properties)];

const idOpParams = operationParameters.map(each => {
const pascalName = pascalCase(`${each.name}`);

if (!each.isPathParam) {
return {
name: undefined,
value: valueOf(each.expression)
};
}
const match = props.find(p => pascalCase(p.serializedName) === pascalName);
if (match) {

const defaultOfType = $this.state.project.schemaDefinitionResolver.resolveTypeDeclaration(match.schema, true, $this.state).defaultOfType;
// match up vp name
const vp = allVPs.find(pp => pascalCase(pp.property.serializedName) === pascalName);
if (vp) {
return {
name: `InputObject.${vp.name}`,
value: `InputObject.${vp.name} ?? ${defaultOfType}`
};
}
// fall back!

console.error(`Unable to match identity parameter '${each.name}' member to appropriate virtual parameter. (Guessing '${pascalCase(match.language.csharp?.name ?? '')}').`);
return {
name: `InputObject.${pascalCase(match.language.csharp?.name ?? '')}`,
value: `InputObject.${pascalCase(match.language.csharp?.name ?? '')} ?? ${defaultOfType}`
};
}
console.error(`Unable to match idenity parameter '${each.name}' member to appropriate virtual parameter. (Guessing '${pascalName}')`);
return {
name: `InputObject.${pascalName}`,
value: `InputObject.${pascalName}`
};
});
for (const opParam of idOpParams) {
if (opParam.name) {
yield If(IsNull(opParam.name), `ThrowTerminatingError( new ${ErrorRecord}(new global::System.Exception("InputObject has null value for ${opParam.name}"),string.Empty, ${ErrorCategory('InvalidArgument')}, InputObject) );`);
}
}
yield `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.language.csharp?.name}`, ...[...idOpParams.map(each => toExpression(each.value)), ...callbackMethods, dotnet.This, pipeline]).implementation}`;

}
};

if (idschema && values(idschema.properties).first(each => each.language.csharp?.uid === 'universal-parameter:resource identity')) {
yield If('InputObject?.Id != null', `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.language.csharp?.name}ViaIdentity`, ...[toExpression('InputObject.Id'), ...idOpParams.map(each => each.expression), ...callbackMethods, dotnet.This, pipeline]).implementation}`);
yield Else(identityFromPathParams);
} else {
yield identityFromPathParams;
}
} else {
yield `await this.${$this.$<Property>('Client').invokeMethod(`${apiCall.language.csharp?.name}`, ...[...operationParameters.map(each => each.expression), ...callbackMethods, dotnet.This, pipeline]).implementation}`;
}
yield $this.eventListener.signal(Events.CmdletAfterAPICall);
};

Expand Down Expand Up @@ -2505,19 +2510,18 @@ export class NewCmdletClass extends Class {
}
}

// skip-for-time-being
// if (this.isViaIdentity) {
// // add in the pipeline parameter for the identity

// const idschema = values(this.state.project.model.schemas).first(each => each.details.default.uid === 'universal-parameter-type');
// const idtd = this.state.project.schemaDefinitionResolver.resolveTypeDeclaration(<Schema>idschema, true, this.state);
// const idParam = this.add(new BackedProperty('InputObject', idtd, {
// description: 'Identity Parameter'
// }));
// const parameters = [new LiteralExpression('Mandatory = true'), new LiteralExpression('HelpMessage = "Identity Parameter"'), new LiteralExpression('ValueFromPipeline = true')];
// idParam.add(new Attribute(ParameterAttribute, { parameters }));
// idParam.add(new Attribute(CategoryAttribute, { parameters: [`${ParameterCategory}.Path`] }));
// }
if (this.isViaIdentity) {
// add in the pipeline parameter for the identity

const idschema = values(this.state.project.model.schemas.objects).first(each => each.language.default.uid === 'universal-parameter-type');
const idtd = this.state.project.schemaDefinitionResolver.resolveTypeDeclaration(idschema, true, this.state);
const idParam = this.add(new BackedProperty('InputObject', idtd, {
description: 'Identity Parameter'
}));
const parameters = [new LiteralExpression('Mandatory = true'), new LiteralExpression('HelpMessage = "Identity Parameter"'), new LiteralExpression('ValueFromPipeline = true')];
idParam.add(new Attribute(ParameterAttribute, { parameters }));
idParam.add(new Attribute(CategoryAttribute, { parameters: [`${ParameterCategory}.Path`] }));
}
for (const vParam of values(vps.operation)) {
if (vParam.name === 'Host') {
// skip 'Host'
Expand Down Expand Up @@ -2576,7 +2580,7 @@ export class NewCmdletClass extends Class {
regularCmdletParameter.add(new Attribute(AllowEmptyCollectionAttribute));
}

NewAddInfoAttribute(regularCmdletParameter, propertyType, vParam.required, false, vParam.description, origin.details.default.name);
NewAddInfoAttribute(regularCmdletParameter, propertyType, vParam.required ?? false, false, vParam.description, origin.details.default.serializedName);
NewAddCompleterInfo(regularCmdletParameter, vParam);
addDefaultInfo(regularCmdletParameter, vParam);

Expand All @@ -2588,12 +2592,13 @@ export class NewCmdletClass extends Class {
const httpParam = origin.details.csharp.httpParameter;
//const uid = httpParam ? httpParam.details.csharp.uid : 'no-parameter';

const cat = values(operation.callGraph[0].parameters).
where(each => !(each.language.csharp?.constantValue)).
first(each => each.schema === httpParam.schema);
if (httpParam) {
// xichen: Is it safe to compare by csharp serializedName? Because we no longer have uid
const cat = operation.callGraph[0].parameters?.find((param) => !param.language.csharp?.constantValue && param.language.csharp?.serializedName === httpParam.language.csharp?.serializedName);

if (cat) {
regularCmdletParameter.add(new Attribute(CategoryAttribute, { parameters: [`${ParameterCategory}.${pascalCase((cat.protocol.http?.in))}`] }));
if (cat) {
regularCmdletParameter.add(new Attribute(CategoryAttribute, { parameters: [`${ParameterCategory}.${pascalCase((cat.protocol.http?.in))}`] }));
}
}


Expand Down
2 changes: 1 addition & 1 deletion powershell/cmdlets/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class NewCmdletNamespace extends Namespace {
return this.state.project.cmdletFolder;
}

constructor(parent: Namespace, private state: NewState, objectInitializer?: DeepPartial<CmdletNamespace>) {
constructor(parent: Namespace, private state: NewState, objectInitializer?: DeepPartial<NewCmdletNamespace>) {
super('Cmdlets', parent);
this.apply(objectInitializer);
}
Expand Down
2 changes: 1 addition & 1 deletion powershell/llcsharp/model/model-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ export class NewModelClass extends Class implements NewEnhancedTypeDeclaration {
initializer: actualProperty.language.csharp?.constantValue ? typeof actualProperty.language.csharp.constantValue === 'string' ? new StringExpression(actualProperty.language.csharp.constantValue) : new LiteralExpression(actualProperty.language.csharp.constantValue) : undefined
});

if (actualProperty.language.csharp?.readOnly) {
if (actualProperty.language.csharp?.readOnly || actualProperty.readOnly) {
myProperty.set = undefined;
}
myProperty.language = virtualProperty.property.language;
Expand Down
Loading