Пример #1
0
        public static RestApiItemViewModel FromSwaggerModel(Swagger.SwaggerModel swagger)
        {
            var uid = GetUid(swagger);
            var vm  = new RestApiItemViewModel
            {
                Name        = swagger.Info.Title,
                Uid         = uid,
                HtmlId      = GetHtmlId(uid),
                Metadata    = swagger.Metadata,
                Description = swagger.Description,
                Summary     = swagger.Summary,
                Children    = new List <RestApiItemViewModel>(),
                Raw         = swagger.Raw
            };

            foreach (var path in swagger.Paths)
            {
                foreach (var op in path.Value)
                {
                    var itemUid = GetUidForOperation(uid, op.Value);
                    var itemVm  = new RestApiItemViewModel
                    {
                        Path          = path.Key,
                        OperationName = op.Key,
                        OperationId   = op.Value.OperationId,
                        HtmlId        = GetHtmlId(itemUid),
                        Uid           = itemUid,
                        Metadata      = op.Value.Metadata,
                        Description   = op.Value.Description,
                        Summary       = op.Value.Summary,
                        Parameters    = op.Value.Parameters?.Select(s => new RestApiParameterViewModel
                        {
                            Description = s.Description,
                            Metadata    = s.Metadata
                        }).ToList(),
                        Responses = op.Value.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
                    itemVm.Metadata[Constants.PropertyName.Source] = swagger.Metadata[Constants.PropertyName.Source];
                    vm.Children.Add(itemVm);
                }
            }

            return(vm);
        }
Пример #2
0
 private static string GetUid(Swagger.SwaggerModel swagger)
 {
     return(GenerateUid(swagger.Host, swagger.BasePath, swagger.Info.Title));
 }
Пример #3
0
        public static RestApiRootItemViewModel FromSwaggerModel(Swagger.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)
                    });
                }
            }
            foreach (var path in swagger.Paths)
            {
                foreach (var op in path.Value)
                {
                    var itemUid = GetUidForOperation(uid, op.Value);
                    var itemVm  = new RestApiChildItemViewModel
                    {
                        Path          = path.Key,
                        OperationName = op.Key,
                        OperationId   = op.Value.OperationId,
                        HtmlId        = GetHtmlId(itemUid),
                        Uid           = itemUid,
                        Metadata      = op.Value.Metadata,
                        Description   = op.Value.Description,
                        Summary       = op.Value.Summary,
                        Parameters    = op.Value.Parameters?.Select(s => new RestApiParameterViewModel
                        {
                            Description = s.Description,
                            Metadata    = s.Metadata
                        }).ToList(),
                        Responses = op.Value.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
                    itemVm.Metadata[Constants.PropertyName.Source] = swagger.Metadata[Constants.PropertyName.Source];
                    vm.Children.Add(itemVm);
                }
            }

            return(vm);
        }