Skip to content

Commit

Permalink
Merge pull request Azure#171 from Azure/daschult/clientproperties
Browse files Browse the repository at this point in the history
Get client properties from ServiceClient instead of OperationArguments
  • Loading branch information
Dan Schulte authored Jul 11, 2018
2 parents 32c272d + 701b559 commit e9264bf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
56 changes: 39 additions & 17 deletions lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class ServiceClient {
}
if (operationSpec.urlParameters && operationSpec.urlParameters.length > 0) {
for (const urlParameter of operationSpec.urlParameters) {
let urlParameterValue: string = getOperationArgumentValueFromParameter(operationArguments, urlParameter, operationSpec.serializer);
let urlParameterValue: string = getOperationArgumentValueFromParameter(this, operationArguments, urlParameter, operationSpec.serializer);
urlParameterValue = operationSpec.serializer.serialize(urlParameter.mapper, urlParameterValue, getPathStringFromParameter(urlParameter));
if (!urlParameter.skipEncoding) {
urlParameterValue = encodeURIComponent(urlParameterValue);
Expand All @@ -202,7 +202,7 @@ export class ServiceClient {
}
if (operationSpec.queryParameters && operationSpec.queryParameters.length > 0) {
for (const queryParameter of operationSpec.queryParameters) {
let queryParameterValue: any = getOperationArgumentValueFromParameter(operationArguments, queryParameter, operationSpec.serializer);
let queryParameterValue: any = getOperationArgumentValueFromParameter(this, operationArguments, queryParameter, operationSpec.serializer);
if (queryParameterValue != undefined) {
queryParameterValue = operationSpec.serializer.serialize(queryParameter.mapper, queryParameterValue, getPathStringFromParameter(queryParameter));
if (queryParameter.collectionFormat != undefined) {
Expand Down Expand Up @@ -241,7 +241,7 @@ export class ServiceClient {

if (operationSpec.headerParameters) {
for (const headerParameter of operationSpec.headerParameters) {
let headerValue: any = getOperationArgumentValueFromParameter(operationArguments, headerParameter, operationSpec.serializer);
let headerValue: any = getOperationArgumentValueFromParameter(this, operationArguments, headerParameter, operationSpec.serializer);
if (headerValue != undefined) {
headerValue = operationSpec.serializer.serialize(headerParameter.mapper, headerValue, getPathStringFromParameter(headerParameter));
const headerCollectionPrefix = (headerParameter.mapper as DictionaryMapper).headerCollectionPrefix;
Expand Down Expand Up @@ -276,7 +276,7 @@ export class ServiceClient {

httpRequest.withCredentials = this._withCredentials;

serializeRequestBody(httpRequest, operationArguments, operationSpec);
serializeRequestBody(this, httpRequest, operationArguments, operationSpec);

if (operationSpec.responses) {
let rawResponse = false;
Expand All @@ -298,9 +298,9 @@ export class ServiceClient {
}
}

export function serializeRequestBody(httpRequest: WebResource, operationArguments: OperationArguments, operationSpec: OperationSpec): void {
export function serializeRequestBody(serviceClient: ServiceClient, httpRequest: WebResource, operationArguments: OperationArguments, operationSpec: OperationSpec): void {
if (operationSpec.requestBody && operationSpec.requestBody.mapper) {
httpRequest.body = getOperationArgumentValueFromParameter(operationArguments, operationSpec.requestBody, operationSpec.serializer);
httpRequest.body = getOperationArgumentValueFromParameter(serviceClient, operationArguments, operationSpec.requestBody, operationSpec.serializer);

const bodyMapper = operationSpec.requestBody.mapper;
const { required, xmlName, xmlElementName, serializedName } = bodyMapper;
Expand All @@ -327,7 +327,7 @@ export function serializeRequestBody(httpRequest: WebResource, operationArgument
} else if (operationSpec.formDataParameters && operationSpec.formDataParameters.length > 0) {
httpRequest.formData = {};
for (const formDataParameter of operationSpec.formDataParameters) {
const formDataParameterValue: any = getOperationArgumentValueFromParameter(operationArguments, formDataParameter, operationSpec.serializer);
const formDataParameterValue: any = getOperationArgumentValueFromParameter(serviceClient, operationArguments, formDataParameter, operationSpec.serializer);
if (formDataParameterValue != undefined) {
const formDataParameterPropertyName: string = formDataParameter.mapper.serializedName || getPathStringFromParameter(formDataParameter);
httpRequest.formData[formDataParameterPropertyName] = operationSpec.serializer.serialize(formDataParameter.mapper, formDataParameterValue, getPathStringFromParameter(formDataParameter));
Expand Down Expand Up @@ -384,25 +384,23 @@ export function getPropertyParent(parent: PropertyParent, propertyPath: string[]
return parent;
}

function getOperationArgumentValueFromParameter(operationArguments: OperationArguments, parameter: OperationParameter, serializer: Serializer): any {
return getOperationArgumentValueFromParameterPath(operationArguments, parameter.parameterPath, parameter.mapper, serializer);
function getOperationArgumentValueFromParameter(serviceClient: ServiceClient, operationArguments: OperationArguments, parameter: OperationParameter, serializer: Serializer): any {
return getOperationArgumentValueFromParameterPath(serviceClient, operationArguments, parameter.parameterPath, parameter.mapper, serializer);
}

function getOperationArgumentValueFromParameterPath(operationArguments: OperationArguments, parameterPath: ParameterPath, parameterMapper: Mapper, serializer: Serializer): any {
function getOperationArgumentValueFromParameterPath(serviceClient: ServiceClient, operationArguments: OperationArguments, parameterPath: ParameterPath, parameterMapper: Mapper, serializer: Serializer): any {
let value: any;
if (typeof parameterPath === "string") {
parameterPath = [parameterPath];
}
if (operationArguments.arguments) {
if (Array.isArray(parameterPath)) {
if (parameterPath.length > 0) {
value = operationArguments.arguments;
for (const parameterPathPart of parameterPath) {
value = value[parameterPathPart];
if (value == undefined) {
break;
}
let propertySearchResult: PropertySearchResult = getPropertyFromParameterPath(operationArguments.arguments, parameterPath);
if (!propertySearchResult.propertyFound) {
propertySearchResult = getPropertyFromParameterPath(serviceClient, parameterPath);
}
value = propertySearchResult.propertyValue;

// Serialize just for validation purposes.
const parameterPathString: string = getPathStringFromParameterPath(parameterPath, parameterMapper);
Expand All @@ -412,7 +410,7 @@ function getOperationArgumentValueFromParameterPath(operationArguments: Operatio
for (const propertyName in parameterPath) {
const propertyMapper: Mapper = (parameterMapper as CompositeMapper).type.modelProperties[propertyName];
const propertyPath: ParameterPath = parameterPath[propertyName];
const propertyValue: any = getOperationArgumentValueFromParameterPath(operationArguments, propertyPath, propertyMapper, serializer);
const propertyValue: any = getOperationArgumentValueFromParameterPath(serviceClient, operationArguments, propertyPath, propertyMapper, serializer);
// Serialize just for validation purposes.
const propertyPathString: string = getPathStringFromParameterPath(propertyPath, propertyMapper);
serializer.serialize(propertyMapper, propertyValue, propertyPathString);
Expand All @@ -427,3 +425,27 @@ function getOperationArgumentValueFromParameterPath(operationArguments: Operatio
}
return value;
}

interface PropertySearchResult {
propertyValue?: any;
propertyFound: boolean;
}

function getPropertyFromParameterPath(parent: { [parameterName: string]: any }, parameterPath: string[]): PropertySearchResult {
const result: PropertySearchResult = { propertyFound: false };
let i = 0;
for (; i < parameterPath.length; ++i) {
const parameterPathPart: string = parameterPath[i];
// Make sure to check inherited properties too, so don't use hasOwnProperty().
if (parent != undefined && parameterPathPart in parent) {
parent = parent[parameterPathPart];
} else {
break;
}
}
if (i === parameterPath.length) {
result.propertyValue = parent;
result.propertyFound = true;
}
return result;
}
6 changes: 6 additions & 0 deletions test/shared/serviceClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe("ServiceClient", function () {
it("should serialize a JSON String request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down Expand Up @@ -189,6 +190,7 @@ describe("ServiceClient", function () {
it("should serialize a JSON ByteArray request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down Expand Up @@ -216,6 +218,7 @@ describe("ServiceClient", function () {
it("should serialize a JSON Stream request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down Expand Up @@ -243,6 +246,7 @@ describe("ServiceClient", function () {
it("should serialize an XML String request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down Expand Up @@ -273,6 +277,7 @@ describe("ServiceClient", function () {
it("should serialize an XML ByteArray request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down Expand Up @@ -303,6 +308,7 @@ describe("ServiceClient", function () {
it("should serialize an XML Stream request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
arguments: {
Expand Down

0 comments on commit e9264bf

Please sign in to comment.