コード例 #1
0
ファイル: JsonSchema.cs プロジェクト: lurumad/autorest
        public static JsonSchema CreateStringEnum(string enumValue, params string[] extraEnumValues)
        {
            JsonSchema result = new JsonSchema()
            {
                JsonType = "string"
            };

            result.AddEnum(enumValue);
            if (extraEnumValues != null)
            {
                foreach (string extraEnumValue in extraEnumValues)
                {
                    result.AddEnum(extraEnumValue);
                }
            }
            return(result);
        }
コード例 #2
0
        public static JsonSchema CreateSingleValuedEnum(string enumValue)
        {
            var result = new JsonSchema
            {
                JsonType      = "string",
                Configuration = SchemaConfiguration.OmitExpressionRef,
            };

            result.AddEnum(enumValue);

            return(result);
        }
コード例 #3
0
        private static JsonSchema ParseEnumType(Property property, EnumType enumType)
        {
            JsonSchema result = new JsonSchema()
            {
                JsonType = "string"
            };

            foreach (EnumValue enumValue in enumType.Values)
            {
                result.AddEnum(enumValue.SerializedName);
            }

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

            return(result);
        }
コード例 #4
0
        private static JsonSchema ParsePrimaryType(Property property, PrimaryType primaryType)
        {
            JsonSchema result = new JsonSchema()
            {
                Format = primaryType.Format
            };

            switch (primaryType.Type)
            {
                case KnownPrimaryType.Boolean:
                    result.JsonType = "boolean";
                    break;

                case KnownPrimaryType.Int:
                case KnownPrimaryType.Long:
                    result.JsonType = "integer";
                    break;

                case KnownPrimaryType.Double:
                    result.JsonType = "number";
                    break;

                case KnownPrimaryType.Object:
                    result.JsonType = "object";
                    break;

                case KnownPrimaryType.DateTime:
                case KnownPrimaryType.String:
                case KnownPrimaryType.TimeSpan:
                    result.JsonType = "string";
                    break;

                default:
                    Debug.Assert(false, "Unrecognized known property type: " + primaryType.Type);
                    break;
            }

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

                if (property.DefaultValue != null)
                {
                    result.AddEnum(property.DefaultValue);
                }

                if (property.Constraints.Count > 0)
                {
                    foreach (KeyValuePair<Constraint, string> entry in property.Constraints)
                    {
                        switch (entry.Key)
                        {
                            case Constraint.InclusiveMinimum:
                                Debug.Assert(result.JsonType == "integer" || result.JsonType == "number", "Expected to only find an InclusiveMinimum constraint on an integer or number property.");
                                result.Minimum = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                                break;

                            case Constraint.InclusiveMaximum:
                                Debug.Assert(result.JsonType == "integer" || result.JsonType == "number", "Expected to only find an InclusiveMaximum constraint on an integer or number property.");
                                result.Maximum = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                                break;

                            case Constraint.Pattern:
                                Debug.Assert(result.JsonType == "string", "Expected to only find a Pattern constraint on a string property.");
                                result.Pattern = entry.Value;
                                break;

                            default:
                                Debug.Fail("Unrecognized property Constraint: " + entry.Key);
                                break;
                        }
                    }
                }
            }

            return result;
        }
コード例 #5
0
        private static JsonSchema ParseEnumType(Property property, EnumType enumType)
        {
            JsonSchema result = new JsonSchema()
            {
                JsonType = "string"
            };

            foreach (EnumValue enumValue in enumType.Values)
            {
                result.AddEnum(enumValue.Name);
            }

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

            return result;
        }
コード例 #6
0
        private static JsonSchema ParseCompositeType(Property property, CompositeType compositeType, IDictionary<string, JsonSchema> definitions, IEnumerable<CompositeType> modelTypes)
        {
            string definitionName = compositeType.Name;

            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.Type, definitions, modelTypes);
                    if (subPropertyDefinition != null)
                    {
                        definition.AddProperty(subProperty.Name, 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.Type, definitions, modelTypes);
                                    if (subTypePropertyDefinition != null)
                                    {
                                        definition.AddProperty(subTypeProperty.Name, 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)),
                                compositeType.Name);
                            throw new NotSupportedException(errorMessage);
                        }
                    }
                }
            }

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

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

            return result;
        }
コード例 #7
0
        private static JsonSchema ParsePrimaryType(Property property, PrimaryType primaryType)
        {
            JsonSchema result = new JsonSchema()
            {
                Format = primaryType.Format
            };

            switch (primaryType.KnownPrimaryType)
            {
            case KnownPrimaryType.Boolean:
                result.JsonType = "boolean";
                break;

            case KnownPrimaryType.Int:
            case KnownPrimaryType.Long:
                result.JsonType = "integer";
                break;

            case KnownPrimaryType.Double:
            case KnownPrimaryType.Decimal:
                result.JsonType = "number";
                break;

            case KnownPrimaryType.Object:
                result.JsonType = "object";
                break;

            case KnownPrimaryType.ByteArray:
                result.JsonType = "array";
                result.Items    = new JsonSchema()
                {
                    JsonType = "integer"
                };
                break;

            case KnownPrimaryType.Base64Url:
            case KnownPrimaryType.Date:
            case KnownPrimaryType.DateTime:
            case KnownPrimaryType.String:
            case KnownPrimaryType.TimeSpan:
                result.JsonType = "string";
                break;

            case KnownPrimaryType.Uuid:
                result.JsonType = "string";
                result.Pattern  = @"^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$";
                break;

            default:
                Debug.Assert(false, "Unrecognized known property type: " + primaryType.KnownPrimaryType);
                break;
            }

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

                if (property.DefaultValue != null)
                {
                    result.AddEnum(property.DefaultValue);
                }

                if (property.Constraints.Count > 0)
                {
                    foreach (KeyValuePair <Constraint, string> entry in property.Constraints)
                    {
                        switch (entry.Key)
                        {
                        case Constraint.InclusiveMinimum:
                            Debug.Assert(result.JsonType == "integer" || result.JsonType == "number", "Expected to only find an InclusiveMinimum constraint on an integer or number property.");
                            result.Minimum = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                            break;

                        case Constraint.InclusiveMaximum:
                            Debug.Assert(result.JsonType == "integer" || result.JsonType == "number", "Expected to only find an InclusiveMaximum constraint on an integer or number property.");
                            result.Maximum = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                            break;

                        case Constraint.Pattern:
                            Debug.Assert(result.JsonType == "string", "Expected to only find a Pattern constraint on a string property.");
                            result.Pattern = entry.Value;
                            break;

                        case Constraint.MinLength:
                            Debug.Assert(result.JsonType == "string" || result.JsonType == "array", "Expected to only find a MinLength constraint on a string or array property.");
                            result.MinLength = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                            break;

                        case Constraint.MaxLength:
                            Debug.Assert(result.JsonType == "string" || result.JsonType == "array", "Expected to only find a MaxLength constraint on a string or array property.");
                            result.MaxLength = Double.Parse(entry.Value, CultureInfo.CurrentCulture);
                            break;

                        default:
                            Debug.Fail("Unrecognized property Constraint: " + entry.Key);
                            break;
                        }
                    }
                }
            }

            return(result);
        }
コード例 #8
0
        private static JsonSchema ParseCompositeType(Property property, CompositeType compositeType, bool includeBaseModelTypeProperties, IDictionary <string, JsonSchema> definitions, IEnumerable <CompositeType> modelTypes)
        {
            string definitionName = compositeType.SerializedName;

            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);

                JsonSchema baseTypeDefinition;

                string discriminatorPropertyName = compositeType.PolymorphicDiscriminator;
                if (string.IsNullOrWhiteSpace(discriminatorPropertyName))
                {
                    baseTypeDefinition = definition;
                }
                else
                {
                    baseTypeDefinition = new JsonSchema();
                    definition.AddAllOf(baseTypeDefinition);

                    JsonSchema derivedTypeDefinitionRefs = new JsonSchema();

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

                        foreach (CompositeType subType in subTypes)
                        {
                            if (subType != null)
                            {
                                // Sub-types are never referenced directly in the Swagger
                                // discriminator scenario, so they wouldn't be added to the
                                // produced resource schema. By calling ParseCompositeType() on the
                                // sub-type we add the sub-type to the resource schema.
                                ParseCompositeType(null, subType, false, definitions, modelTypes);

                                derivedTypeDefinitionRefs.AddAnyOf(new JsonSchema()
                                {
                                    Ref = "#/definitions/" + subType.SerializedName,
                                });

                                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);
                                    }
                                }
                            }
                        }

                        baseTypeDefinition.AddProperty(discriminatorPropertyName, discriminatorDefinition);

                        definition.AddAllOf(derivedTypeDefinitionRefs);
                    }
                }

                IEnumerable <Property> compositeTypeProperties = includeBaseModelTypeProperties ? compositeType.ComposedProperties : compositeType.Properties;
                foreach (Property subProperty in compositeTypeProperties)
                {
                    JsonSchema subPropertyDefinition = ParseType(subProperty, subProperty.ModelType, definitions, modelTypes);
                    if (subPropertyDefinition != null)
                    {
                        baseTypeDefinition.AddProperty(subProperty.SerializedName.Else(subProperty.Name.RawValue), subPropertyDefinition, subProperty.IsRequired);
                    }
                }
            }

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

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

            return(result);
        }
コード例 #9
0
        private static JsonSchema ParseCompositeType(Property property, CompositeType compositeType, IDictionary <string, JsonSchema> definitions, IEnumerable <CompositeType> modelTypes)
        {
            string definitionName = compositeType.Name;

            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.Type, definitions, modelTypes);
                    if (subPropertyDefinition != null)
                    {
                        definition.AddProperty(subProperty.Name, 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.Type, definitions, modelTypes);
                                    if (subTypePropertyDefinition != null)
                                    {
                                        definition.AddProperty(subTypeProperty.Name, 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)),
                                compositeType.Name);
                            throw new NotSupportedException(errorMessage);
                        }
                    }
                }
            }

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

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

            return(result);
        }
コード例 #10
0
ファイル: JsonSchema.cs プロジェクト: jhancock93/autorest
 public static JsonSchema CreateStringEnum(string enumValue, params string[] extraEnumValues)
 {
     JsonSchema result = new JsonSchema() { JsonType = "string" };
     result.AddEnum(enumValue);
     if (extraEnumValues != null)
     {
         foreach (string extraEnumValue in extraEnumValues)
         {
             result.AddEnum(extraEnumValue);
         }
     }
     return result;
 }