예제 #1
0
        public CodeModelCsaf()
        {
            _innerTypes = new HashSet<CompositeType>();

            _resourceType = New<CompositeType>(new
            {
                Name = "Microsoft.Rest.Azure.Resource",
                SerializedName = "Resource",
            });

            var stringType = New<PrimaryType>(KnownPrimaryType.String,new
            {
                Name = "string"
            });

            _resourceType.Add(New <Property>(new { Name = "location", SerializedName = "location", Type = stringType }));
            _resourceType.Add(New <Property>(new { Name = "id", SerializedName = "id", Type = stringType }));
            _resourceType.Add(New <Property>(new { Name = "name", SerializedName = "name", Type = stringType }));
            _resourceType.Add(New <Property>(new { Name = "type", SerializedName = "type", Type = stringType }));
            _resourceType.Add(New <Property>(new { Name = "tags", SerializedName = "tags", Type = New<DictionaryType>(new { ValueType = stringType, NameFormat = "System.Collections.Generic.IDictionary<string, {0}>" }) }));

            _subResourceType = New<CompositeType>(new
            {
                Name = "Microsoft.Rest.Azure.SubResource",
                SerializedName = "SubResource"
            });
            _subResourceType.Add(New<Property>(new { Name = "id", SerializedName = "id", Type = stringType }));
        }
예제 #2
0
        public override CompositeType Add(CompositeType item)
        {
            // Removing all models that contain the extension "x-ms-external", as they will be
            // generated in nodejs client runtime for azure - "ms-rest-azure".
            if (item.Extensions.ContainsKey(AzureExtensions.PageableExtension) ||
                item.Extensions.ContainsKey(AzureExtensions.ExternalExtension))
            {
                return null;
            }

            return base.Add(item);
        }
예제 #3
0
 public virtual CompositeType AddHeader(CompositeType item)
 {
     if( !_headertypes.Contains( item ) && !_modeltypes.Contains( item ) )
     {
         BeforeAddHeader(item);
         // disambiguation is performed when the item's parent reference is changed
         item.CodeModel = this;
         _headertypes.Add(item);
     }
     return item;
 }
예제 #4
0
 partial void BeforeAddHeader(CompositeType item);
예제 #5
0
 public virtual void RemoveError(CompositeType item)
 {
     _errortypes.Remove(item);
 }
예제 #6
0
 partial void BeforeAddError(CompositeType item);
예제 #7
0
 public virtual void Remove(CompositeType item)
 {
     _modeltypes.Remove(item);
 }
예제 #8
0
        /// <summary>
        /// Determines a composite type as an External Resource if it's name equals "Resource" 
        /// and it has an extension named "x-ms-azure-resource" marked as true.
        /// </summary>
        /// <param name="compositeType">Type to determine if it is an external resource</param>
        /// <returns>True if it is an external resource, false otherwise</returns>
        public static bool IsAzureResource(CompositeType compositeType)
        {
            if (compositeType == null)
            {
                return false;
            }

            if (compositeType.ComposedExtensions.ContainsKey(AzureResourceExtension))
            {
                var external = compositeType.ComposedExtensions[AzureResourceExtension] as bool?;
                return (external == null || external.Value);
            }

            return false;
        }
        private List<Stack<IModelType>> BuildResponses(Method method, CompositeType headerType)
        {
            string methodName = method.Name;
            var typesList = new List<Stack<IModelType>>();
            foreach (var response in _operation.Responses)
            {
                if (response.Key.EqualsIgnoreCase("default"))
                {
                    TryBuildDefaultResponse(methodName, response.Value, method, headerType);
                }
                else
                {
                    if (
                        !(TryBuildResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                            typesList, headerType) ||
                          TryBuildStreamResponse(response.Key.ToHttpStatusCode(), response.Value, method, typesList, headerType) ||
                          TryBuildEmptyResponse(methodName, response.Key.ToHttpStatusCode(), response.Value, method,
                              typesList, headerType)))
                    {
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.InvariantCulture,
                            Resources.UnsupportedMimeTypeForResponseBody,
                            methodName,
                            response.Key));
                    }
                }
            }

            return typesList;
        }
 /// <summary>
 /// Determines whether one composite type derives directly or indirectly from another.
 /// </summary>
 /// <param name="type">Type to test.</param>
 /// <param name="possibleAncestorType">Type that may be an ancestor of this type.</param>
 /// <returns>true if the type is an ancestor, false otherwise.</returns>
 public static bool DerivesFrom(this CompositeType type, CompositeType possibleAncestorType)
 {
     return
         type.BaseModelType != null &&
         (type.BaseModelType.Equals(possibleAncestorType) ||
          type.BaseModelType.DerivesFrom(possibleAncestorType));
 }
예제 #11
0
        protected void AddHeaderDictionary(IndentedStringBuilder builder, CompositeType headersType)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (headersType == null)
            {
                throw new ArgumentNullException("headersType");
            }

            foreach (var prop in headersType.Properties)
            {
                var enumType = prop.ModelType as EnumType;
                if (CodeModel.EnumTypes.Contains(prop.ModelType) && !enumType.ModelAsString)
                {
                    builder.AppendLine(String.Format(CultureInfo.InvariantCulture, "'{0}': models.{1},", prop.SerializedName, prop.ModelType.ToPythonRuntimeTypeString()));
                }
                else
                {
                    builder.AppendLine(String.Format(CultureInfo.InvariantCulture, "'{0}': '{1}',", prop.SerializedName, prop.ModelType.ToPythonRuntimeTypeString()));
                }
            }
        }
        private static JsonSchema ParseCompositeType(Property property, CompositeType compositeType, IDictionary<string, JsonSchema> definitions, IEnumerable<CompositeType> modelTypes)
        {
            string definitionName = compositeType.Name.RawValue;

            if (!definitions.ContainsKey(definitionName))
            {
                JsonSchema definition = new JsonSchema()
                {
                    JsonType = "object",
                    Description = compositeType.Documentation
                };

                // This definition must be added to the definition map before we start parsing
                // its properties because its properties may recursively reference back to this
                // definition.
                definitions.Add(definitionName, definition);

                foreach (Property subProperty in compositeType.ComposedProperties)
                {
                    JsonSchema subPropertyDefinition = ParseType(subProperty, subProperty.ModelType, definitions, modelTypes);
                    if (subPropertyDefinition != null)
                    {
                        definition.AddProperty(subProperty.Name.RawValue, subPropertyDefinition, subProperty.IsRequired);
                    }
                }

                string discriminatorPropertyName = compositeType.PolymorphicDiscriminator;
                if (!string.IsNullOrWhiteSpace(discriminatorPropertyName))
                {
                    CompositeType[] subTypes = modelTypes.Where(modelType => modelType.BaseModelType == compositeType).ToArray();
                    if (subTypes != null && subTypes.Length > 0)
                    {
                        JsonSchema discriminatorDefinition = new JsonSchema()
                        {
                            JsonType = "string"
                        };

                        if (subTypes.Length == 1)
                        {
                            CompositeType subType = subTypes[0];
                            if (subType != null)
                            {
                                foreach (Property subTypeProperty in subType.Properties)
                                {
                                    JsonSchema subTypePropertyDefinition = ParseType(subTypeProperty, subTypeProperty.ModelType, definitions, modelTypes);
                                    if (subTypePropertyDefinition != null)
                                    {
                                        definition.AddProperty(subTypeProperty.Name.RawValue, subTypePropertyDefinition, subTypeProperty.IsRequired);
                                    }
                                }

                                const string discriminatorValueExtensionName = "x-ms-discriminator-value";
                                if (subType.ComposedExtensions.ContainsKey(discriminatorValueExtensionName))
                                {
                                    string discriminatorValue = subType.ComposedExtensions[discriminatorValueExtensionName] as string;
                                    if (!string.IsNullOrWhiteSpace(discriminatorValue))
                                    {
                                        discriminatorDefinition.AddEnum(discriminatorValue);
                                    }
                                }
                            }

                            definition.AddProperty(discriminatorPropertyName, discriminatorDefinition);
                        }
                        else
                        {
                            string errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Multiple sub-types ({0}) of a polymorphic discriminated type ({1}) are not currently supported.",
                                string.Join(", ", subTypes.Select(subType => subType.Name.RawValue)),
                                compositeType.Name.RawValue);
                            throw new NotSupportedException(errorMessage);
                        }
                    }
                }
            }

            JsonSchema result = new JsonSchema()
            {
                Ref = "#/definitions/" + definitionName
            };

            if (property != null)
            {
                result.Description = RemovePossibleValuesFromDescription(property.Documentation);
            }

            return result;
        }
예제 #13
0
        private static void RemoveFlatteningConflicts(CompositeType compositeType)
        {
            if (compositeType == null)
            {
                throw new ArgumentNullException("compositeType");
            }

            foreach (Property innerProperty in compositeType.Properties)
            {
                // Check conflict among peers

                var conflictingPeers = compositeType.Properties
                    .Where(p => p.Name == innerProperty.Name && p.SerializedName != innerProperty.SerializedName);

                if (conflictingPeers.Any())
                {
                    foreach (var cp in conflictingPeers.Concat(new[] { innerProperty }))
                    {
                        if (cp.Extensions.ContainsKey(FlattenOriginalTypeName))
                        {
                            cp.Name = cp.Extensions[FlattenOriginalTypeName].ToString() + "_" + cp.Name;
                        }
                    }
                }

                if (compositeType.BaseModelType != null)
                {
                    var conflictingParentProperties = compositeType.BaseModelType.ComposedProperties
                        .Where(p => p.Name == innerProperty.Name && p.SerializedName != innerProperty.SerializedName);

                    if (conflictingParentProperties.Any())
                    {
                        innerProperty.Name = compositeType.Name + "_" + innerProperty.Name;
                    }
                }
            }
        }
예제 #14
0
 public PageRba(CompositeType source,  string nextLinkName, string itemName)
 {
     this.LoadFrom(source);
     this.NextLinkName = nextLinkName;
     this.ItemName = itemName;
 }
예제 #15
0
 public virtual CompositeType InsertHeader(CompositeType item)
 {
     if( !_headertypes.Contains(item))
     {
         // disambiguation is performed when the item's parent reference is changed
         item.CodeModel = this;
         _headertypes.Insert(0, item);
     }
     return item;
 }
예제 #16
0
 /// <summary>
 /// Checks whether model should be excluded from producing.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>True if should be excluded, false otherwise.</returns>
 protected override bool ExcludeModel(CompositeType model)
 {
     return (model.Extensions.ContainsKey(AzureExtensions.ExternalExtension) && 
             (bool) model.Extensions[AzureExtensions.ExternalExtension]) 
             || model.Name == "Resource" || model.Name == "SubResource";
 }
예제 #17
0
 public virtual void RemoveHeader(CompositeType item)
 {
     _headertypes.Remove(item);
 }
예제 #18
0
 partial void BeforeAdd(CompositeType item);
 private void VerifyFirstPropertyIsByteArray(CompositeType serviceType)
 {
     var referenceKey = serviceType.Name.RawValue;
     var responseType = _swaggerModeler.GeneratedTypes[referenceKey];
     var property = responseType.Properties.FirstOrDefault(p => p.ModelType is PrimaryType && ((PrimaryType)p.ModelType).KnownPrimaryType == KnownPrimaryType.ByteArray);
     if (property == null)
     {
         throw new KeyNotFoundException(
             "Please specify a field with type of System.Byte[] to deserialize the file contents to");
     }
 }
예제 #20
0
 /// <summary>
 /// Checks whether model should be excluded from producing.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>True if should be excluded, false otherwise.</returns>
 protected virtual bool ExcludeModel(CompositeType model)
 {
     return false;
 }