Пример #1
0
        private void AddSchemas(ResourceNode node)
        {
            if (node.Method != null)
            {
                MethodDocumentationAttribute attribute = node.Method.GetCustomAttributes <MethodDocumentationAttribute>().FirstOrDefault();

                if (attribute != null)
                {
                    AddSchemas(attribute.RequestTypes);
                    AddSchemas(attribute.ResponseTypes);
                }
            }
            foreach (ResourceNode child in node.Children.Values)
            {
                AddSchemas(child);
            }
        }
        private void WriteMethod(StreamWriter writer, ResourceNode node, DocumentationHeaderSettings headerSettings, SchemaStore schemaStore, ExampleStore exampleStore, int indent)
        {
            MethodDocumentationAttribute attribute = node.Method.GetCustomAttributes <MethodDocumentationAttribute>().FirstOrDefault();

            if (node.AllowsAnonymous())
            {
                string securitySchemes = "null";
                if (attribute != null && attribute.AllowMultipleSecuritySchemes)
                {
                    securitySchemes = string.Concat(securitySchemes, ", oauth_2_0");
                }
                writer.WriteLine(string.Concat(GetIndentString(indent + 1), "securedBy: [", securitySchemes, "]"));
            }

            if (attribute != null)
            {
                if (attribute.Summary != null)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "description: |"));
                    writer.WriteLine(string.Concat(GetIndentString(indent + 2), attribute.Summary));
                    Example example = exampleStore.GetExample(node.Class, node.Method, "");
                    if (example != null)
                    {
                        writer.WriteLine(GetIndentString(indent + 2));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 2), "For more information, go to ", headerSettings.RepositoryFilesURI, "/", example.DocFilename, "#", example.DocHeading));
                    }
                    else if (attribute.RequestTypes != null)
                    {
                        SerialisationLog.Warning(string.Concat("No example to retrieve link to more information for ", node.Class.Name, ".", node.Method.Name));
                    }
                }
                else
                {
                    SerialisationLog.Warning(string.Concat("No summary for ", node.Class.Name, ".", node.Method.Name));
                }

                if (attribute.ResponseTypes != null && attribute.ResponseTypes.Any(r => r.GetProperties().Any(p => p.PropertyType == typeof(PageInfo))))
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "queryParameters: "));
                    foreach (KeyValuePair <string, Dictionary <string, string> > field in headerSettings.PagingFields)
                    {
                        writer.WriteLine(string.Concat(GetIndentString(indent + 2), field.Key, ":"));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "displayName: ", field.Value["displayName"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "description: ", field.Value["description"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "type: ", field.Value["type"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "required: ", field.Value["required"]));
                    }
                }

                if (attribute.RequestTypes != null)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "body: "));

                    foreach (Type requestType in attribute.RequestTypes)
                    {
                        ContentTypeAttribute contentTypeAttribute = requestType.GetCustomAttributes <ContentTypeAttribute>().FirstOrDefault();
                        if (contentTypeAttribute == null)
                        {
                            SerialisationLog.Warning(string.Concat("No ContentTypeAttribute for ", requestType.FullName));
                        }

                        if (attribute.RequestTypeNames != null)
                        {
                            foreach (string contentType in attribute.RequestTypeNames)
                            {
                                TDataExchangeFormat dataExchangeFormat = SerialisationUtils.GetDataExchangeFormatFromContentType(contentType);

                                if (dataExchangeFormat != TDataExchangeFormat.None)
                                {
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Request, requestType, indent + 2);
                                }
                                else
                                {
                                    SerialisationLog.Warning(string.Concat("No supported data exchange format for ", contentType));
                                }
                            }
                        }
                        else
                        {
                            foreach (TDataExchangeFormat dataExchangeFormat in Enum.GetValues(typeof(TDataExchangeFormat)))
                            {
                                if (SerialisationUtils.IsStandardDataExchangeFormat(dataExchangeFormat))
                                {
                                    string contentType = GetContentType(contentTypeAttribute, dataExchangeFormat);
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Request, requestType, indent + 2);
                                }
                            }
                        }
                    }
                }

                writer.WriteLine(string.Concat(GetIndentString(indent + 1), "responses:"));

                HttpStatusCode[] statusCodes = attribute.StatusCodes;
                if (node.Class.GetCustomAttributes <AuthorizeAttribute>().FirstOrDefault() != null)
                {
                    statusCodes = statusCodes.Concat(new[] { HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden }).ToArray();
                }
                foreach (HttpStatusCode statusCode in statusCodes)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 2), (int)statusCode, ":"));
                    WriteDefaultDescription(writer, headerSettings, statusCode, indent + 3);

                    if (SerialisationUtils.IsSuccessStatusCode(statusCode) && attribute.ResponseTypes != null)
                    {
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "body:"));
                        foreach (Type responseType in attribute.ResponseTypes)
                        {
                            ContentTypeAttribute contentTypeAttribute = responseType.GetCustomAttributes <ContentTypeAttribute>().FirstOrDefault();
                            if (contentTypeAttribute == null)
                            {
                                SerialisationLog.Warning(string.Concat("No ContentTypeAttribute for ", responseType.FullName));
                            }

                            foreach (TDataExchangeFormat dataExchangeFormat in Enum.GetValues(typeof(TDataExchangeFormat)))
                            {
                                if (SerialisationUtils.IsStandardDataExchangeFormat(dataExchangeFormat))
                                {
                                    string contentType = GetContentType(contentTypeAttribute, dataExchangeFormat);
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Response, responseType, indent + 4);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                SerialisationLog.Warning(string.Concat("No method-level documentation for ", node.Class.Name, ".", node.Method.Name));
            }
        }
Пример #3
0
        private string JsonToXml(Example currentExample, ResourceNode resourceTree, string json)
        {
            string     xml    = null;
            MethodInfo method = resourceTree.Find(currentExample.ClassName, currentExample.MethodName).Method;
            MethodDocumentationAttribute attribute = method.GetCustomAttributes <MethodDocumentationAttribute>().FirstOrDefault();

            if (attribute != null)
            {
                Type objectType = null;

                if (currentExample.ExampleType == TMessageType.Request)
                {
                    objectType = GetObjectType(attribute.RequestTypes, currentExample);
                }
                else
                {
                    objectType = GetObjectType(attribute.ResponseTypes, currentExample);
                }

                if (objectType != null)
                {
                    try
                    {
                        object deserialisedObject = JsonConvert.DeserializeObject(json, objectType);

                        Stream                  stream     = new MemoryStream();
                        XmlSerializer           serializer = new XmlSerializer(objectType);
                        XmlSerializerNamespaces xns        = new XmlSerializerNamespaces();
                        xns.Add(string.Empty, string.Empty);
                        serializer.Serialize(stream, deserialisedObject, xns);

                        stream.Position = 0;
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            reader.ReadLine();  // skip <?xml version="1.0"?>
                            xml = reader.ReadToEnd().Replace("+json", "+xml");
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        SerialisationLog.Warning(string.Concat("Failed to serialise XML example for ", currentExample.ClassName, ".", currentExample.MethodName));
                    }
                    catch (Exception ex)
                    {
                        if (ex is JsonReaderException || ex is JsonSerializationException)
                        {
                            SerialisationLog.Warning(string.Concat("Failed to deserialise JSON example for ", currentExample.ClassName, ".", currentExample.MethodName));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    SerialisationLog.Warning(string.Concat("No ", currentExample.ExampleType.ToString().ToLower(), " type for ", currentExample.ClassName, ".", currentExample.MethodName));
                }
            }
            else
            {
                SerialisationLog.Warning(string.Concat("Could not convert JSON example to XML due to no method documentation for ", currentExample.ClassName, ".", currentExample.MethodName));
            }
            return(xml);
        }