private IEnumerable <ValidationMessage> ValidateOpenAPISpec(string input, ServiceDefinitionMetadata metadata) { var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); var serviceDefinition = SwaggerParser.Parse(input, File.ReadAllText(input)); return(validator.GetValidationExceptions(new Uri(input, UriKind.RelativeOrAbsolute), serviceDefinition, metadata).OfType <ValidationMessage>()); }
protected override async Task <bool> ProcessInternal() { var files = await ListInputs(); if (files.Length != 1) { return(false); } var content = await ReadFile(files[0]); var fs = new MemoryFileSystem(); fs.WriteAllText(files[0], content); var serviceDefinition = SwaggerParser.Load(files[0], fs); var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); foreach (ValidationMessage validationEx in validator.GetValidationExceptions(new Uri(files[0], UriKind.RelativeOrAbsolute), serviceDefinition)) { LogValidationMessage(validationEx); } return(true); }
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)); }
public async Task <bool> Process() { try { using (Start) { AddMessageListener(); var files = await ListInputs(); if (files.Length != 1) { return(false); } var content = await ReadFile(files[0]); var fs = new MemoryFileSystem(); fs.VirtualStore.Add(files[0], new StringBuilder(content)); var serviceDefinition = SwaggerParser.Load(files[0], fs); var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); foreach (var validationEx in validator.GetValidationExceptions(new Uri(files[0], UriKind.RelativeOrAbsolute), serviceDefinition)) { Logger.Instance.Log(validationEx); } } } catch { return(false); // replace by more verbose feedback } return(true); }
public override IEnumerable <LogMessage> Compare() { var settings = Settings.Instance; Logger.Instance.Log(Category.Info, Resources.ParsingSwagger); if (string.IsNullOrWhiteSpace(Settings.Input) || string.IsNullOrWhiteSpace(Settings.Previous)) { throw ErrorManager.CreateError(Resources.InputRequired); } var oldDefintion = SwaggerParser.Load(settings.Previous, settings.FileSystemInput); var newDefintion = SwaggerParser.Load(settings.Input, settings.FileSystemInput); var context = new ComparisonContext(oldDefintion, newDefintion); // Look for semantic errors and warnings in the new document. var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); var LogMessages = validator.GetValidationExceptions(newDefintion.FilePath, newDefintion, new ServiceDefinitionMetadata { ServiceDefinitionDocumentType = ServiceDefinitionDocumentType.ARM, MergeState = ServiceDefinitionDocumentState.Composed }).ToList(); // Only compare versions if the new version is correct. var comparisonMessages = !LogMessages.Any(m => m.Severity > Category.Error) ? newDefintion.Compare(context, oldDefintion) : Enumerable.Empty <ComparisonMessage>(); return(LogMessages .Select(msg => new ComparisonMessage(new MessageTemplate { Id = 0, Message = msg.Message }, msg.Path, msg.Severity)) .Concat(comparisonMessages)); }
private IEnumerable <ValidationMessage> ValidateSwagger(string input, ServiceDefinitionMetadata metadata) { // Most rules are to be applied for ARM documents // Also, most rules need to be run over the composite document (i.e. AFTER merge state) // hence the defaults using (NewContext) { var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); var serviceDefinition = SwaggerParser.Parse(input, File.ReadAllText(input)); return(validator.GetValidationExceptions(new Uri(input, UriKind.RelativeOrAbsolute), serviceDefinition, metadata).OfType <ValidationMessage>()); } }
protected override async Task <bool> ProcessInternal() { var docStateInput = await GetValue <string>("merge-state"); ServiceDefinitionDocumentState docState; if (!Enum.TryParse <ServiceDefinitionDocumentState>(docStateInput, true, out docState)) { throw new Exception("Invalid Input for merge-state: " + docStateInput + ". Valid values are 'individual' and 'composed'."); } if ((await GetValue <bool>("azure-arm-validator.debugger")) || (docState == ServiceDefinitionDocumentState.Composed && await GetValue <bool>("azure-arm-validator.composed-debugger")) || (docState == ServiceDefinitionDocumentState.Individual && await GetValue <bool>("azure-arm-validator.individual-debugger"))) { Debugger.Await(); } var files = await ListInputs(); foreach (var file in files) { var content = await ReadFile(file); var fs = new MemoryFileSystem(); fs.WriteAllText(file, content); var serviceDefinition = SwaggerParser.Load(file, fs); var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); var docTypeInput = (await GetValue <string>("openapi-type")); ServiceDefinitionDocumentType docType; // Convert data-plane to dataplane if (!Enum.TryParse <ServiceDefinitionDocumentType>(docTypeInput.Replace("-", ""), true, out docType)) { throw new Exception("Invalid Input for openapi-type: " + docTypeInput + ". Valid values are 'arm', 'data-plane' or 'default'."); } var metadata = new ServiceDefinitionMetadata { ServiceDefinitionDocumentType = docType, MergeState = docState }; foreach (ValidationMessage validationEx in validator.GetValidationExceptions(new Uri(file, UriKind.RelativeOrAbsolute), serviceDefinition, metadata)) { LogValidationMessage(validationEx); } } return(true); }
public override CodeModel 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); CodeModel.Add(clientProperty); } var methods = new List <Method>(); // 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; methods.Add(method); if (method.DefaultResponse.Body is CompositeType) { CodeModel.AddError((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]]; } CodeModel.Add(objectType); } CodeModel.AddRange(methods); return(CodeModel); }
public CodeModel Build(ServiceDefinition serviceDefinition) { ServiceDefinition = serviceDefinition; if (Settings.Instance.CodeGenerator.EqualsIgnoreCase("None")) { // Look for semantic errors and warnings in the document. var validator = new RecursiveObjectValidator(PropertyNameResolver.JsonName); foreach (var validationEx in validator.GetValidationExceptions(ServiceDefinition.FilePath, ServiceDefinition, new ServiceDefinitionMetadata { // LEGACY MODE! set defaults for the metadata, marked to be deprecated ServiceDefinitionDocumentType = ServiceDefinitionDocumentType.ARM, MergeState = ServiceDefinitionDocumentState.Composed })) { Logger.Instance.Log(validationEx); } return(New <CodeModel>()); } Logger.Instance.Log(Category.Info, 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); clientProperty.RealPath = new string[] { parameter.SerializedName.Value }; CodeModel.Add(clientProperty); } var methods = new List <Method>(); // 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; methods.Add(method); if (method.DefaultResponse.Body is CompositeType) { CodeModel.AddError((CompositeType)method.DefaultResponse.Body); } } else { Logger.Instance.Log(Category.Warning, Resources.OptionsNotSupported); } } } // Set base type foreach (var typeName in GeneratedTypes.Keys) { var objectType = GeneratedTypes[typeName]; if (ExtendedTypes.ContainsKey(typeName)) { objectType.BaseModelType = GeneratedTypes[ExtendedTypes[typeName]]; } CodeModel.Add(objectType); } CodeModel.AddRange(methods); return(CodeModel); }