private void ProcessModels(ApiDeclaration apiDeclaration, GenerationContext dataContext)
        {
            foreach (KeyValuePair <string, ModelObject> modelObject in apiDeclaration.Models)
            {
                if (!dataContext.DataModels.ContainsKey(modelObject.Key))
                {
                    DataModelContext dataModelContext = new DataModelContext
                    {
                        ID = modelObject.Value.ID
                    };

                    dataModelContext.SubTypes.AddRange(modelObject.Value.SubTypes);

                    foreach (KeyValuePair <string, PropertyObject> propertyObject in modelObject.Value.Properties)
                    {
                        ParameterContext parameter = new ParameterContext {
                            Name = propertyObject.Key
                        };
                        InitParameterFromDataTypeObject(parameter, propertyObject.Value);

                        dataModelContext.Propeties.Add(propertyObject.Key, parameter);
                    }

                    dataContext.DataModels.Add(modelObject.Key, dataModelContext);
                }
            }
        }
Exemplo n.º 2
0
        private static void ApiSpec(ApiDeclaration declaration, string apiPath, Action <ApiSpec> applyAssertions)
        {
            var apiSpec = declaration.Apis
                          .Single(api => api.Path == apiPath);

            applyAssertions(apiSpec);
        }
Exemplo n.º 3
0
        private static void Api(ApiDeclaration declaration, string apiPath, Action <Api> applyAssertions)
        {
            var api = declaration.Apis
                      .Single(a => a.Path == apiPath);

            applyAssertions(api);
        }
        private void ProcessOperations(ApiDeclaration apiDeclaration, GenerationContext dataContext)
        {
            OperationGroupContext operationGroupContext = new OperationGroupContext {
                Name = apiDeclaration.ResourcePath.NormalizeName()
            };

            foreach (ApiObject apiObject in apiDeclaration.Apis)
            {
                string operationPath = apiObject.Path;

                foreach (OperationObject operation in apiObject.Operations)
                {
                    OperationContext operationContext = new OperationContext
                    {
                        RelativeUrl = $"{apiDeclaration.BasePath}{operationPath}",
                        Method      = operation.Method.ToString(),
                        Name        = operation.NickName
                    };

                    InitParameterFromDataTypeObject(operationContext, operation);

                    foreach (ParameterObject parameterObject in operation.Parameters)
                    {
                        ParameterContext parameter = new ParameterContext
                        {
                            Name = parameterObject.Name
                        };

                        InitParameterFromDataTypeObject(parameter, parameterObject);

                        switch (parameterObject.ParamType)
                        {
                        case ParamType.Path:
                            operationContext.PathParameters.Add(parameter);
                            break;

                        case ParamType.Query:
                            operationContext.QueryParameters.Add(parameter);
                            break;

                        case ParamType.Body:
                            operationContext.BodyParameter = parameter;
                            break;

                        case ParamType.Header:
                        case ParamType.Form:
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }

                    operationGroupContext.Operations.Add(operationContext);
                }
            }

            dataContext.Groups.Add(operationGroupContext);
        }
Exemplo n.º 5
0
        private static string GetSubType(ApiDeclaration swaggerResource, string subTypeName)
        {
            var subType =
                swaggerResource.Models.SingleOrDefault(
                    x => x.Value.SubTypes != null && x.Value.SubTypes.Contains(subTypeName)).Key;

            return(subType);
        }
        private async Task ProcessResource(Resource resource, GenerationContext commonContext)
        {
            Uri uri = new Uri(context.Url);

            // TODO: refactor this
            string relativeUrl       = resource.Path.Replace("{format}", context.Format.ToString().ToLower()).Replace("/", "");
            string apiDeclarationUrl = $"http://{uri.Authority}{String.Join("", uri.Segments.Take(uri.Segments.Length - 1))}{relativeUrl}";

            ApiDeclaration apiDeclaration = await communicationService.SendRequestAsync <ApiDeclaration>(apiDeclarationUrl);

            Task processOperationTask = Task.Run(() => ProcessOperations(apiDeclaration, commonContext));
            Task processModelTask     = Task.Run(() => ProcessModels(apiDeclaration, commonContext));

            await Task.WhenAll(processOperationTask, processModelTask);
        }
Exemplo n.º 7
0
        private static EndpointModel CreateEndpoint(ApiDeclaration apiDeclaration, SwaggerModel.ResourceListing.Api resourceListingApi)
        {
            var types = apiDeclaration.models.Values.Select(model => new TypeModel
            {
                Name = model.id,
            }).ToList();

            types.ForEach((type) =>
            {
                var model       = apiDeclaration.models[type.Name];
                type.Properties = model.properties.Select(kvp =>
                {
                    var name       = kvp.Key;
                    var property   = kvp.Value;
                    var typeIsList = property.type == "array";
                    return(new PropertyModel
                    {
                        Name = name,
                        Description = property.description,
                        TypeIsList = typeIsList,
                        Type = typeIsList
                                    ? types.FirstOrDefault(t => t.Name == property.items._ref) ?? GetPrimitiveType(property.items.type, property.items.format)
                                    : types.FirstOrDefault(t => t.Name == property._ref) ?? GetPrimitiveType(property.type, property.format),
                        IsRequired = model.required != null && model.required.Contains(name)
                    });
                }).OrderBy(p => p.Name).ToList();
            });

            return(new EndpointModel
            {
                Description = resourceListingApi.description,
                Name = apiDeclaration.resourcePath.ToPascalCase(),
                BasePath = apiDeclaration.basePath,
                ResourcePath = apiDeclaration.resourcePath,
                Routes = apiDeclaration.apis.SelectMany(
                    api => api.operations.Select(
                        o => CreateRouteModel(o, api, types)
                        )
                    ).OrderBy(r => r.Name).ToList(),
                Types = types
            });
        }
        public ApiDeclaration GetApiDeclaration(string resourcePath)
        {
            var routeData = RetrieveSwaggerRouteData()
                            .Where(d => d.ResourcePath == resourcePath)
                            .ToList();

            var apiDeclaration = new ApiDeclaration
            {
                BasePath = new Uri("/", UriKind.Relative),
                Apis     = routeData.GroupBy(d => d.ApiPath).Select(GetApi).OrderBy(api => api.Path)
            };

            var modelsData      = RetrieveSwaggerModelData();
            var modelsForRoutes = GetModelsForRoutes(routeData, modelsData);

            apiDeclaration.Models = modelsForRoutes.SelectMany(m => m.ToModel(modelsData))
                                    .OrderBy(m => m.Id)
                                    .ToDictionary(m => m.Id, m => m);

            return(apiDeclaration);
        }
Exemplo n.º 9
0
        public ApiDeclaration GetApiDeclaration(string resourcePath, string sBasePath)
        {
            var routeData = _metadataProvider.RetrieveSwaggerRouteData()
                            .Where(d => d.ResourcePath == resourcePath)
                            .ToList();

            var apiDeclaration = new ApiDeclaration
            {
                //BasePath = new Uri("/test", UriKind.Relative),//PetrK, we need something like this!
                BasePath = new Uri(sBasePath, UriKind.Relative),
                Apis     = routeData.GroupBy(d => d.ApiPath).Select(GetApi).OrderBy(api => api.Path)
            };

            var modelsData      = _metadataProvider.RetrieveSwaggerModelData();
            var modelsForRoutes = GetModelsForRoutes(routeData, modelsData);

            apiDeclaration.Models = modelsForRoutes.SelectMany(m => m.ToModel(modelsData))
                                    .GroupBy(m => m.Id)
                                    .Select(g => g.First())
                                    .OrderBy(m => m.Id)
                                    .ToDictionary(m => m.Id, m => m);

            return(apiDeclaration);
        }
Exemplo n.º 10
0
        private static void Model(ApiDeclaration declaration, string id, Action <ModelSpec> applyAssertions)
        {
            var modelSpec = declaration.Models[id];

            applyAssertions(modelSpec);
        }
Exemplo n.º 11
0
        private static void Model(ApiDeclaration declaration, string id, Action <DataType> applyAssertions)
        {
            var model = declaration.Models[id];

            applyAssertions(model);
        }
Exemplo n.º 12
0
 public SwaggerConverter(ApiDeclaration root) : base(root)
 {
 }
Exemplo n.º 13
0
 public static string GetResourceName(this ApiDeclaration apiDeclaration)
 {
     return(apiDeclaration.resourcePath.Replace("/", " ").ToPascalCase().Trim());
 }
Exemplo n.º 14
0
 protected BaseSwaggerConverter(ApiDeclaration api)
 {
     Root = api;
 }