Example #1
0
        private void BuildMethodParameters(Method method)
        {
            foreach (var swaggerParameter in DeduplicateParameters(_operation.Parameters))
            {
                var parameter = ((ParameterBuilder)swaggerParameter.GetBuilder(_swaggerModeler)).Build();
                method.Parameters.Add(parameter);

                StringBuilder parameterName = new StringBuilder(parameter.Name);
                parameterName = CollectionFormatBuilder.OnBuildMethodParameter(method, swaggerParameter,
                                                                               parameterName);

                if (swaggerParameter.In == ParameterLocation.Header)
                {
                    method.RequestHeaders[swaggerParameter.Name] =
                        string.Format(CultureInfo.InvariantCulture, "{{{0}}}", parameterName);
                }
            }
        }
Example #2
0
        public Method BuildMethod(HttpMethod httpMethod, string url, string methodName, string methodGroup)
        {
            EnsureUniqueMethodName(methodName, methodGroup);

            var method = new Method
            {
                HttpMethod = httpMethod,
                Url        = url,
                Name       = methodName
            };

            method.ContentType = APP_JSON_MIME;
            string produce = _effectiveProduces.FirstOrDefault(s => s.StartsWith(APP_JSON_MIME, StringComparison.OrdinalIgnoreCase));

            if (!string.IsNullOrEmpty(produce))
            {
                method.ContentType = produce;
            }

            if (method.ContentType.IndexOf("charset=", StringComparison.OrdinalIgnoreCase) == -1)
            {
                // Enable UTF-8 charset
                method.ContentType += "; charset=utf-8";
            }
            method.Description = _operation.Description;
            method.Summary     = _operation.Summary;

            // Service parameters
            if (_operation.Parameters != null)
            {
                foreach (var swaggerParameter in DeduplicateParameters(_operation.Parameters))
                {
                    var parameter = ((ParameterBuilder)swaggerParameter.GetBuilder(_swaggerModeler)).Build();
                    method.Parameters.Add(parameter);

                    StringBuilder parameterName = new StringBuilder(parameter.Name);
                    parameterName = CollectionFormatBuilder.OnBuildMethodParameter(method, swaggerParameter,
                                                                                   parameterName);

                    if (swaggerParameter.In == ParameterLocation.Header)
                    {
                        method.RequestHeaders[swaggerParameter.Name] =
                            string.Format(CultureInfo.InvariantCulture, "{{{0}}}", parameterName);
                    }
                }
            }

            // Response format
            var typesList = new List <Stack <IType> >();

            _operation.Responses.ForEach(response =>
            {
                if (string.Equals(response.Key, "default", StringComparison.OrdinalIgnoreCase))
                {
                    TryBuildDefaultResponse(methodName, response.Value, method);
                }
                else
                {
                    if (
                        !(TryBuildResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                                           typesList) ||
                          TryBuildStreamResponse(response.Key.ToHttpStatusCode(), response.Value, method, typesList) ||
                          TryBuildEmptyResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                                                typesList)))
                    {
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.InvariantCulture,
                                          Resources.UnsupportedMimeTypeForResponseBody,
                                          methodName,
                                          response.Key));
                    }
                }
            });

            method.ReturnType = BuildMethodReturnType(typesList);
            if (method.Responses.Count == 0 &&
                method.DefaultResponse != null)
            {
                method.ReturnType = method.DefaultResponse;
            }

            // Copy extensions
            _operation.Extensions.ForEach(extention => method.Extensions.Add(extention.Key, extention.Value));

            return(method);
        }
Example #3
0
        public Method BuildMethod(HttpMethod httpMethod, string url, string methodName, string methodGroup)
        {
            EnsureUniqueMethodName(methodName, methodGroup);

            var method = new Method
            {
                HttpMethod     = httpMethod,
                Url            = url,
                Name           = methodName,
                SerializedName = _operation.OperationId
            };

            method.RequestContentType = _effectiveConsumes.FirstOrDefault() ?? APP_JSON_MIME;
            string produce = _effectiveConsumes.FirstOrDefault(s => s.StartsWith(APP_JSON_MIME, StringComparison.OrdinalIgnoreCase));

            if (!string.IsNullOrEmpty(produce))
            {
                method.RequestContentType = produce;
            }

            if (method.RequestContentType.StartsWith(APP_JSON_MIME, StringComparison.OrdinalIgnoreCase) &&
                method.RequestContentType.IndexOf("charset=", StringComparison.OrdinalIgnoreCase) == -1)
            {
                // Enable UTF-8 charset
                method.RequestContentType += "; charset=utf-8";
            }
            method.Description = _operation.Description;
            method.Summary     = _operation.Summary;

            // Service parameters
            if (_operation.Parameters != null)
            {
                foreach (var swaggerParameter in DeduplicateParameters(_operation.Parameters))
                {
                    var parameter = ((ParameterBuilder)swaggerParameter.GetBuilder(_swaggerModeler)).Build();
                    method.Parameters.Add(parameter);

                    StringBuilder parameterName = new StringBuilder(parameter.Name);
                    parameterName = CollectionFormatBuilder.OnBuildMethodParameter(method, swaggerParameter,
                                                                                   parameterName);

                    if (swaggerParameter.In == ParameterLocation.Header)
                    {
                        method.RequestHeaders[swaggerParameter.Name] =
                            string.Format(CultureInfo.InvariantCulture, "{{{0}}}", parameterName);
                    }
                }
            }

            // Build header object
            var responseHeaders = new Dictionary <string, Header>();

            foreach (var response in _operation.Responses.Values)
            {
                if (response.Headers != null)
                {
                    response.Headers.ForEach(h => responseHeaders[h.Key] = h.Value);
                }
            }

            var headerTypeName = string.Format(CultureInfo.InvariantCulture,
                                               "{0}-{1}-Headers", methodGroup, methodName).Trim('-');
            var headerType = new CompositeType
            {
                Name           = headerTypeName,
                SerializedName = headerTypeName,
                Documentation  = string.Format(CultureInfo.InvariantCulture, "Defines headers for {0} operation.", methodName)
            };

            responseHeaders.ForEach(h =>
            {
                var property = new Property
                {
                    Name           = h.Key,
                    SerializedName = h.Key,
                    Type           = h.Value.GetBuilder(this._swaggerModeler).BuildServiceType(h.Key),
                    Documentation  = h.Value.Description
                };
                headerType.Properties.Add(property);
            });

            if (!headerType.Properties.Any())
            {
                headerType = null;
            }

            // Response format
            var typesList = new List <Stack <IType> >();

            foreach (var response in _operation.Responses)
            {
                if (string.Equals(response.Key, "default", StringComparison.OrdinalIgnoreCase))
                {
                    TryBuildDefaultResponse(methodName, response.Value, method, headerType);
                }
                else
                {
                    if (
                        !(TryBuildResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                                           typesList, headerType) ||
                          TryBuildStreamResponse(response.Key.ToHttpStatusCode(), response.Value, method, typesList, headerType) ||
                          TryBuildEmptyResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                                                typesList, headerType)))
                    {
                        throw new InvalidOperationException(
                                  string.Format(CultureInfo.InvariantCulture,
                                                Resources.UnsupportedMimeTypeForResponseBody,
                                                methodName,
                                                response.Key));
                    }
                }
            }

            method.ReturnType = BuildMethodReturnType(typesList, headerType);
            if (method.Responses.Count == 0)
            {
                method.ReturnType = method.DefaultResponse;
            }

            if (method.ReturnType.Headers != null)
            {
                _swaggerModeler.ServiceClient.HeaderTypes.Add(method.ReturnType.Headers as CompositeType);
            }

            // Copy extensions
            _operation.Extensions.ForEach(extention => method.Extensions.Add(extention.Key, extention.Value));

            return(method);
        }