public void SimpleInterface() { var body = new Source( @" interface Hello { world: String }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new InterfaceDefinition { Name = new Name("Hello", new Location(11, 16, body)), Fields = ImmutableArray.Create( new FieldDefinition { Name = new Name("world", new Location(21, 26, body)), Type = new NamedType("String", 28, 34, body), Location = new Location(21, 34, body), }), Location = new Location(1, 36, body), }), Location = new Location(1, 36, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleNonNullType() { var body = new Source( @" type Hello { world: String! }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new TypeDefinition { Name = new Name("Hello", new Location(6, 11, body)), Fields = ImmutableArray.Create( new FieldDefinition { Name = new Name("world", new Location(16, 21, body)), Type = new NonNullType { Type = new NamedType("String", 23, 29, body), Location = new Location(23, 30, body), }, Location = new Location(16, 30, body), }), Location = new Location(1, 32, body), }), Location = new Location(1, 32, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleInputObject() { var body = new Source( @" input Hello { world: String }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new InputObjectDefinition { Name = new Name("Hello", new Location(7, 12, body)), Fields = ImmutableArray.Create( new InputValueDefinition { Name = new Name("world", new Location(17, 22, body)), Type = new NamedType("String", 24, 30, body), Location = new Location(17, 30, body), }), Location = new Location(1, 32, body), }), Location = new Location(1, 32, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleFieldWithArg() { var body = new Source( @" type Hello { world(flag: Boolean): String }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new TypeDefinition { Name = new Name("Hello", new Location(6, 11, body)), Fields = ImmutableArray.Create( new FieldDefinition { Name = new Name("world", new Location(16, 21, body)), Type = new NamedType("String", 38, 44, body), Arguments = ImmutableArray.Create( new InputValueDefinition { Name = new Name("flag", new Location(22, 26, body)), Type = new NamedType("Boolean", 28, 35, body), Location = new Location(22, 35, body), }), Location = new Location(16, 44, body), }), Location = new Location(1, 46, body), }), Location = new Location(1, 46, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void DoesNotAlterAst() { var schemaKitchenSink = TestUtils.SchemaKitchenSink.Value; var ast = SchemaParser.ParseSchema(schemaKitchenSink); var astCopy = SchemaParser.ParseSchema(schemaKitchenSink); var schemaPrinter = new SchemaPrinter(); schemaPrinter.VisitSchemaDocument(ast); ast.ShouldBeEquivalentToDeepDynamic(astCopy); }
public void SchemaWithRegularArrayOfDoublesProperty() { string testingPropertyName = "testKey"; string testingPropertyType = "array"; string testingPropertyFormat = null; string testingArrayItemType = "double"; string testingArrayItemFormat = "number"; Dictionary <string, OpenApiMediaType> content = new Dictionary <string, OpenApiMediaType> { { "application/json", new OpenApiMediaType { Schema = new OpenApiSchema { Properties = new Dictionary <string, OpenApiSchema> { { testingPropertyName, new OpenApiSchema { Type = testingPropertyType, Title = testingPropertyName, Items = new OpenApiSchema { Type = testingArrayItemType, Format = testingArrayItemFormat }, Format = testingPropertyFormat } } } } } } }; Dictionary <string, object> parsedSchema = SchemaParser.ParseSchema(content); Dictionary <string, object> testingProperty = (Dictionary <string, object>)parsedSchema.First().Value; Dictionary <string, object> arrayTypeDictionary = (Dictionary <string, object>)testingProperty["ArrayItemSchema"]; Assert.AreEqual(testingPropertyName, parsedSchema.First().Key); Assert.That(arrayTypeDictionary.ContainsKey("Type")); Assert.That(arrayTypeDictionary.ContainsKey("Format")); Assert.AreEqual(testingArrayItemType, arrayTypeDictionary["Type"]); Assert.AreEqual(testingArrayItemFormat, arrayTypeDictionary["Format"]); }
public void Scalar() { var body = new Source("scalar Hello"); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new ScalarDefinition { Name = new Name("Hello", new Location(7, 12, body)), Location = new Location(0, 12, body), }), Location = new Location(0, 12, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SchemaWithRegularProperties() { string testingPropertyName = "testKey"; string testingPropertyType = "string"; string testingPropertyExample = "test"; string testingPropertyFormat = null; Dictionary <string, OpenApiMediaType> content = new Dictionary <string, OpenApiMediaType> { { "application/json", new OpenApiMediaType { Schema = new OpenApiSchema { Properties = new Dictionary <string, OpenApiSchema> { { testingPropertyName, new OpenApiSchema { Type = testingPropertyType, Example = new OpenApiString(testingPropertyExample), Title = testingPropertyName, Format = testingPropertyFormat } } } } } } }; Dictionary <string, object> parsedSchema = SchemaParser.ParseSchema(content); Dictionary <string, object> testingProperty = (Dictionary <string, object>)parsedSchema.First().Value; Assert.AreEqual(testingPropertyName, parsedSchema.First().Key); Assert.That(testingProperty.ContainsKey("Title")); Assert.That(testingProperty.ContainsKey("Type")); Assert.That(testingProperty.ContainsKey("Format")); Assert.That(testingProperty.ContainsKey("Example")); Assert.AreEqual(testingPropertyName, testingProperty["Title"]); Assert.AreEqual(testingPropertyType, testingProperty["Type"]); Assert.AreEqual(testingPropertyFormat, testingProperty["Format"]); Assert.AreEqual(testingPropertyExample, testingProperty["Example"]); }
public void SimpleTypeInheritingInterface() { var body = new Source("type Hello implements World { }"); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new TypeDefinition { Name = new Name("Hello", new Location(5, 10, body)), Interfaces = ImmutableArray.Create(new NamedType("World", 22, 27, body)), Location = new Location(0, 31, body), }), Location = new Location(0, 31, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleUnion() { var body = new Source("union Hello = World"); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new UnionDefinition { Name = new Name("Hello", new Location(6, 11, body)), Types = ImmutableArray.Create( new NamedType("World", 14, 19, body)), Location = new Location(0, 19, body), }), Location = new Location(0, 19, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleFieldWithTwoArgs() { var body = new Source( @" type Hello { world(argOne: Boolean, argTwo: Int): String }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new TypeDefinition { Name = new Name("Hello", new Location(6, 11, body)), Fields = ImmutableArray.Create( new FieldDefinition { Name = new Name("world", new Location(16, 21, body)), Type = new NamedType("String", 53, 59, body), Arguments = ImmutableArray.Create( new InputValueDefinition { Name = new Name("argOne", new Location(22, 28, body)), Type = new NamedType("Boolean", 30, 37, body), DefaultValue = null, Location = new Location(22, 37, body), }, new InputValueDefinition { Name = new Name("argTwo", new Location(39, 45, body)), Type = new NamedType("Int", 47, 50, body), DefaultValue = null, Location = new Location(39, 50, body), }), Location = new Location(16, 59, body), }), Location = new Location(1, 61, body), }), Location = new Location(1, 61, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SingleValueEnum() { var body = new Source("enum Hello { WORLD }"); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new EnumDefinition { Name = new Name("Hello", new Location(5, 10, body)), Values = ImmutableArray.Create( new EnumValueDefinition("WORLD", new Location(13, 18, body))), Location = new Location(0, 20, body), }), Location = new Location(0, 20, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void SimpleFieldWithListArg() { var body = new Source( @" type Hello { world(things: [String]): String }".ToLF()); var doc = SchemaParser.ParseSchema(body); var expected = new SchemaDocument { Definitions = ImmutableArray.Create <SchemaDefinition>( new TypeDefinition { Name = new Name("Hello", new Location(6, 11, body)), Fields = ImmutableArray.Create( new FieldDefinition { Name = new Name("world", new Location(16, 21, body)), Type = new NamedType("String", 41, 47, body), Arguments = ImmutableArray.Create( new InputValueDefinition { Name = new Name("things", new Location(22, 28, body)), Type = new ListType { Type = new NamedType("String", 31, 37, body), Location = new Location(30, 38, body), }, DefaultValue = null, Location = new Location(22, 38, body), }), Location = new Location(16, 47, body), }), Location = new Location(1, 49, body), }), Location = new Location(1, 49, body), }; doc.ShouldBeEquivalentToDeepDynamic(expected); }
public void PrintsKitchenSink() { var schemaKitchenSink = TestUtils.SchemaKitchenSink.Value; var ast = SchemaParser.ParseSchema(schemaKitchenSink); var schemaPrinter = new SchemaPrinter(); var printed = schemaPrinter.VisitSchemaDocument(ast); printed.Should().Be( @"type Foo implements Bar { one: Type two(argument: InputType!): Type three(argument: InputType, other: String): Int four(argument: String = ""string""): String five(argument: [String] = [""string"", ""string""]): String six(argument: InputType = {key: ""value""}): Type } interface Bar { one: Type four(argument: String = ""string""): String } union Feed = Story | Article | Advert scalar CustomScalar enum Site { DESKTOP MOBILE } input InputType { key: String! answer: Int = 42 } ".ToLF()); }
public IActionResult Index() { if (HybridSupport.IsElectronActive) { Electron.IpcMain.On("select-file-path", async(args) => { var mainWindow = Electron.WindowManager.BrowserWindows.First(); var options = new OpenDialogOptions { Properties = new OpenDialogProperty[] { OpenDialogProperty.openFile, } }; string[] files = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options); Electron.IpcMain.Send(mainWindow, "select-file-path-reply", files); }); Electron.IpcMain.On("select-schema-path", async(args) => { var mainWindow = Electron.WindowManager.BrowserWindows.First(); var options = new OpenDialogOptions { Properties = new OpenDialogProperty[] { OpenDialogProperty.openFile }, Filters = new FileFilter[] { new FileFilter { Name = "Json schema", Extensions = new string[] { "json" } } } }; string[] files = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options); Electron.IpcMain.Send(mainWindow, "select-schema-path-reply", files); }); Electron.IpcMain.On("validate-file-btn", (args) => { var mainWindow = Electron.WindowManager.BrowserWindows.First(); try { args = args.ToString().Replace(@"\", @"\\"); var arguments = JsonConvert.DeserializeObject <FileDefinition>(args.ToString()); var filePath = arguments.FilePath; var schemaPath = arguments.SchemaFilePath; var fileType = arguments.FileType; if (!System.IO.File.Exists(filePath)) { Electron.IpcMain.Send(mainWindow, "results", $"<div class='alert alert-danger' role='alert'>Invalid file {filePath}</div>"); return; } if (!System.IO.File.Exists(schemaPath)) { Electron.IpcMain.Send(mainWindow, "results", $"<div class='alert alert-danger' role='alert'>Invalid file {schemaPath}</div>"); return; } var schemaData = string.Empty; var schemaParser = new SchemaParser(); try { using (var sr = new StreamReader(System.IO.File.OpenRead(schemaPath))) { schemaData = sr.ReadToEnd(); } } catch (Exception ex) { var errorMsg = String.Format(null, SharedResources.SchemaError, ex.Message); Console.WriteLine($"{errorMsg}"); } if (string.IsNullOrEmpty(schemaData)) { Electron.IpcMain.Send(mainWindow, "results", "<div class='alert alert-danger' role='alert'>Schema file is empty.</div>"); return; } var parsedSchema = schemaParser.ParseSchema(schemaData); var schemaValidator = new FluentSchemaValidator(); var schemaValidationResults = schemaValidator.Validate(parsedSchema); if (!schemaValidationResults.IsValid && schemaValidationResults.Errors.Any()) { var results = _viewRenderService.RenderToStringAsync("Partial/_SchemaFileValidationResult", schemaValidationResults.Errors); if (results.IsCompletedSuccessfully && !results.IsFaulted) { Electron.IpcMain.Send(mainWindow, "results", results.Result); return; } else { Electron.IpcMain.Send(mainWindow, "results", "<div class='alert alert-danger' role='alert'>An Error has occurred while processing the file. Please contact your administrator.</div>"); return; } } if (parsedSchema.FileFormat.ToLower() == "fixed") { var validator = new FixedLengthFileValidator(filePath, schemaPath, parsedSchema); var result = validator.ValidateFile(); result.Results = result.Results.Take(50).ToList(); var results = _viewRenderService.RenderToStringAsync("Partial/_FixedLengthFileValidationResult", result); if (results.IsCompletedSuccessfully && !results.IsFaulted) { Electron.IpcMain.Send(mainWindow, "results", results.Result); return; } else { Electron.IpcMain.Send(mainWindow, "results", "<div class='alert alert-danger' role='alert'>An Error has occurred while processing the file. Please contact your administrator.</div>"); return; } } else if (parsedSchema.FileFormat.ToLower() == "separated") { var validator = new SeparatedFileValidator(filePath, schemaPath, parsedSchema); var result = validator.ValidateFile(); result.Results = result.Results.Take(50).ToList(); var results = _viewRenderService.RenderToStringAsync("Partial/_SeparatedFileValidationResult", result); if (results.IsCompletedSuccessfully && !results.IsFaulted) { Electron.IpcMain.Send(mainWindow, "results", results.Result); return; } else { Electron.IpcMain.Send(mainWindow, "results", "<div class='alert alert-danger' role='alert'>An Error has occurred while processing the file. Please contact your administrator.</div>"); return; } } } catch (Exception ex) { Electron.IpcMain.Send(mainWindow, "results", $"<div class='alert alert-danger' role='alert'>{ex.Message.ToString()}</div>"); return; } }); Electron.IpcMain.On("create-html-file", async(args) => { var mainWindow = Electron.WindowManager.BrowserWindows.First(); var options = new OpenDialogOptions { Properties = new OpenDialogProperty[] { OpenDialogProperty.openDirectory, } }; string[] directories = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options); var directory = directories[0]; var pathString = Path.Combine(directory, "Validation_Result.html"); var file = System.IO.File.Create(pathString); file.Close(); Electron.IpcMain.Send(mainWindow, "created-html-file-reply", pathString); }); } return(View()); }
public static void ParseSchemaErr(string source) { Action action = () => SchemaParser.ParseSchema(source); action.ShouldThrow <SyntaxError>(); }
public void SchemaWithAdditionalProperties() { string testingPropertyName = "testKey"; string testingPropertyType = "boolean"; bool testingPropertyExample = true; string testingPropertyFormat = null; Dictionary <string, OpenApiMediaType> content = new Dictionary <string, OpenApiMediaType> { { "application/json", new OpenApiMediaType { Schema = new OpenApiSchema { AdditionalPropertiesAllowed = true, AdditionalProperties = new OpenApiSchema { Properties = new Dictionary <string, OpenApiSchema> { { testingPropertyName, new OpenApiSchema { Type = testingPropertyType, Example = new OpenApiBoolean(testingPropertyExample), Title = testingPropertyName, Format = testingPropertyFormat } } } } } } } }; Dictionary <string, object> firstAdditionalPropertyDictionary = (Dictionary <string, object>)SchemaParser.ParseSchema(content).First().Value; Dictionary <string, object> firstAdditionalPropertyItemDictionary = (Dictionary <string, object>)firstAdditionalPropertyDictionary.First().Value; Assert.AreEqual(testingPropertyName, firstAdditionalPropertyDictionary.First().Key); Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Title")); Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Type")); Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Format")); Assert.That(firstAdditionalPropertyItemDictionary.ContainsKey("Example")); Assert.AreEqual(testingPropertyName, firstAdditionalPropertyItemDictionary["Title"]); Assert.AreEqual(testingPropertyType, firstAdditionalPropertyItemDictionary["Type"]); Assert.AreEqual(testingPropertyFormat, firstAdditionalPropertyItemDictionary["Format"]); Assert.AreEqual(testingPropertyExample.ToString(), firstAdditionalPropertyItemDictionary["Example"]); }
public void ParsingNotSupportedContentTypeShouldReturnNull(string contentType) { Assert.IsNull(SchemaParser.ParseSchema(new Dictionary <string, OpenApiMediaType> { { contentType, null } })); }