示例#1
0
 /// <summary>
 /// Performs a deep clone of a property.
 /// </summary>
 /// <returns>A deep clone of current object.</returns>
 public object Clone()
 {
     Property property = new Property();
     property.LoadFrom(this);
     property.Constraints = new Dictionary<Constraint, string>(this.Constraints);
     property.Extensions = new Dictionary<string, object>(this.Extensions);
     return property;
 }
 /// <summary>
 /// Initializes a new instance of the class PropertyTemplateModel.
 /// </summary>
 /// <param name="source">The source property object.</param>
 public PropertyTemplateModel(Property source)
 {
     this.LoadFrom(source);
 }
        public void ResourceIsFlattenedForConflictingResource()
        {
            var serviceClient = new ServiceClient();
            serviceClient.BaseUrl = "https://petstore.swagger.wordnik.com";
            serviceClient.ApiVersion = "1.0.0";
            serviceClient.Documentation =
                "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification";
            serviceClient.Name = "Swagger Petstore";

            var getPet = new Method();
            var resource = new CompositeType();
            var dogProperties = new CompositeType();
            var dog = new CompositeType();
            serviceClient.Methods.Add(getPet);
            resource.Name = "resource";
            resource.Properties.Add(new Property
            {
                Name = "id",
                SerializedName = "id",
                Type = new PrimaryType(KnownPrimaryType.String),
                IsRequired = true
            });
            resource.Properties.Add(new Property
            {
                Name = "location",
                SerializedName = "location",
                Type = new PrimaryType(KnownPrimaryType.String),
                IsRequired = true
            });
            resource.Properties.Add(new Property
            {
                Name = "name",
                SerializedName = "name",
                Type = new PrimaryType(KnownPrimaryType.String),
                IsRequired = true
            });
            resource.Properties.Add(new Property
            {
                Name = "tags",
                SerializedName = "tags",
                Type = new SequenceType { ElementType = new PrimaryType(KnownPrimaryType.String) },
                IsRequired = true
            });
            resource.Properties.Add(new Property
            {
                Name = "type",
                SerializedName = "type",
                Type = new PrimaryType(KnownPrimaryType.String),
                IsRequired = true
            });
            dogProperties.Name = "dogProperties";
            dogProperties.SerializedName = "dogProperties";
            dogProperties.Properties.Add(new Property
            {
                Name = "id",
                SerializedName = "id",
                Type = new PrimaryType(KnownPrimaryType.Long),
                IsRequired = true
            });
            dogProperties.Properties.Add(new Property
            {
                Name = "name",
                SerializedName = "name",
                Type = new PrimaryType(KnownPrimaryType.String),
                IsRequired = true
            });
            dog.Name = "dog";
            dog.SerializedName = "dog";
            dog.BaseModelType = resource;
            var dogPropertiesProperty = new Property
            {
                Name = "properties",
                SerializedName = "properties",
                Type = dogProperties,
                IsRequired = true
            };
            dogPropertiesProperty.Extensions[SwaggerExtensions.FlattenExtension] = true;
            dog.Properties.Add(dogPropertiesProperty);
            dog.Properties.Add(new Property
            {
                Name = "pedigree",
                SerializedName = "pedigree",
                Type = new PrimaryType(KnownPrimaryType.Boolean),
                IsRequired = true
            });
            getPet.ReturnType = new Response(dog, null);

            serviceClient.ModelTypes.Add(resource);
            serviceClient.ModelTypes.Add(dogProperties);
            serviceClient.ModelTypes.Add(dog);

            var codeGen = new SampleAzureCodeGenerator(new Settings());
            codeGen.NormalizeClientModel(serviceClient);
            Assert.Equal(3, serviceClient.ModelTypes.Count);
            Assert.Equal("dog", serviceClient.ModelTypes.First(m => m.Name == "dog").Name);
            Assert.Equal(3, serviceClient.ModelTypes.First(m => m.Name == "dog").Properties.Count);
            Assert.True(serviceClient.ModelTypes.First(m => m.Name == "dog").Properties.Any(p => p.Name == "dog_name"));
            Assert.True(serviceClient.ModelTypes.First(m => m.Name == "dog").Properties.Any(p => p.Name == "dog_id"));
            Assert.True(serviceClient.ModelTypes.First(m => m.Name == "dog").Properties.Any(p => p.Name == "pedigree"));
            Assert.Equal("dog", serviceClient.Methods[0].ReturnType.Body.Name);
            Assert.Equal(serviceClient.ModelTypes.First(m => m.Name == "dog"), serviceClient.Methods[0].ReturnType.Body);
        }