async function tweakOperation()

in powershell/plugins/sdk-tweak-model.ts [296:390]


async function tweakOperation(state: State) {
  for (const operationGroup of state.model.operationGroups) {
    if (isReserved(operationGroup.$key)) {
      operationGroup.$key = pascalCase(`${operationGroup.$key}Model`);
      operationGroup.language.default.name = operationGroup.$key;
    }
    for (const operation of operationGroup.operations) {
      let initializeResponseBody = '';
      operation.language.default.returnTypeHeader = {};
      operation.language.default.returnTypeHeader.name = '';
      if (operation.responses) {
        const schemas = new Set();
        let respCountWithBody = 0;
        let binaryResponse = false;
        // sometimes, it will generate a binary response with no schema
        let specialBinaryResponse = 0;
        operation.responses.forEach(function (resp) {
          if ((<any>resp).schema) {
            schemas.add((<any>resp).schema);
            if ((<Schema>((<any>resp).schema)).type === SchemaType.Binary) {
              binaryResponse = true;
            }
          } else if ((<any>resp).binary) {
            binaryResponse = true;
            specialBinaryResponse = 1;
          }
        });
        respCountWithBody = schemas.size + specialBinaryResponse;
        const isHead = operation.requests && operation.requests[0].protocol.http?.method === 'head';
        if (isHead) {
          const succeedCode = operation.responses.filter(r => (<string>r.protocol.http?.statusCodes[0]).startsWith('2'))[0].protocol.http?.statusCodes[0];
          initializeResponseBody = `_result.Body = (_statusCode == System.Net.HttpStatusCode.${(<any>StatusCodes)[succeedCode]});`;
        }
        const responses = operation.responses.filter(r => (<any>r).schema);
        const hasHeaderResponse = operation.responses.some(r => (<any>r).protocol.http.headers);
        const headerSchema = pascalCase(operationGroup.$key + (operation.language.default.original ?? operation.language.default.name) + 'Headers');
        operation.language.default.returnTypeHeader.name = hasHeaderResponse ? headerSchema : '';
        let headerPostfix = hasHeaderResponse ? `,${headerSchema}` : '';
        if (respCountWithBody === 0) {
          const statusCodes = new Array<string>();
          operation.responses.forEach(resp => statusCodes.push(resp.protocol.http?.statusCodes[0]));
          if (isHead && operation.responses?.length === 2 && statusCodes.includes('404') && !hasHeaderResponse) {
            operation.language.default.responseType = 'Microsoft.Rest.Azure.AzureOperationResponse<bool>';
            operation.language.default.returnType = 'bool';
          } else {
            headerPostfix = hasHeaderResponse ? (isHead ? `AzureOperationResponse<bool,${headerSchema}>` : `AzureOperationHeaderResponse<${headerSchema}>`) : 'AzureOperationResponse';
            operation.language.default.responseType = `Microsoft.Rest.Azure.${headerPostfix}`;
            operation.language.default.returnType = hasHeaderResponse ? (isHead ? 'bool' : headerSchema) : 'void';
          }
        } else if (respCountWithBody === 1) {
          const respSchema = binaryResponse ? undefined : (<any>responses[0]).schema;
          if (operation.language.default.pageable) {
            const responseType = respSchema.language.default.virtualProperties.owned.find((p: VirtualProperty) => p.name === pascalCase(operation.language.default.pageable.itemName)).property.schema.elementType.language.csharp.fullname;
            if (responseType) {
              if (hasHeaderResponse) {
                operation.language.default.responseType = `Microsoft.Rest.Azure.AzureOperationResponse<${operation.language.default.pageable.ipageType}<${responseType}>${headerPostfix}>`;
              } else {
                operation.language.default.responseType = `Microsoft.Rest.Azure.AzureOperationResponse<${operation.language.default.pageable.ipageType}<${responseType}>>`;
              }
            } else {
              operation.language.default.responseType = 'Microsoft.Rest.Azure.AzureOperationResponse';
            }
            // Mark response as pageable
            if (respSchema.language.default.pagable == undefined) {
              respSchema.language.default.pagable = true;
            }
            operation.language.default.returnType = `${operation.language.default.pageable.ipageType}<${responseType}>`;
            operation.language.default.deserializeType = `${operation.language.default.pageable.pageType}<${responseType}>`;
          } else {
            if (respSchema) {
              respSchema.language.default.pagable = false;
            }
            const postfix = !!respSchema && (valueType(respSchema.type)
              || (respSchema.type === SchemaType.SealedChoice && respSchema.extensions && !respSchema.extensions['x-ms-model-as-string'])
              || (respSchema.type === SchemaType.Choice && valueType((<ChoiceSchema>respSchema).choiceType.type))
              || (respSchema.type === SchemaType.SealedChoice && respSchema.extensions && valueType((<SealedChoiceSchema>respSchema).choiceType.type))
            ) ? '?' : '';
            const fullname = binaryResponse ? 'System.IO.Stream' : ((respSchema.type === SchemaType.Choice || (respSchema.type === SchemaType.SealedChoice && (valueType((<SealedChoiceSchema>respSchema).choiceType.type) || (respSchema.extensions && respSchema.extensions['x-ms-model-as-string'])))) ? (<SealedChoiceSchema>respSchema).choiceType.language.csharp?.fullname : respSchema.language.csharp.fullname);
            operation.language.default.responseType = `Microsoft.Rest.Azure.AzureOperationResponse<${fullname}${postfix}${headerPostfix}>`;
            operation.language.default.returnType = `${fullname}${postfix}`;
          }
        } else {
          operation.language.default.responseType = `Microsoft.Rest.Azure.AzureOperationResponse<object${headerPostfix}>`;
          operation.language.default.returnType = 'object';
        }
      } else {
        operation.language.default.responseType = 'Microsoft.Rest.Azure.AzureOperationResponse';
        operation.language.default.returnType = 'void';
      }
      operation.language.default.initializeResponseBody = initializeResponseBody;
      addMethodParameterDeclaration(operation, state);
      setFailureStatusCodePredicate(operation);
    }
  }
}