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[Generator.Extensions.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);
        }
        public override IType BuildServiceType(string serviceTypeName)
        {
            // Check if already generated
            if (serviceTypeName != null && Modeler.GeneratedTypes.ContainsKey(serviceTypeName))
            {
                return Modeler.GeneratedTypes[serviceTypeName];
            }

            _schema = Modeler.Resolver.Unwrap(_schema);

            // If primitive type
            if ((_schema.Type != null && _schema.Type != DataType.Object) ||
                _schema.AdditionalProperties != null)
            {
                return _schema.GetBuilder(Modeler).ParentBuildServiceType(serviceTypeName);
            }

            // If the object does not have any properties, treat it as raw json (i.e. object)
            if (_schema.Properties.IsNullOrEmpty() && string.IsNullOrEmpty(_schema.Extends))
            {
                return PrimaryType.Object;
            }

            // Otherwise create new object type
            var objectType = new CompositeType 
                            { 
                                Name = serviceTypeName, 
                                SerializedName = serviceTypeName, 
                                Documentation = _schema.Description 
                            };
            // Put this in already generated types serializationProperty
            Modeler.GeneratedTypes[serviceTypeName] = objectType;

            if (_schema.Properties != null)
            {
                // Visit each property and recursively build service types
                foreach (var property in _schema.Properties)
                {
                    string name = property.Key;
                    if (name != _schema.Discriminator)
                    {
                        string propertyServiceTypeName;
                        if (property.Value.Reference != null)
                        {
                            propertyServiceTypeName = property.Value.Reference.StripDefinitionPath();
                        }
                        else
                        {
                            propertyServiceTypeName = serviceTypeName + "_" + property.Key;
                        }
                        var propertyType =
                            property.Value.GetBuilder(Modeler).BuildServiceType(propertyServiceTypeName);

                        var propertyObj = new Property
                        {
                            Name = name,
                            SerializedName = name,
                            Type = propertyType,
                            IsRequired = property.Value.IsRequired
                        };

                        //propertyObj.Type = objectType;
                        propertyObj.Documentation = property.Value.Description;
                        var enumType = propertyType as EnumType;
                        if (enumType != null)
                        {
                            if (propertyObj.Documentation == null)
                            {
                                propertyObj.Documentation = string.Empty;
                            }
                            else
                            {
                                propertyObj.Documentation = propertyObj.Documentation.TrimEnd('.') + ". ";
                            }
                            propertyObj.Documentation += "Possible values for this property include: " +
                                                       string.Join(", ", enumType.Values.Select(v =>
                                                           string.Format(CultureInfo.InvariantCulture, 
                                                           "'{0}'", v.Name))) + ".";
                        }
                        propertyObj.IsReadOnly = property.Value.ReadOnly;
                        objectType.Properties.Add(propertyObj);
                    }
                    else
                    {
                        objectType.PolymorphicDiscriminator = name;
                    }
                }
            }

            // Copy over extensions
            _schema.Extensions.ForEach(e => objectType.Extensions[e.Key] = e.Value);

            // Put this in the extended type serializationProperty for building method return type in the end
            if (_schema.Extends != null)
            {
                Modeler.ExtendedTypes[serviceTypeName] = _schema.Extends.StripDefinitionPath();
            }

            return objectType;
        }
 /// <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);
 }