public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            OperationFilter          = context.OperationFilter ?? Defaults.DefaultOperationFilter;
            OperationParameterFilter = context.OperationParameterFilter ?? Defaults.DefaultOperationParameterFilter;

            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var apiOp in ExtractApiOperations(context))
            {
                if (!OperationFilter(apiOp.JObject))
                {
                    continue;
                }

                NormalizationApiOperation op = ParseOperationData(apiOp);
                op.ApiNamespace   = context.ApiNamespace;
                op.ModelNamespace = context.ModelNamespace;

                op.Response.ApiNamespace   = context.ApiNamespace;
                op.Response.ModelNamespace = context.ModelNamespace;

                foreach (var param in op.Parameters)
                {
                    param.ApiNamespace   = context.ApiNamespace;
                    param.ModelNamespace = context.ModelNamespace;
                }

                ctx.NormalizationContext.Operations.Add(op);
            }

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
예제 #2
0
        public async Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            var basePath = context.ResourceListingUri;

            foreach (var path in ctx.ApiPathJsonMapping.Keys.ToList())
            {
                var apiUriBuilder = new UriBuilder(basePath);
                apiUriBuilder.Path += new Uri(path, UriKind.Relative);
                var json = await context.Loader(apiUriBuilder.Uri);

                if (string.IsNullOrWhiteSpace(json))
                {
                    throw new SwaseyException("Invalid JSON for api [{0}]: '{1}'", path, json);
                }

                var obj = JSON.DeserializeDynamic(json);
                if (obj == null)
                {
                    throw new SwaseyException("Unable to parse api definition JSON [{0}]: '{1}'", path, json);
                }

                ctx.ApiPathJsonMapping[path] = obj;
            }

            return(ctx);
        }
예제 #3
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalModel in context.NormalizationContext.Models)
            {
                var normalModelName = new NormalModelName(normalModel.ResourcePath, normalModel.Name);
                _modelNameMaps.Add(normalModelName);

                var modelDef = new ModelDefinition(normalModel.AsMetadata())
                {
                    ContextName  = normalModel.ResourceName,
                    Description  = normalModel.Description,
                    Name         = normalModel.Name.MapDataTypeName(),
                    ResourceName = normalModel.ResourcePath.ResourceNameFromPath()
                };
                normalModelName.New = modelDef.Name;

                modelDef.Properties.AddRange(ExtractModelProperties(normalModel.Properties));

                serviceDefinition.AddModel(modelDef);
            }

            var resourceModelLookup = _modelNameMaps.ToLookup(x => x.ResourcePath);

            // Ensure all Operations refer to the proper model names
            foreach (var op in context.NormalizationContext.Operations)
            {
                if (!resourceModelLookup.Contains(op.ResourcePath))
                {
                    continue;
                }

                var resourceModels = resourceModelLookup[op.ResourcePath].ToList();

                var returnItem = resourceModels.SingleOrDefault(x => op.Response.TypeName.Equals(x.Old));
                if (returnItem != null)
                {
                    op.Response.SetTypeName(returnItem.New);
                }

                foreach (var opParam in op.Parameters)
                {
                    var paramItem = resourceModels.SingleOrDefault(x => opParam.TypeName.Equals(x.Old));
                    if (paramItem != null)
                    {
                        opParam.SetTypeName(paramItem.New);
                    }
                }
            }


            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
예제 #4
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var modelObj in ExtractModels(context))
            {
                var modelType = (string)modelObj.Model.type;
                var isEnum    = "enum".Equals(modelType, StringComparison.InvariantCultureIgnoreCase);

                if (isEnum)
                {
                    var model = ParseEnumData(modelObj);
                    model.ApiNamespace   = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;
                    ctx.NormalizationContext.Enums.Add(model);
                }
                else
                {
                    var model = ParseModelData(modelObj);
                    model.ApiNamespace   = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;

                    foreach (var prop in model.Properties)
                    {
                        prop.ApiNamespace   = context.ApiNamespace;
                        prop.ModelNamespace = context.ModelNamespace;
                    }

                    ctx.NormalizationContext.Models.Add(model);
                }
            }

            foreach (var model in ctx.NormalizationContext.Models.Where(x => x.RawSubTypes.Any()))
            {
                foreach (var st in model.RawSubTypes)
                {
                    var sm = ctx.NormalizationContext.Models.FirstOrDefault(x => x.Name.Equals(st, StringComparison.InvariantCultureIgnoreCase));
                    if (sm == null)
                    {
                        continue;
                    }
                    model.SubTypes.Add(sm);
                }
            }

            var enumNames  = ctx.NormalizationContext.Enums.Select(x => x.Name).ToList();
            var modelNames = ctx.NormalizationContext.Models.Select(x => x.Name).ToList();

            // Ensure that Enum Properties are properly indicated
            ctx.NormalizationContext.Models
            .SelectMany(x => x.Properties)
            .Where(x => enumNames.Contains(x.TypeName) && !modelNames.Contains(x.TypeName))
            .ToList()
            .ForEach(x => x.IsEnum = true);

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
예제 #5
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalOp in context.NormalizationContext.Operations)
            {
                var op = new OperationDefinition20(NormalizeOperationPath(normalOp), normalOp.AsMetadata())
                {
                    ConsumesOctetStream = normalOp.SupportsStreamingUpload,
                    Description         = normalOp.Description,
                    HttpMethod          = normalOp.HttpMethod,
                    Name = ExtractName(normalOp),
                    ProducesOctetStream = normalOp.SupportsStreamingDownload,
                    ResourceName        = normalOp.ResourcePath,
                    Response            = NormalizeResponseDefinition(normalOp)
                };

                normalOp.Parameters
                .Select(NormalizeParameterDefinition)
                .ToList()
                .ForEach(x => op.AddParameter(x));

                serviceDefinition.AddOperation(op);
            }


            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
예제 #6
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            var enums = context.NormalizationContext
                        .Enums
                        .Select(CreateDefinition)
                        .ToLookup(x => x.Name);

            if (!enums.Any())
            {
                goto ReturnResult;
            }

            foreach (var e in enums)
            {
                var x = e.FirstOrDefault();
                if (x != null)
                {
                    serviceDefinition.AddEnum(x);
                }
            }


ReturnResult:
            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (!json.ContainsKey("swagger") == null || string.IsNullOrWhiteSpace((string)json.swagger))
            {
                throw new SwaseyException("swagger is required");
            }

            //swagger, info, info.version, info.title, path are required
            if (!json.ContainsKey("info") == null)
            {
                throw new SwaseyException("version is required");
            }

            if (!json.info.ContainsKey("version") == null || string.IsNullOrWhiteSpace((string)json.info.version))
            {
                throw new SwaseyException("version is required");
            }

            if (!json.info.ContainsKey("title") == null || string.IsNullOrWhiteSpace((string)json.info.title))
            {
                throw new SwaseyException("title is required");
            }

            if (!json.ContainsKey("paths") == null)
            {
                throw new SwaseyException("paths is required");
            }

            var ctx = new LifecycleContext(context)
            {
                State          = LifecycleState.Continue,
                SwaggerVersion = (string)json.swagger
            };

            ctx.ServiceMetadata = new ServiceMetadata(ctx.ServiceMetadata)
            {
                ApiVersion = (string)json.version
            };

            foreach (var path in json.paths)
            {
                ctx.ApiPathJsonMapping.Add((string)path.Key, path.Value);
            }

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (!json.ContainsKey("apiVersion") == null || string.IsNullOrWhiteSpace((string)json.apiVersion))
            {
                throw new SwaseyException("apiVersion is required");
            }

            if (!json.ContainsKey("swaggerVersion") == null || string.IsNullOrWhiteSpace((string)json.swaggerVersion))
            {
                throw new SwaseyException("swaggerVersion is required");
            }

            if (!json.ContainsKey("apis") == null)
            {
                throw new SwaseyException("apis is required");
            }

            var ctx = new LifecycleContext(context)
            {
                State          = LifecycleState.Continue,
                SwaggerVersion = (string)json.swaggerVersion
            };

            ctx.ServiceMetadata = new ServiceMetadata(ctx.ServiceMetadata)
            {
                ApiVersion = (string)json.apiVersion
            };

            foreach (var item in json.apis)
            {
                if (!item.ContainsKey("path") || string.IsNullOrWhiteSpace((string)item.path))
                {
                    throw new SwaseyException("api.path is required");
                }

                ctx.ApiPathJsonMapping.Add((string)item.path, null);
            }

            ctx.ApiPathJsonMapping.Add((string)json.paths, null);

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalOp in context.NormalizationContext.Operations)
            {
                var op = new OperationDefinition(NormalizeOperationPath(normalOp), normalOp.AsMetadata())
                {
                    ConsumesOctetStream = normalOp.SupportsStreamingUpload,
                    Description         = normalOp.Description,
                    HttpMethod          = normalOp.HttpMethod,
                    Name = ExtractName(normalOp),
                    ProducesOctetStream = normalOp.SupportsStreamingDownload,
                    ResourceName        = normalOp.ResourcePath.ResourceNameFromPath(),
                    Response            = NormalizeResponseDefinition(normalOp)
                };

                normalOp.Parameters
                .Select(NormalizeParameterDefinition)
                .ToList()
                .ForEach(x => op.AddParameter(x));

                if (serviceDefinition.Operations.Any(x => ((IOperationDefinition)x).ResourceName == op.ResourceName && ((IOperationDefinition)x).Name == op.Name))
                {
                    var pathParams = op.Parameters.Where(x => x.Type == ParameterType.Path && x.IsRequired).OrderBy(x => x.Name);
                    op.Name += string.Join("And", pathParams.Select(x => "By" + x.Name.UCFirst()));
                }

                serviceDefinition.AddOperation(op);
            }


            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }