Exemplo n.º 1
0
        public static RestApiRootItemViewModel FromSwaggerModel(SwaggerModel swagger)
        {
            var uid = GetUid(swagger);
            var vm = new RestApiRootItemViewModel
            {
                Name = swagger.Info.Title,
                Uid = uid,
                HtmlId = GetHtmlId(uid),
                Metadata = swagger.Metadata,
                Description = swagger.Description,
                Summary = swagger.Summary,
                Children = new List<RestApiChildItemViewModel>(),
                Raw = swagger.Raw,
                Tags = new List<RestApiTagViewModel>()
            };
            if (swagger.Tags != null)
            {
                foreach (var tag in swagger.Tags)
                {
                    vm.Tags.Add(new RestApiTagViewModel
                    {
                        Name = tag.Name,
                        Description = tag.Description,
                        HtmlId = string.IsNullOrEmpty(tag.BookmarkId) ? GetHtmlId(tag.Name) : tag.BookmarkId, // Fall back to tag name's html id
                        Metadata = tag.Metadata,
                        Uid = GetUidForTag(uid, tag)
                    });
                }
            }
            if (swagger.Paths != null)
            {
                foreach (var path in swagger.Paths)
                {
                    var commonParameters = path.Value.Parameters;
                    foreach (var op in path.Value.Metadata)
                    {
                        // fetch operations from metadata
                        if (OperationNames.Contains(op.Key, StringComparer.OrdinalIgnoreCase))
                        {
                            var opJObject = op.Value as JObject;
                            if (opJObject == null)
                            {
                                throw new InvalidOperationException($"Value of {op.Key} should be JObject");
                            }

                            // convert operation from JObject to OperationObject
                            var operation = opJObject.ToObject<OperationObject>();
                            var parameters = GetParametersForOperation(operation.Parameters, commonParameters);
                            var itemUid = GetUidForOperation(uid, operation);
                            var itemVm = new RestApiChildItemViewModel
                            {
                                Path = path.Key,
                                OperationName = op.Key,
                                OperationId = operation.OperationId,
                                HtmlId = GetHtmlId(itemUid),
                                Uid = itemUid,
                                Metadata = operation.Metadata,
                                Description = operation.Description,
                                Summary = operation.Summary,
                                Parameters = parameters?.Select(s => new RestApiParameterViewModel
                                {
                                    Description = s.Description,
                                    Name = s.Name,
                                    Metadata = s.Metadata
                                }).ToList(),
                                Responses = operation.Responses?.Select(s => new RestApiResponseViewModel
                                {
                                    Metadata = s.Value.Metadata,
                                    Description = s.Value.Description,
                                    Summary = s.Value.Summary,
                                    HttpStatusCode = s.Key,
                                    Examples = s.Value.Examples?.Select(example => new RestApiResponseExampleViewModel
                                    {
                                        MimeType = example.Key,
                                        Content = example.Value != null ? JsonUtility.Serialize(example.Value) : null,
                                    }).ToList(),
                                }).ToList(),
                            };

                            // TODO: line number
                            object value;
                            if (swagger.Metadata.TryGetValue(Constants.PropertyName.Source, out value))
                            {
                                itemVm.Metadata[Constants.PropertyName.Source] = value;
                            }
                            else
                            {
                                itemVm.Metadata[Constants.PropertyName.Source] = null;
                            }
                            vm.Children.Add(itemVm);
                        }
                    }
                }
            }

            return vm;
        }
Exemplo n.º 2
0
 private static void CheckOperationId(SwaggerModel swagger, string fileName)
 {
     if (swagger.Paths != null)
     {
         foreach (var path in swagger.Paths)
         {
             foreach (var operation in path.Value.Metadata)
             {
                 JToken operationId;
                 var jObject = operation.Value as JObject;
                 if (jObject != null && !jObject.TryGetValue(OperationIdKey, out operationId))
                 {
                     throw new DocfxException($"{OperationIdKey} should exist in operation '{operation.Key}' of path '{path.Key}' for swagger file '{fileName}'");
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 private static string GetUid(SwaggerModel swagger)
 {
     return GenerateUid(swagger.Host, swagger.BasePath, swagger.Info.Title, swagger.Info.Version);
 }