public void should_parse_array_in_type_object()
        {
            const string schema = @"{
        '$schema': 'http://json-schema.org/draft-03/schema#',
        'type': 'object',
        'properties': {
            'prop1': { 
                'type': ['object', 'null'],
                'properties':
                 {
                    'readonly': { 'type' : 'boolean' }
                 }
            }
        },
    }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(1, obj.Properties.Count);
            Assert.AreEqual("Prop1", obj.Properties.First().Type);
            Assert.AreEqual(1, objects.Count);
            Assert.AreEqual(1, objects.First().Value.Properties.Count);
            Assert.AreEqual(0, warnings.Count);
        }
        public void should_parse_primitive_arrays()
        {
            const string schema = @"
                {
                  '$schema': 'http://json-schema.org/draft-04/schema#',
                  'id': 'IdList',
                  'type': 'array',
                  'minItems': 1,
                  'uniqueItems': false,
                  'additionalItems': true,
                  'items': {
                    'id': '0',
                    'type': 'integer'
                  }
                }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(true, obj.IsArray);
            Assert.AreEqual("int", obj.Type);
            Assert.AreEqual(0, obj.Properties.Count);
        }
        public void should_parse_required_as_array_in_object_v4()
        {
            const string schema = @"
                {
                  '$schema': 'http://json-schema.org/draft-04/schema#',
                  'type': 'object',
                  'properties': {
                    'id': { 'type': 'integer' },
                    'name': { 'type': 'string' },
                    'observations': { 'type': 'string' }
                  },
                  'required': ['id', 'name']
                }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(3, obj.Properties.Count);
            Assert.AreEqual(true, obj.Properties.First(c => c.Name == "Id").Required);
            Assert.AreEqual(true, obj.Properties.First(c => c.Name == "Ipname").Required);
            Assert.AreEqual(false, obj.Properties.First(c => c.Name == "Observations").Required);
        }
        public void should_parse_bodyless_objects()
        {
            const string schema =
                "    {  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                "         \"type\": \"object\",\r\n" +
                "         \"description\": \"A single support status\",\r\n" +
                "         \"properties\": {\r\n" +
                "           \"id\":  { \"type\": \"string\", \"required\": true },\r\n" +
                "           \"name\": { \"type\": \"object\", \"required\": true },\r\n" +
                "           \"title\": { \"type\": \"object\", \r\n" +
                "                 \"properties\": \r\n" +
                "                   {\r\n" +
                "                      \"id\": { \"type\": \"string\", \"required\": true },\r\n" +
                "                      \"at\": { \"type\": \"string\", \"required\": true }\r\n" +
                "                   }\r\n" +
                "            }\r\n" +
                "         }\r\n" +
                "    }\r\n";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(3, obj.Properties.Count);
            Assert.AreEqual("Title", obj.Properties.First(p => p.Name == "Title").Type);
            Assert.AreEqual("object", obj.Properties.First(p => p.Name == "Ipname").Type);
        }
        public void should_parse_properties_as_nullable_when_not_required_v4()
        {
            const string schema = "{\r\n" +
                                  "      \"$schema\": \"http://json-schema.org/draft-04/schema\",\r\n" +
                                  "      \"type\": \"object\",\r\n" +
                                  "      \"properties\": \r\n" +
                                  "      {\r\n" +
                                  "        \"id\": { \"type\": \"integer\" },\r\n" +
                                  "        \"price\": { \"type\": \"number\" },\r\n" +
                                  "        \"optionalPrice\": { \"type\": \"number\" },\r\n" +
                                  "        \"orderItemId\": { \"type\": \"integer\" },\r\n" +
                                  "        \"description\": { \"type\": \"string\" },\r\n" +
                                  "        \"comment\": { \"type\": \"string\" },\r\n" +
                                  "        \"status\": { \"type\": \"boolean\", }\r\n" +
                                  "      },\r\n" +
                                  "    \"required\": [\"id\", \"price\", \"description\"]\r\n" +
                                  "    }\r\n";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual("int", obj.Properties.First(p => p.Name == "Id").Type);
            Assert.AreEqual("decimal", obj.Properties.First(p => p.Name == "Price").Type);
            Assert.AreEqual("decimal?", obj.Properties.First(p => p.Name == "OptionalPrice").Type);
            Assert.AreEqual("int?", obj.Properties.First(p => p.Name == "OrderItemId").Type);
            Assert.AreEqual("bool?", obj.Properties.First(p => p.Name == "Status").Type);
            Assert.AreEqual("string", obj.Properties.First(p => p.Name == "Description").Type);
            Assert.AreEqual("string", obj.Properties.First(p => p.Name == "Comment").Type);
        }
        private void GenerateButton_Click(object sender, EventArgs e)
        {
            SelectProjectButton.Enabled = false;
            GenerateButton.Enabled      = false;
            JsonSchemaParser.GenerateArraysInstedOfLists = ArrayCollectionsCheckbox.Checked;

            LogTextBox.Text = "Generating...";
            Schema schema;

            try
            {
                schema = JsonSchemaParser.ComposeEndpointSchema(SchemaTextBox.Text);
            }
            catch (Exception ex)
            {
                LogTextBox.Text            += "\r\nError reading endpoint";
                LogTextBox.Text            += "\r\n" + ex.Message;
                GenerateButton.Enabled      = true;
                SelectProjectButton.Enabled = true;
                return;
            }
            LogTextBox.Text += "\r\nEnpoint schema - OK";

            LogTextBox.Text += "\r\nWriting code...";

            SchemaGenerator.WriteCSharp(directoryPath + "\\",
                                        schema,
                                        (_) => LogTextBox.Text += ("\r\n" + _),
                                        "Acumatica." + endpointName.Replace(".", "_"),
                                        pathToProject,
                                        additionalPath);
        }
        public void should_keep_original_names()
        {
            const string schema = "{\r\n" +
                                  "  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                                  "  \"type\": \"array\",\r\n" +
                                  "  \"items\": \r\n" +
                                  "  {\r\n" +
                                  "    \"type\": \"object\",\r\n" +
                                  "    \"properties\": \r\n" +
                                  "    {\r\n" +
                                  "      \"id\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"at\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"to-address-id\": { \"type\": \"integer\", \"required\": true },\r\n" +
                                  "      \"order_item_id\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"status\": { \"type\": \"string\", \"required\": true, \"enum\": [ \"scheduled\", \"completed\", \"failed\" ] },\r\n" +
                                  "      \"droneId\": { \"type\": \"string\" }\r\n" +
                                  "    }\r\n" +
                                  "  }\r\n" +
                                  "}\r\n";
            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual("to-address-id", obj.Properties.First(p => p.Name == "Toaddressid").OriginalName);
            Assert.AreEqual("order_item_id", obj.Properties.First(p => p.Name == "Order_item_id").OriginalName);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;

            textBox2.Text = "Generating...";
            Schema schema;

            try
            {
                schema = JsonSchemaParser.ComposeEndpointSchema(textBox1.Text);
            }
            catch (Exception ex)
            {
                textBox2.Text  += "\r\nError reading endpoint";
                textBox2.Text  += "\r\n" + ex.Message;
                button1.Enabled = true;
                button2.Enabled = true;
                return;
            }
            textBox2.Text += "\r\nEnpoint schema - OK";

            textBox2.Text += "\r\nWriting code...";

            SchemaGenerator.WriteCSharp(directoryPath + "\\", schema, (_) => textBox2.Text += ("\r\n" + _), "Acumatica." + endpointName.Replace(".", "_"), pathToProject, additionalPath);
        }
        public void should_parse_enums()
        {
            const string schema = @"{
          'id': 'http://some.site.somewhere/entry-schema#',
          '$schema': 'http://json-schema.org/draft-03/schema#',
          'description': 'schema for an fstab entry',
          'type': 'object',
          'properties': {
              'fstype': {
                  'enum': [ 'ext3', 'ext4', 'btrfs' ]
              },
              'readonly': { 'type': 'boolean' }
          },
      }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(0, warnings.Count);
            Assert.AreEqual(2, obj.Properties.Count);
            Assert.AreEqual(1, enums.Count);
        }
Esempio n. 10
0
        public void should_parse_schema_when_array()
        {
            const string schema = "{\r\n" +
                                  "  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                                  "  \"type\": \"array\",\r\n" +
                                  "  \"items\": \r\n" +
                                  "  {\r\n" +
                                  "    \"type\": \"object\",\r\n" +
                                  "    \"properties\": \r\n" +
                                  "    {\r\n" +
                                  "      \"id\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"at\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"toAddressId\": { \"type\": \"integer\", \"required\": true },\r\n" +
                                  "      \"orderItemId\": { \"type\": \"string\", \"required\": true },\r\n" +
                                  "      \"status\": { \"type\": \"string\", \"required\": true, \"enum\": [ \"scheduled\", \"completed\", \"failed\" ] },\r\n" +
                                  "      \"droneId\": { \"type\": \"string\" }\r\n" +
                                  "    }\r\n" +
                                  "  }\r\n" +
                                  "}\r\n";
            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var obj      = parser.Parse("name", schema, objects, warnings);

            Assert.AreEqual(0, warnings.Count);
            Assert.AreEqual("Name", obj.Name);
            Assert.IsTrue(obj.IsArray);
            Assert.AreEqual(6, obj.Properties.Count);
            Assert.AreEqual("int", obj.Properties.First(p => p.Name == "ToAddressId").Type);
        }
        public void should_parse_recursive_schemas()
        {
            const string schema = "      { \r\n" +
                                  "        \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                                  "        \"type\": \"object\",\r\n" +
                                  "        \"id\": \"Customer\",\r\n" +
                                  "        \"properties\": {\r\n" +
                                  "          \"Id\": { \"type\": \"integer\"},\r\n" +
                                  "          \"Company\": { \"type\": \"string\"},\r\n" +
                                  "          \"SupportRepresentant\":\r\n" +
                                  "            { \r\n" +
                                  "              \"type\": \"object\",\r\n" +
                                  "              \"id\": \"Employee\",\r\n" +
                                  "              \"properties\": {\r\n" +
                                  "                \"Id\": { \"type\": \"integer\"},\r\n" +
                                  "                \"Title\": { \"type\": \"string\"},\r\n" +
                                  "                \"BirthDate\": { \"type\": \"string\"},\r\n" +
                                  "                \"HireDate\": { \"type\": \"string\"},\r\n" +
                                  "                \"ReportsTo\":\r\n" +
                                  "                  { \"$ref\": \"Employee\" },\r\n" +
                                  "                \"FirstName\": { \"type\": \"string\"},\r\n" +
                                  "                \"LastName\": { \"type\": \"string\"},\r\n" +
                                  "                \"Address\": { \"type\": \"string\"},\r\n" +
                                  "                \"City\": { \"type\": \"string\"},\r\n" +
                                  "                \"State\": { \"type\": \"string\"},\r\n" +
                                  "                \"Country\": { \"type\": \"string\"},\r\n" +
                                  "                \"PostalCode\": { \"type\": \"string\"},\r\n" +
                                  "                \"Phone\": { \"type\": \"string\"},\r\n" +
                                  "                \"Fax\": { \"type\": \"string\"},\r\n" +
                                  "                \"Email\": { \"type\": \"string\"}\r\n" +
                                  "              }\r\n" +
                                  "            },\r\n" +
                                  "          \"FirstName\": { \"type\": \"string\"},\r\n" +
                                  "          \"LastName\": { \"type\": \"string\"},\r\n" +
                                  "          \"Address\": { \"type\": \"string\"},\r\n" +
                                  "          \"City\": { \"type\": \"string\"},\r\n" +
                                  "          \"State\": { \"type\": \"string\"},\r\n" +
                                  "          \"Country\": { \"type\": \"string\"},\r\n" +
                                  "          \"PostalCode\": { \"type\": \"string\"},\r\n" +
                                  "          \"Phone\": { \"type\": \"string\"},\r\n" +
                                  "          \"Fax\": { \"type\": \"string\"},\r\n" +
                                  "          \"Email\": { \"type\": \"string\"}\r\n" +
                                  "        }\r\n" +
                                  "      }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();

            var obj = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(1, objects.Count);
            Assert.AreEqual("Employee", objects.First().Value.Name);
            Assert.AreEqual("Employee", objects.First().Value.Properties[4].Type);
            Assert.AreEqual("SupportRepresentant", obj.Properties[2].Name);
            Assert.AreEqual("Employee", obj.Properties[2].Type);
        }
Esempio n. 12
0
        public void GivenAJsonSchema_WhenParseJSchema_CorrectResultShouldBeReturned()
        {
            var testSchema         = JSchema.Parse(File.ReadAllText(Path.Join(TestConstants.CustomizedTestSchemaDirectory, "ValidSchema.schema.json")));
            var parquetSchemaNode  = JsonSchemaParser.ParseJSchema("testType", testSchema);
            var expectedSchemaNode = JSchema.Parse(File.ReadAllText(Path.Join(TestConstants.CustomizedTestSchemaDirectory, "ExpectedValidParquetSchemaNode.json")));

            Assert.True(JToken.DeepEquals(
                            JObject.Parse(JsonConvert.SerializeObject(parquetSchemaNode)),
                            JObject.Parse(JsonConvert.SerializeObject(expectedSchemaNode))));
        }
Esempio n. 13
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                // USAGE
                return;
            }
            var root   = new FileInfo(args[0]);
            var parser = new JsonSchemaParser(root.Directory);
            var source = parser.Load(root.Name, "");

            // extra
            source.AddJsonPath(".meshes[].primitives[].extras.targetNames", new JsonSchemaSource
            {
                type  = JsonSchemaType.Array,
                items = new JsonSchemaSource
                {
                    JsonPath = ".meshes[].primitives[].extras.targetNames[]",
                    type     = JsonSchemaType.String,
                },
            });

            for (int i = 2; i < args.Length; i += 2)
            {
                var jsonPath            = args[i];
                var extensionSchemaPath = new FileInfo(args[i + 1]);
                var extensionParser     = new JsonSchemaParser(root.Directory, extensionSchemaPath.Directory);
                var extensionSchema     = extensionParser.Load(extensionSchemaPath, jsonPath);
                var(parent, child) = JsonSchemaSource.SplitParent(jsonPath);
                {
                    var parentSchema = source.Get(parent);
                    parentSchema.AddProperty(child, extensionSchema);
                }
            }

            source.Dump();

            if (args.Length < 2)
            {
                return;
            }

            ProtoGenerator.Generator.GenerateTo(source, new DirectoryInfo(args[1]), clearFolder: true);
        }
        public void should_parse_attributes()
        {
            const string schema = @"
                {
                  '$schema': 'http://json-schema.org/draft-03/schema#',
                  'type': 'object',
                  'properties': {
                    'age': {
                        'type': 'integer',
                        'minimum': 18,
                        'required': true
                    },
                    'name': { 
                        'description': 'the name',
                        'type': 'string',
                        'minLength': 4,
                        'required': true
                    },
                    'observations': {
                        'description': 'the observations',
                        'type': 'string',
                        'maxLength': 255
                    },
                    'weight': { 
                        'type': 'number',
                        'maximum': 100
                    }
                  }
                }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(18, obj.Properties.First(c => c.Name == "Age").Minimum);
            Assert.AreEqual(100, obj.Properties.First(c => c.Name == "Weight").Maximum);
            Assert.AreEqual(4, obj.Properties.First(c => c.Name == "Ipname").MinLength);
            Assert.AreEqual(255, obj.Properties.First(c => c.Name == "Observations").MaxLength);
            Assert.AreEqual(true, obj.Properties.First(c => c.Name == "Age").Required);
            Assert.AreEqual(true, obj.Properties.First(c => c.Name == "Ipname").Required);
        }
Esempio n. 15
0
        public void should_build_custom_attributes()
        {
            const string schema = @"
                {
                  '$schema': 'http://json-schema.org/draft-03/schema#',
                  'type': 'object',
                  'properties': {
                    'age': {
                        'type': 'integer',
                        'minimum': 18,
                        'required': true
                    },
                    'name': { 
                        'description': 'the name',
                        'type': 'string',
                        'minLength': 4,
                        'required': true
                    },
                    'observations': {
                        'description': 'the observations',
                        'type': 'string',
                        'maxLength': 255
                    },
                    'weight': { 
                        'type': 'number',
                        'maximum': 100
                    }
                  }
                }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            var us       = new CultureInfo("en-US");
            var minValue = double.MinValue;

            Assert.AreEqual("        [Range(double.MinValue,100.00)]", obj.Properties.First(c => c.Name == "Weight").CustomAttributes);
            Assert.AreEqual("        [Required]" + Environment.NewLine + "        [Range(18,int.MaxValue)]", obj.Properties.First(c => c.Name == "Age").CustomAttributes);
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            string solutionFolderPath = GetParentDirectory(Directory.GetCurrentDirectory(), 5).ToString();

            foreach (var file in Directory.GetFiles(solutionFolderPath + EndpointSchemaDirectory))
            {
                string       endpoint = file.Replace(solutionFolderPath + EndpointSchemaDirectory, "");
                StreamReader reader   = new StreamReader(file);
                string       input    = reader.ReadToEnd();
                reader.Close();

                Schema schema = JsonSchemaParser.ComposeEndpointSchema(input);

                WriteCSharp(
                    solutionFolderPath + string.Format(OutputDirectoryTemplate, endpoint),
                    endpoint,
                    schema,
                    (_) => Console.WriteLine(_));
            }
        }
        public void should_parse_additionalProperties_as_dictionary()
        {
            const string schema = "{\r\n" +
                                  "      \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                                  "      \"type\": \"object\",\r\n" +
                                  "      \"properties\": \r\n" +
                                  "      {\r\n" +
                                  "        \"id\": { \"type\": \"integer\" },\r\n" +
                                  "        \"price\": { \"type\": \"number\" }\r\n" +
                                  "      },\r\n" +
                                  "     \"additionalProperties\": {\"type\": \"string\"}\r\n" +
                                  "    }\r\n";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(3, obj.Properties.Count);
            Assert.AreEqual("IDictionary<string, object>", obj.Properties.First(p => p.Name == "AdditionalProperties").Type);
        }
        public void should_parse_integers()
        {
            const string schema =
                "    {  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                "         \"type\": \"object\",\r\n" +
                "         \"description\": \"A single support status\",\r\n" +
                "         \"properties\": {\r\n" +
                "           \"id\":  { \"type\": \"string\", \"required\": true },\r\n" +
                "           \"name\": { \"type\": \"string\", \"required\": true },\r\n" +
                "           \"exampleIntProp\": { \"type\": \"integer\", \"required\": true }\r\n" +
                "         }\r\n" +
                "    }\r\n";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(3, obj.Properties.Count);
            Assert.AreEqual(1, obj.Properties.Count(p => p.Type == "int"));
            Assert.AreEqual(0, warnings.Count);
        }
Esempio n. 19
0
        public void Basics()
        {
            var schema = JsonSchemaParser.ParseFile("ContactSchema.json");

            dynamic contact = new DynamicData(schema);

            contact.First = "John";
            contact.Last  = "Smith";
            contact.Age   = 25;

            Assert.AreEqual("John", contact.First);
            Assert.AreEqual(25, contact.Age);

            contact.First = "Mark";
            Assert.AreEqual("Mark", contact.First);

            Assert.Throws <InvalidOperationException>(() => {
                contact.First = 20;
            });

            Assert.Throws <InvalidOperationException>(() => {
                contact.Middle = "Mark";
            });
        }
        public void should_parse_array_in_type_string()
        {
            const string schema = @"{
        '$schema': 'http://json-schema.org/draft-03/schema#',
        'type': 'object',
        'properties': {
            'prop1': { 
                'type': ['string', 'null']
            }
        },
    }";

            var parser = new JsonSchemaParser();
            var warnings = new Dictionary<string, string>();
            var objects = new Dictionary<string, ApiObject>();
            var enums = new Dictionary<string, ApiEnum>();
            var obj = parser.Parse("name", schema, objects, warnings, enums);

            Assert.AreEqual(1, obj.Properties.Count);
            Assert.AreEqual("string", obj.Properties.First().Type); 
            Assert.AreEqual(0, warnings.Count);
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                // USAGE
                return;
            }
            var root       = new FileInfo(args[0]);
            var parser     = new JsonSchemaParser(root.Directory);
            var jsonSchema = parser.Load(root.Name, "");

            // add extensions
            jsonSchema.AddJsonPath(".materials[].extensions.KHR_materials_unlit", new JsonSchemaSource
            {
                type = JsonSchemaType.Object,
            });
            jsonSchema.AddJsonPath(".meshes[].primitives[].extras.targetNames", new JsonSchemaSource
            {
                type  = JsonSchemaType.Array,
                items = new JsonSchemaSource
                {
                    JsonPath = ".meshes[].primitives[].extras.targetNames[]",
                    type     = JsonSchemaType.String,
                },
            });

            for (int i = 2; i < args.Length; i += 2)
            {
                var jsonPath            = args[i];
                var extensionSchemaPath = new FileInfo(args[i + 1]);
                var extensionParser     = new JsonSchemaParser(root.Directory, extensionSchemaPath.Directory);
                var extensionSchema     = extensionParser.Load(extensionSchemaPath, jsonPath);
                var(parent, child) = JsonSchemaSource.SplitParent(jsonPath);
                jsonSchema.Get(parent).AddProperty(child, extensionSchema);
            }

            foreach (var source in jsonSchema.Traverse())
            {
                var schema = source.Create();
                // index 項を列挙する
                if (IndexTargets.Map.TryGetValue(schema.JsonPath, out string target))
                {
                    if (schema is IntegerJsonSchema index)
                    {
                        Console.WriteLine($"{schema.JsonPath} => {target}");
                        index.IndexTargetJsonPath = target;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    // Console.WriteLine(schema.JsonPath);
                }

                if (schema.JsonPath == ".extensions.VRM.blendShapeMaster.blendShapeGroups[].binds[]")
                {
                    // 特別処理
                    schema.HardCode = @"
                // 対象の morph が存在していることを確認する
                var meshProp = json.GetProperty(""mesh"");
                var indexProp = json.GetProperty(""index"");                
                if(meshProp.ValueKind == JsonValueKind.Number
                    && indexProp.ValueKind == JsonValueKind.Number)
                {
                    var mesh = meshProp.GetInt32();
                    if(mesh<0)
                    {
                        m_context.AddMessage(MessageTypes.MinimumException, mesh, ""${json_path}"", ""mesh"");
                    }
                    else{
                        var jsonPath = string.Format("".meshes[{0}].primitives[*].targets"" , mesh);
                        if(m_context.TryGetArrayLength(jsonPath, out int length))
                        {
                            var index = indexProp.GetInt32();
                            if(index<0)
                            {
                                m_context.AddMessage(MessageTypes.MinimumException, index, ""${json_path}"", ""index"");
                            }
                            else if(index >= length)
                            {
                                m_context.AddMessage(MessageTypes.ArrayExceedLength, index, ""${json_path}"", ""index"");
                            }
                            else{
                                // OK
                            }
                        }
                        else
                        {
                            m_context.AddMessage(MessageTypes.ArrayNotExists, mesh, ""${json_path}"", ""mesh"");
                        }
                    }
                }
                else{
                    m_context.AddMessage(MessageTypes.InvalidType, json, ""${json_path}"", null);
                }
";
                }
            }

            if (args.Length < 2)
            {
                return;
            }

            var config = new ValidatorGenerator.Configuration
            {
                Prefix = "gltf",
                Suffix = "__Validator",
            };

            ValidatorGenerator.GenerateTo(jsonSchema, new DirectoryInfo(args[1]), config, true);
        }
 public void should_keep_original_names()
 {
     const string schema = "{\r\n" +
                           "  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
                           "  \"type\": \"array\",\r\n" +
                           "  \"items\": \r\n" +
                           "  {\r\n" +
                           "    \"type\": \"object\",\r\n" +
                           "    \"properties\": \r\n" +
                           "    {\r\n" +
                           "      \"id\": { \"type\": \"string\", \"required\": true },\r\n" +
                           "      \"at\": { \"type\": \"string\", \"required\": true },\r\n" +
                           "      \"to-address-id\": { \"type\": \"integer\", \"required\": true },\r\n" +
                           "      \"order_item_id\": { \"type\": \"string\", \"required\": true },\r\n" +
                           "      \"status\": { \"type\": \"string\", \"required\": true, \"enum\": [ \"scheduled\", \"completed\", \"failed\" ] },\r\n" +
                           "      \"droneId\": { \"type\": \"string\" }\r\n" +
                           "    }\r\n" +
                           "  }\r\n" +
                           "}\r\n";
     var parser = new JsonSchemaParser();
     var warnings = new Dictionary<string, string>();
     var objects = new Dictionary<string, ApiObject>();
     var enums = new Dictionary<string, ApiEnum>();
     var obj = parser.Parse("name", schema, objects, warnings, enums);
     Assert.AreEqual("to-address-id", obj.Properties.First(p => p.Name == "Toaddressid").OriginalName);
     Assert.AreEqual("order_item_id", obj.Properties.First(p => p.Name == "Order_item_id").OriginalName);
 }
	    public void should_parse_recursive_schemas()
	    {
	        var schema = "      { \r\n" +
	                     "        \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
	                     "        \"type\": \"object\",\r\n" +
	                     "        \"id\": \"Customer\",\r\n" +
	                     "        \"properties\": {\r\n" +
	                     "          \"Id\": { \"type\": \"integer\"},\r\n" +
	                     "          \"Company\": { \"type\": \"string\"},\r\n" +
	                     "          \"SupportRepresentant\":\r\n" +
	                     "            { \r\n" +
	                     "              \"type\": \"object\",\r\n" +
	                     "              \"id\": \"Employee\",\r\n" +
	                     "              \"properties\": {\r\n" +
	                     "                \"Id\": { \"type\": \"integer\"},\r\n" +
	                     "                \"Title\": { \"type\": \"string\"},\r\n" +
	                     "                \"BirthDate\": { \"type\": \"string\"},\r\n" +
	                     "                \"HireDate\": { \"type\": \"string\"},\r\n" +
	                     "                \"ReportsTo\":\r\n" +
	                     "                  { \"$ref\": \"Employee\" },\r\n" +
	                     "                \"FirstName\": { \"type\": \"string\"},\r\n" +
	                     "                \"LastName\": { \"type\": \"string\"},\r\n" +
	                     "                \"Address\": { \"type\": \"string\"},\r\n" +
	                     "                \"City\": { \"type\": \"string\"},\r\n" +
	                     "                \"State\": { \"type\": \"string\"},\r\n" +
	                     "                \"Country\": { \"type\": \"string\"},\r\n" +
	                     "                \"PostalCode\": { \"type\": \"string\"},\r\n" +
	                     "                \"Phone\": { \"type\": \"string\"},\r\n" +
	                     "                \"Fax\": { \"type\": \"string\"},\r\n" +
	                     "                \"Email\": { \"type\": \"string\"}\r\n" +
	                     "              }\r\n" +
	                     "            },\r\n" +
	                     "          \"FirstName\": { \"type\": \"string\"},\r\n" +
	                     "          \"LastName\": { \"type\": \"string\"},\r\n" +
	                     "          \"Address\": { \"type\": \"string\"},\r\n" +
	                     "          \"City\": { \"type\": \"string\"},\r\n" +
	                     "          \"State\": { \"type\": \"string\"},\r\n" +
	                     "          \"Country\": { \"type\": \"string\"},\r\n" +
	                     "          \"PostalCode\": { \"type\": \"string\"},\r\n" +
	                     "          \"Phone\": { \"type\": \"string\"},\r\n" +
	                     "          \"Fax\": { \"type\": \"string\"},\r\n" +
	                     "          \"Email\": { \"type\": \"string\"}\r\n" +
	                     "        }\r\n" +
	                     "      }";
            
            var parser = new JsonSchemaParser();
            var warnings = new Dictionary<string, string>();
            var objects = new Dictionary<string, ApiObject>();
            var enums = new Dictionary<string, ApiEnum>();

            var obj = parser.Parse("name", schema, objects, warnings, enums);
            Assert.AreEqual(1, objects.Count);
            Assert.AreEqual("Employee", objects.First().Value.Name);
            Assert.AreEqual("Employee", objects.First().Value.Properties[4].Type);
            Assert.AreEqual("SupportRepresentant", obj.Properties[2].Name);
            Assert.AreEqual("Employee", obj.Properties[2].Type);
	    }
		public void should_parse_schema_when_array()
		{
			const string schema = "{\r\n" +
			                      "  \"$schema\": \"http://json-schema.org/draft-03/schema\",\r\n" +
			                      "  \"type\": \"array\",\r\n" +
			                      "  \"items\": \r\n" +
			                      "  {\r\n" +
			                      "    \"type\": \"object\",\r\n" +
			                      "    \"properties\": \r\n" +
			                      "    {\r\n" +
			                      "      \"id\": { \"type\": \"string\", \"required\": true },\r\n" +
			                      "      \"at\": { \"type\": \"string\", \"required\": true },\r\n" +
			                      "      \"toAddressId\": { \"type\": \"integer\", \"required\": true },\r\n" +
			                      "      \"orderItemId\": { \"type\": \"string\", \"required\": true },\r\n" +
			                      "      \"status\": { \"type\": \"string\", \"required\": true, \"enum\": [ \"scheduled\", \"completed\", \"failed\" ] },\r\n" +
			                      "      \"droneId\": { \"type\": \"string\" }\r\n" +
			                      "    }\r\n" +
			                      "  }\r\n" +
			                      "}\r\n";
			var parser = new JsonSchemaParser();
			var warnings = new Dictionary<string, string>();
			var objects = new Dictionary<string, ApiObject>();
            var enums = new Dictionary<string, ApiEnum>();
			var obj = parser.Parse("name", schema, objects, warnings, enums);
			Assert.AreEqual(0, warnings.Count);
			Assert.AreEqual("Name", obj.Name);
			Assert.IsTrue(obj.IsArray);
			Assert.AreEqual(6, obj.Properties.Count);
			Assert.AreEqual("int", obj.Properties.First(p => p.Name == "ToAddressId").Type);
		}
	    public void should_parse_enums()
	    {
	        const string schema = @"{
          'id': 'http://some.site.somewhere/entry-schema#',
          '$schema': 'http://json-schema.org/draft-03/schema#',
          'description': 'schema for an fstab entry',
          'type': 'object',
          'properties': {
              'fstype': {
                  'enum': [ 'ext3', 'ext4', 'btrfs' ]
              },
              'readonly': { 'type': 'boolean' }
          },
      }";

            var parser = new JsonSchemaParser();
            var warnings = new Dictionary<string, string>();
            var objects = new Dictionary<string, ApiObject>();
            var enums = new Dictionary<string, ApiEnum>();
            var obj = parser.Parse("name", schema, objects, warnings, enums);

            Assert.AreEqual(0, warnings.Count);
            Assert.AreEqual(2, obj.Properties.Count);
            Assert.AreEqual(1, enums.Count);
	    }
        public void should_parse_v4_schema()
        {
           
            var schema = @"{
          'id': 'http://some.site.somewhere/entry-schema#',
          '$schema': 'http://json-schema.org/draft-04/schema#',
          'description': 'schema for an fstab entry',
          'type': 'object',
          'required': [ 'storage' ],
          'definitions': {
              'diskDevice': {
                  'properties': {
                      'type': { 'enum': [ 'disk' ] },
                      'device': {
                          'type': 'string',
                          'pattern': '^/dev/[^/]+(/[^/]+)*$'
                      }
                  },
                  'required': [ 'type', 'device' ],
                  'additionalProperties': false
              },
              'diskUUID': {
                  'properties': {
                      'type': { 'enum': [ 'disk' ] },
                      'label': {
                          'type': 'string',
                          'pattern': '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$'
                      }
                  },
                  'required': [ 'type', 'label' ],
                  'additionalProperties': false
              },
              'nfs': {
                  'properties': {
                      'type': { 'enum': [ 'nfs' ] },
                      'remotePath': {
                          'type': 'string',
                          'pattern': '^(/[^/]+)+$'
                      },
                      'server': {
                          'type': 'string',
                          'oneOf': [
                              { 'format': 'host-name' },
                              { 'format': 'ipv4' },
                              { 'format': 'ipv6' }
                          ]
                      }
                  },
                  'required': [ 'type', 'server', 'remotePath' ],
                  'additionalProperties': false
              },
              'tmpfs': {
                  'properties': {
                      'type': { 'enum': [ 'tmpfs' ] },
                      'sizeInMB': {
                          'type': 'integer',
                          'minimum': 16,
                          'maximum': 512
                      }
                  },
                  'required': [ 'type', 'sizeInMB' ],
                  'additionalProperties': false
              }
          },
          'properties': {
              'storage': {
                  'type': 'object',
                  'oneOf': [
                      { '$ref': '#/definitions/diskDevice' },
                      { '$ref': '#/definitions/diskUUID' },
                      { '$ref': '#/definitions/nfs' },
                      { '$ref': '#/definitions/tmpfs' }
                  ]
              },
              'fstype': {
                  'enum': [ 'ext3', 'ext4', 'btrfs' ]
              },
              'options': {
                  'type': 'array',
                  'minItems': 1,
                  'items': { 'type': 'string' },
                  'uniqueItems': true
              },
              'readonly': { 'type': 'boolean' }
          },
          
      }";

            var parser = new JsonSchemaParser();
            var warnings = new Dictionary<string, string>();
            var objects = new Dictionary<string, ApiObject>();
            var enums = new Dictionary<string, ApiEnum>();
            var obj = parser.Parse("name", schema, objects, warnings, enums);

            Assert.AreEqual(0, warnings.Count);
            Assert.AreEqual("Name", obj.Name);
        }
Esempio n. 27
0
        public void GivenAInvalidJsonSchema_WhenParseJSchema_ExceptionsShouldBeThrown(JSchema jSchema)
        {
            var schemaParser = new JsonSchemaParser();

            Assert.Throws <GenerateFhirParquetSchemaNodeException>(() => JsonSchemaParser.ParseJSchema("testType", jSchema));
        }
        public void should_parse_v4_schema()
        {
            const string schema = @"{
          'id': 'http://some.site.somewhere/entry-schema#',
          '$schema': 'http://json-schema.org/draft-04/schema#',
          'description': 'schema for an fstab entry',
          'type': 'object',
          'required': [ 'storage' ],
          'definitions': {
              'diskDevice': {
                  'properties': {
                      'type': { 'enum': [ 'disk' ] },
                      'device': {
                          'type': 'string',
                          'pattern': '^/dev/[^/]+(/[^/]+)*$'
                      }
                  },
                  'required': [ 'type', 'device' ],
                  'additionalProperties': false
              },
              'diskUUID': {
                  'properties': {
                      'type': { 'enum': [ 'disk' ] },
                      'label': {
                          'type': 'string',
                          'pattern': '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$'
                      }
                  },
                  'required': [ 'type', 'label' ],
                  'additionalProperties': false
              },
              'nfs': {
                  'properties': {
                      'type': { 'enum': [ 'nfs' ] },
                      'remotePath': {
                          'type': 'string',
                          'pattern': '^(/[^/]+)+$'
                      },
                      'server': {
                          'type': 'string',
                          'oneOf': [
                              { 'format': 'host-name' },
                              { 'format': 'ipv4' },
                              { 'format': 'ipv6' }
                          ]
                      }
                  },
                  'required': [ 'type', 'server', 'remotePath' ],
                  'additionalProperties': false
              },
              'tmpfs': {
                  'properties': {
                      'type': { 'enum': [ 'tmpfs' ] },
                      'sizeInMB': {
                          'type': 'integer',
                          'minimum': 16,
                          'maximum': 512
                      }
                  },
                  'required': [ 'type', 'sizeInMB' ],
                  'additionalProperties': false
              }
          },
          'properties': {
              'storage': {
                  'type': 'object',
                  'oneOf': [
                      { '$ref': '#/definitions/diskDevice' },
                      { '$ref': '#/definitions/diskUUID' },
                      { '$ref': '#/definitions/nfs' },
                      { '$ref': '#/definitions/tmpfs' }
                  ]
              },
              'fstype': {
                  'enum': [ 'ext3', 'ext4', 'btrfs' ]
              },
              'options': {
                  'type': 'array',
                  'minItems': 1,
                  'items': { 'type': 'string' },
                  'uniqueItems': true
              },
              'readonly': { 'type': 'boolean' }
          },
          
      }";

            var parser   = new JsonSchemaParser();
            var warnings = new Dictionary <string, string>();
            var objects  = new Dictionary <string, ApiObject>();
            var enums    = new Dictionary <string, ApiEnum>();
            var obj      = parser.Parse("name", schema, objects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());

            Assert.AreEqual(0, warnings.Count);
            Assert.AreEqual("Name", obj.Name);
        }