private static void AddTypeProperty(TSObject mapper, string mapperTypeName, Action <TSObject> additionalTypeObjectPropertiesAction = null)
 {
     mapper.ObjectProperty("type", typeObject =>
     {
         typeObject.QuotedStringProperty("name", mapperTypeName);
         additionalTypeObjectPropertiesAction?.Invoke(typeObject);
     });
 }
Пример #2
0
 private static void GenerateRequestParameterPath(TSObject parent, string propertyName, string parameterName, bool parameterInOptions, ParameterTransformations parameterTransformations)
 {
     if (!parameterTransformations.IsCreatedFromTransformation(parameterName))
     {
         if (parameterInOptions || parameterTransformations.InputParameterInOptions(parameterName))
         {
             parent.QuotedStringArrayProperty(propertyName, new string[] { "options", parameterName });
         }
         else
         {
             parent.QuotedStringProperty(propertyName, parameterName);
         }
     }
     else if (parameterTransformations.IsUnflattenedVariable(parameterName))
     {
         // Unflattening
         parent.ObjectProperty(propertyName, parameterPathObject =>
         {
             IDictionary <string, string> unflattenedPropertyMappings = parameterTransformations.GetUnflattenedParameterPropertyMappings(parameterName);
             foreach (KeyValuePair <string, string> unflattenedPropertyMapping in unflattenedPropertyMappings)
             {
                 string unflattenedPropertyName = unflattenedPropertyMapping.Key;
                 string inputParameterName      = unflattenedPropertyMapping.Value;
                 bool inputParameterInOptions   = parameterTransformations.InputParameterInOptions(inputParameterName);
                 GenerateRequestParameterPath(parameterPathObject, unflattenedPropertyName, inputParameterName, inputParameterInOptions, parameterTransformations);
             }
         });
     }
     else
     {
         // Ungrouping
         string[] inputParameterPath          = parameterTransformations.GetUngroupedParameterPath(parameterName);
         string   inputParameterPathFirstPart = inputParameterPath[0];
         bool     inputParameterInOptions     = parameterTransformations.InputParameterInOptions(inputParameterPathFirstPart);
         if (inputParameterPath.Length == 1)
         {
             GenerateRequestParameterPath(parent, propertyName, inputParameterPath[0], inputParameterInOptions, parameterTransformations);
         }
         else
         {
             parent.ArrayProperty(propertyName, array =>
             {
                 if (inputParameterInOptions)
                 {
                     array.QuotedString("options");
                 }
                 foreach (string inputParameterPathPart in inputParameterPath)
                 {
                     array.QuotedString(inputParameterPathPart);
                 }
             });
         }
     }
 }
Пример #3
0
        public void GenerateOperationSpec(TSObject operationSpec)
        {
            operationSpec.QuotedStringProperty("httpMethod", HttpMethod.ToString().ToUpper());
            if (IsAbsoluteUrl)
            {
                operationSpec.QuotedStringProperty("baseUrl", CodeModelTS.BaseUrl);
            }

            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");
        }