public void GenerateOperationSpec(TSObject operationSpec) { operationSpec.QuotedStringProperty("httpMethod", HttpMethod.ToString().ToUpper()); if (IsAbsoluteUrl) { operationSpec.QuotedStringProperty("baseUrl", CodeModelTS.SchemeHostAndPort); } string path = Path; if (!string.IsNullOrEmpty(path)) { operationSpec.QuotedStringProperty("path", path); } Parameter[] logicalParameters = LogicalParameters.ToArray(); AddParameterRefs(operationSpec, "urlParameters", logicalParameters.Where(p => p.Location == ParameterLocation.Path)); AddParameterRefs(operationSpec, "queryParameters", logicalParameters.Where(p => p.Location == ParameterLocation.Query)); AddParameterRefs(operationSpec, "headerParameters", logicalParameters.Where(p => p.Location == ParameterLocation.Header)); bool addContentTypeProperty = (!string.IsNullOrEmpty(RequestContentType) && RequestContentType != CodeModelTS.RequestContentType); if (Body != null) { operationSpec.ObjectProperty("requestBody", requestBodyObject => { GenerateRequestParameterPath(requestBodyObject, Body, GetParameterTransformations()); requestBodyObject.Property("mapper", requestBodyMapper => ClientModelExtensions.ConstructRequestBodyMapper(requestBodyMapper, Body)); }); } else { IEnumerable <Parameter> formDataParameters = logicalParameters.Where(p => p.Location == ParameterLocation.FormData); if (formDataParameters.Any()) { AddParameterRefs(operationSpec, "formDataParameters", formDataParameters); addContentTypeProperty = true; } } if (addContentTypeProperty) { operationSpec.QuotedStringProperty("contentType", RequestContentType); } operationSpec.ObjectProperty("responses", responses => { bool isXml = CodeModelTS.ShouldGenerateXmlSerialization; foreach (KeyValuePair <HttpStatusCode, Response> statusCodeResponsePair in Responses) { HttpStatusCode statusCode = statusCodeResponsePair.Key; Response response = statusCodeResponsePair.Value; responses.ObjectProperty(((int)statusCode).ToString(), responseObject => { if (response.Body != null) { responseObject.Property("bodyMapper", responseBodyMapper => ClientModelExtensions.ConstructResponseBodyMapper(responseBodyMapper, response, this)); } if (response.Headers != null) { responseObject.Property("headersMapper", responseHeadersMapper => responseHeadersMapper.Text($"Mappers.{response.Headers.Name}")); } }); } responses.ObjectProperty("default", defaultResponseObject => { Response defaultResponse = DefaultResponse; if (defaultResponse != null && defaultResponse.Body != null) { defaultResponseObject.Property("bodyMapper", responseBodyMapper => ClientModelExtensions.ConstructResponseBodyMapper(responseBodyMapper, defaultResponse, this)); } }); }); if (CodeModel.ShouldGenerateXmlSerialization) { operationSpec.BooleanProperty("isXML", true); } operationSpec.TextProperty("serializer", "serializer"); }
/// <summary> /// Detremines whether the Uri will have any query string /// </summary> /// <returns>True if a query string is possible given the method parameters, otherwise false</returns> protected virtual bool HasQueryParameters() { return(LogicalParameters.Any(p => p.Location == ParameterLocation.Query)); }
private void AddQueryParametersToUri(string variableName, IndentedStringBuilder builder) { builder.AppendLine("System.Collections.Generic.List<string> _queryParameters = new System.Collections.Generic.List<string>();"); if (LogicalParameters.Any(p => p.Location == ParameterLocation.Query)) { foreach (var queryParameter in LogicalParameters .Where(p => p.Location == ParameterLocation.Query).Select(p => p as ParameterCsa)) { string queryParametersAddString = "_queryParameters.Add(string.Format(\"{0}={{0}}\", System.Uri.EscapeDataString({1})));"; if (queryParameter.IsODataFilterExpression) { queryParametersAddString = @"var _odataFilter = {2}.ToString(); if (!string.IsNullOrEmpty(_odataFilter)) {{ _queryParameters.Add(_odataFilter); }}"; } else if (queryParameter.Extensions.ContainsKey(AzureExtensions.SkipUrlEncodingExtension)) { queryParametersAddString = "_queryParameters.Add(string.Format(\"{0}={{0}}\", {1}));"; } if (queryParameter.IsNullable()) { builder.AppendLine("if ({0} != null)", queryParameter.Name) .AppendLine("{").Indent(); } if (queryParameter.CollectionFormat == CollectionFormat.Multi) { builder.AppendLine("if ({0}.Count == 0)", queryParameter.Name) .AppendLine("{").Indent() .AppendLine(queryParametersAddString, queryParameter.SerializedName, "string.Empty").Outdent() .AppendLine("}") .AppendLine("else") .AppendLine("{").Indent() .AppendLine("foreach (var _item in {0})", queryParameter.Name) .AppendLine("{").Indent() .AppendLine(queryParametersAddString, queryParameter.SerializedName, "_item ?? string.Empty").Outdent() .AppendLine("}").Outdent() .AppendLine("}").Outdent(); } else { builder.AppendLine(queryParametersAddString, queryParameter.SerializedName, queryParameter.GetFormattedReferenceValue(ClientReference), queryParameter.Name); } if (queryParameter.IsNullable()) { builder.Outdent() .AppendLine("}"); } } } builder.AppendLine("if (_queryParameters.Count > 0)") .AppendLine("{").Indent() .AppendLine("{0} += ({0}.Contains(\"?\") ? \"&\" : \"?\") + string.Join(\"&\", _queryParameters);", variableName).Outdent() .AppendLine("}"); }