Esempio n. 1
0
        public async Task ShouldBuild_WhenChinook()
        {
            var model = await BuildModel("files/raml1/chinook-v1.raml");

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("InvoiceLine"), model.Objects.First(c => c.Name == "Invoice").Properties.First(p => p.Name == "Lines").Type);

            Assert.AreEqual("ArtistByTrack", model.Objects.First(c => c.Name == "ArtistByTrack").Type);
            Assert.AreEqual("Dictionary<string,Artist>", model.Objects.First(c => c.Name == "ArtistByTrack").BaseClass);
            Assert.AreEqual("TracksByArtist", model.Objects.First(c => c.Name == "TracksByArtist").Type);
            Assert.AreEqual("Dictionary<string,Track[]>", model.Objects.First(c => c.Name == "TracksByArtist").BaseClass);

            Assert.AreEqual("ArtistByTrack", model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Url == "bytrack/{id}").ReturnType);
            Assert.AreEqual("TracksByArtist", model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Url == "byartist/{id}").ReturnType);

            Assert.AreEqual("Artist", model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Album", model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Track", model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Customer", model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "GetById").ReturnType);

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Artist"), model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Album"), model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Customer"), model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Track"), model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "Get").ReturnType);

            Assert.AreEqual("Artist", model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Album", model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Customer", model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Track", model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "Post").Parameter.Type);
        }
        public async Task ShouldBuild_EvenWithIncludedDisorderedTypes()
        {
            var model = await BuildModel("files/raml1/types-order-includes.raml");

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("InvoiceLine"), model.Objects.First(c => c.Name == "Invoice").Properties.First(p => p.Name == "Lines").Type);

            Assert.AreEqual("Artist", model.Objects.First(c => c.Name == "Artist").Type);
            Assert.AreEqual(2, model.Objects.First(c => c.Name == "Artist").Properties.Count());

            Assert.AreEqual(3, model.Objects.First(c => c.Name == "Album").Properties.Count());
            Assert.AreEqual("Artist", model.Objects.First(c => c.Name == "Album").Properties.First(m => m.Name == "Artist").Type);

            Assert.AreEqual("Artist", model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Album", model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Track", model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "GetById").ReturnType);
            Assert.AreEqual("Customer", model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "GetById").ReturnType);

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Artist"), model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Album"), model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Customer"), model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Track"), model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "Get").ReturnType);

            Assert.AreEqual("Artist", model.Controllers.First(c => c.Name == "Artists").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Album", model.Controllers.First(c => c.Name == "Albums").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Customer", model.Controllers.First(c => c.Name == "Customers").Methods.First(m => m.Name == "Post").Parameter.Type);
            Assert.AreEqual("Track", model.Controllers.First(c => c.Name == "Tracks").Methods.First(m => m.Name == "Post").Parameter.Type);
        }
        private void ParseArray(IDictionary <string, ApiObject> objects, JsonSchema schema, Property prop, KeyValuePair <string, JsonSchema> property, IDictionary <string, ApiEnum> enums)
        {
            if (schema.Items == null || !schema.Items.Any())
            {
                return;
            }

            var netType = NetTypeMapper.GetNetType(schema.Items.First().Type, property.Value.Format);

            if (netType != null)
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(netType);
            }
            else
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(property.Key));
                foreach (var item in schema.Items)
                {
                    var modifiedKey = ParseObject(property.Key, item, objects, enums);
                    if (modifiedKey != null)
                    {
                        prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(modifiedKey));
                    }
                }
            }
        }
        public void should_parse_bodyless_arrays()
        {
            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" +
                "           \"names\": { \"type\": \"array\", \"required\": true },\r\n" +
                "           \"titles\": { \"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" +
                "                       }\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(CollectionTypeHelper.GetCollectionType("string"), obj.Properties.First(p => p.Name == "Names").Type);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Titles"), obj.Properties.First(p => p.Name == "Titles").Type);
        }
        public async Task ShouldHandleTypeExpressions()
        {
            var model = await GetTypeExpressionsModel();

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Movie"), model.Classes.First().Methods.First(m => m.Verb == "Get").OkReturnType);
            Assert.AreEqual("string", model.Classes.First().Methods.First(m => m.Verb == "Put").Parameter.Type);
            Assert.AreEqual("string", model.Classes.First().Methods.First(m => m.Verb == "Post").Parameter.Type);
        }
Esempio n. 6
0
        public async Task ShouldDetectArrayTypes_WhenMovies()
        {
            var model = await GetMoviesModel();

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Movie"), model.Controllers.First(o => o.Name == "Movies").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Movie"), model.Controllers.First(o => o.Name == "Movies").Methods.First(m => m.Name == "GetAvailable").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Movie"), model.Controllers.First(o => o.Name == "Search").Methods.First(m => m.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Movie"), model.Controllers.First(o => o.Name == "Movies").Methods.First(m => m.Name == "Post").Parameter.Type);
        }
Esempio n. 7
0
        public async Task ShouldHandleCasing()
        {
            var model = await BuildModel("files/raml1/case.raml");

            Assert.IsNotNull(model.Objects.First(c => c.Name == "Person"));
            Assert.IsNotNull(model.Objects.First(c => c.Name == "Customer"));
            Assert.AreEqual("Person", model.Objects.First(c => c.Name == "Customer").BaseClass);

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Controllers.First(c => c.Name == "Persons").Methods.First(m => m.Verb == "Post").Parameter.Type);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Controllers.First(c => c.Name == "Persons").Methods.First(m => m.Verb == "Get").ReturnType);
        }
Esempio n. 8
0
        public async Task ShouldHandleDates()
        {
            var model = await BuildModel("files/raml1/dates.raml");

            Assert.AreEqual(3, model.Objects.Count());
            Assert.AreEqual("DateTime", model.Objects.First(x => x.Name == "Person").Properties.First(x => x.Name == "Born").Type);
            Assert.AreEqual("DateTime", model.Objects.First(x => x.Name == "User").Properties.First(x => x.Name == "Lastaccess").Type);
            Assert.AreEqual("DateTime", model.Objects.First(x => x.Name == "Sample").Properties.First(x => x.Name == "Prop1").Type);
            Assert.AreEqual("DateTime", model.Objects.First(x => x.Name == "Sample").Properties.First(x => x.Name == "Prop2").Type);
            Assert.AreEqual("DateTimeOffset", model.Objects.First(x => x.Name == "Sample").Properties.First(x => x.Name == "Prop3").Type);

            Assert.AreEqual("DateTime", model.Controllers.First(x => x.Name == "Access").Methods.First(x => x.Name == "Post").Parameter.Type);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("DateTime"), model.Controllers.First(x => x.Name == "Access").Methods.First(x => x.Name == "Get").ReturnType);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("DateTime"), model.Controllers.First(x => x.Name == "Persons").Methods.First(x => x.Name == "Put").Parameter.Type);
        }
        private static string GetType(KeyValuePair <string, JsonSchema> property, bool isEnum, string enumName)
        {
            if (isEnum)
            {
                return(enumName);
            }

            var type = NetTypeMapper.Map(property.Value.Type);

            if (!string.IsNullOrWhiteSpace(type))
            {
                if (type == "string" || (property.Value.Required != null && property.Value.Required.Value))
                {
                    return(type);
                }

                return(type + "?");
            }

            if (HasMultipleTypes(property))
            {
                return(HandleMultipleTypes(property));
            }

            if (!string.IsNullOrWhiteSpace(property.Value.Id))
            {
                return(NetNamingMapper.GetObjectName(property.Value.Id));
            }

            // if it is a "body less" array then I assume it's an array of strings
            if (property.Value.Type == JsonSchemaType.Array && (property.Value.Items == null || !property.Value.Items.Any()))
            {
                return(CollectionTypeHelper.GetCollectionType("string"));
            }

            // if it is a "body less" object then use object as the type
            if (property.Value.Type == JsonSchemaType.Object && (property.Value.Properties == null || !property.Value.Properties.Any()))
            {
                return("object");
            }

            if (property.Value == null)
            {
                return(null);
            }

            return(NetNamingMapper.GetObjectName(property.Key));
        }
Esempio n. 10
0
        public async Task ShouldHandleUnionTypes()
        {
            var model = await BuildModel("files/raml1/uniontypes.raml");

            Assert.AreEqual(5, model.Objects.Count());

            Assert.AreEqual(2, model.Objects.First(c => c.Name == "Customer").Properties.Count);
            Assert.AreEqual("Person", model.Objects.First(c => c.Name == "Customer").Properties.First(c => c.Name == "Person").Type);
            Assert.AreEqual("Company", model.Objects.First(c => c.Name == "Customer").Properties.First(c => c.Name == "Company").Type);

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Objects.First(c => c.Name == "Customers").Properties.First(c => c.Name == "Person").Type);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Company"), model.Objects.First(c => c.Name == "Customers").Properties.First(c => c.Name == "Company").Type);

            Assert.AreEqual(false, model.Objects.First(c => c.Name == "Group").IsArray);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Objects.First(c => c.Name == "Group").Properties.First(c => c.Name == "Person").Type);
            Assert.AreEqual("Company", model.Objects.First(c => c.Name == "Group").Properties.First(c => c.Name == "Company").Type);
        }
Esempio n. 11
0
        private void ParseArray(IDictionary <string, ApiObject> objects, Newtonsoft.JsonV4.Schema.JsonSchema schema, Property prop, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property, IDictionary <string, ApiEnum> enums)
        {
            var netType = NetTypeMapper.Map(schema.Items.First().Type);

            if (netType != null)
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(netType);
            }
            else
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(property.Key));
                foreach (var item in schema.Items)
                {
                    ParseObject(property.Key, item.Properties, objects, enums, item);
                }
            }
        }
        public async Task ShouldBuildArrayTypes()
        {
            var model = await GetArraysModel();

            Assert.IsTrue(model.Objects.Any(o => o.Name == "ArrayOfObjectItem"));
            Assert.IsTrue(model.Objects.Any(o => o.Name == "ArrayOfPerson"));
            Assert.IsTrue(model.Objects.Any(o => o.Name == "ArrayOfInt"));
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("int"), model.Objects.First(o => o.Name == "ArrayOfInt").Type);
            Assert.IsTrue(model.Objects.Any(o => o.Name == "ArrayOfObject"));
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("ArrayOfObjectItem"), model.Objects.First(o => o.Name == "ArrayOfObject").Type);
            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Objects.First(o => o.Name == "ArrayOfPerson").Type);

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Person"), model.Objects
                            .First(o => o.Name == "TypeThatHasArrayProperty")
                            .Properties
                            .First(p => p.Name == "Persons").Type);

            Assert.AreEqual(6, model.Objects.Count());
        }
        public void should_clean_objects_not_used_as_return_types_client()
        {
            var property = new Property
            {
                Name         = "Prop1",
                Type         = CollectionTypeHelper.GetCollectionType("Object2"),
                OriginalName = "prop1"
            };

            var property2 = new Property
            {
                Name         = "Prop2",
                Type         = "int",
                OriginalName = "prop1"
            };

            var o1 = new ApiObject
            {
                Name       = "Object1",
                Properties = new[] { property }
            };

            var o2 = new ApiObject
            {
                Name       = "Object2",
                IsArray    = true,
                Properties = new[] { property2 }
            };

            var o3 = new ApiObject
            {
                Name       = "Object3",
                IsArray    = true,
                Properties = new[] { property }
            };

            var schemaResponseObjects = new Dictionary <string, ApiObject>();

            schemaResponseObjects.Add("obj1", o1);
            schemaResponseObjects.Add("obj2", o2);
            schemaResponseObjects.Add("obj3", o3);

            var schemaRequestObjects = new Dictionary <string, ApiObject>();
            var cleaner      = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, new Dictionary <string, ApiObject>());
            var classObjects = new List <ClassObject>();

            var methods = new Collection <ClientGeneratorMethod>
            {
                new ClientGeneratorMethod
                {
                    ReturnType = CollectionTypeHelper.GetCollectionType("Object1")
                }
            };

            classObjects.Add(new ClassObject
            {
                Name    = "Class1",
                Methods = methods
            });

            cleaner.CleanObjects(classObjects, schemaResponseObjects, cleaner.IsUsedAsResponseInAnyMethod);

            Assert.AreEqual(2, schemaResponseObjects.Count);
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj1"));
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj2"));
            Assert.IsFalse(schemaResponseObjects.ContainsKey("obj3"));
        }
Esempio n. 14
0
        public async Task ShouldParseArrays()
        {
            var model = await GetTestGeneratedModel();

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Sales"), model.Objects.First(o => o.Name == "GetSales").Properties[1].Type);
        }
Esempio n. 15
0
        public async Task ShouldHandleArrayAsExpression()
        {
            var model = await BuildModel("files/raml1/array-type-expression.raml");

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("string"), model.Objects.First().Properties.First().Type);
        }
        public async Task ShouldParseArrays()
        {
            var model = await GetTestGeneratedModel();

            Assert.AreEqual(CollectionTypeHelper.GetCollectionType("Sales"), model.ResponseObjects["/sales-getOKResponseContent"].Properties[1].Type);
        }