示例#1
0
        public override IEnumerable<ComparisonMessage> Compare()
        {
            Logger.LogInfo(Resources.ParsingSwagger);
            if (string.IsNullOrWhiteSpace(Settings.Input) || string.IsNullOrWhiteSpace(Settings.Previous))
            {
                throw ErrorManager.CreateError(Resources.InputRequired);
            }

            var oldDefintion = SwaggerParser.Load(Settings.Previous, Settings.FileSystem);
            var newDefintion = SwaggerParser.Load(Settings.Input, Settings.FileSystem);

            var context = new ComparisonContext(oldDefintion, newDefintion);

            // Look for semantic errors and warnings in the new document.
            var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName);
            var validationMessages = validator.GetValidationExceptions(newDefintion).ToList();

            // Only compare versions if the new version is correct.
            var comparisonMessages =
                !validationMessages.Any(m => m.Severity > LogEntrySeverity.Error) ?
                newDefintion.Compare(context, oldDefintion) :
                Enumerable.Empty<ComparisonMessage>();

            return validationMessages
                .Select(msg =>
                    new ComparisonMessage(new MessageTemplate { Id = 0, Message = msg.Message }, string.Join("/", msg.Path), msg.Severity))
                .Concat(comparisonMessages);
        }
示例#2
0
        public override ServiceClient Build(out IEnumerable<ValidationMessage> messages)
        {
            Logger.LogInfo(Resources.ParsingSwagger);
            if (string.IsNullOrWhiteSpace(Settings.Input))
            {
                throw ErrorManager.CreateError(Resources.InputRequired);
            }
            ServiceDefinition = SwaggerParser.Load(Settings.Input, Settings.FileSystem);

            // Look for semantic errors and warnings in the document.
            var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName);
            messages = validator.GetValidationExceptions(ServiceDefinition).ToList();

            Logger.LogInfo(Resources.GeneratingClient);
            // Update settings
            UpdateSettings();

            InitializeClientModel();
            BuildCompositeTypes();

            // Build client parameters
            foreach (var swaggerParameter in ServiceDefinition.Parameters.Values)
            {
                var parameter = ((ParameterBuilder)swaggerParameter.GetBuilder(this)).Build();

                var clientProperty = new Property();
                clientProperty.LoadFrom(parameter);

                ServiceClient.Properties.Add(clientProperty);
            }

            // Build methods
            foreach (var path in ServiceDefinition.Paths.Concat(ServiceDefinition.CustomPaths))
            {
                foreach (var verb in path.Value.Keys)
                {
                    var operation = path.Value[verb];
                    if (string.IsNullOrWhiteSpace(operation.OperationId))
                    {
                        throw ErrorManager.CreateError(
                            string.Format(CultureInfo.InvariantCulture,
                                Resources.OperationIdMissing,
                                verb,
                                path.Key));
                    }
                    var methodName = GetMethodName(operation);
                    var methodGroup = GetMethodGroup(operation);

                    if (verb.ToHttpMethod() != HttpMethod.Options)
                    {
                        string url = path.Key;
                        if (url.Contains("?"))
                        {
                            url = url.Substring(0, url.IndexOf('?'));
                        }
                        var method = BuildMethod(verb.ToHttpMethod(), url, methodName, operation);
                        method.Group = methodGroup;
                        ServiceClient.Methods.Add(method);
                        if (method.DefaultResponse.Body is CompositeType)
                        {
                            ServiceClient.ErrorTypes.Add((CompositeType)method.DefaultResponse.Body);
                        }
                    }
                    else
                    {
                        Logger.LogWarning(Resources.OptionsNotSupported);
                    }
                }
            }

            // Set base type
            foreach (var typeName in GeneratedTypes.Keys)
            {
                var objectType = GeneratedTypes[typeName];
                if (ExtendedTypes.ContainsKey(typeName))
                {
                    objectType.BaseModelType = GeneratedTypes[ExtendedTypes[typeName]];
                }

                ServiceClient.ModelTypes.Add(objectType);
            }

            return ServiceClient;
        }