Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        private static JsonSchema ParseEnumType(EnumType enumType)
        {
            JsonSchema result = new JsonSchema();

            result.JsonType = "string";
            foreach (EnumValue enumValue in enumType.Values)
            {
                result.AddEnum(enumValue.Name);
            }

            return(result);
        }
        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);
        }
        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;

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

            return(result);
        }
        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);
        }
Exemplo n.º 6
0
        private static JsonSchema ParseEnumType(EnumType enumType)
        {
            JsonSchema result = new JsonSchema();

            result.JsonType = "string";
            foreach (EnumValue enumValue in enumType.Values)
            {
                result.AddEnum(enumValue.Name);
            }

            return result;
        }
Exemplo n.º 7
0
        private static JsonSchema ParseProperty(Property property, IDictionary<string, JsonSchema> definitionMap)
        {
            JsonSchema propertyDefinition = null;

            if (!property.IsReadOnly)
            {
                propertyDefinition = new JsonSchema();

                IType propertyType = property.Type;

                CompositeType compositeType = propertyType as CompositeType;
                if (compositeType != null)
                {
                    propertyDefinition = ParseCompositeType(compositeType, definitionMap);
                    propertyDefinition.Description = property.Documentation;
                }
                else
                {
                    DictionaryType dictionaryType = propertyType as DictionaryType;
                    if (dictionaryType != null)
                    {
                        propertyDefinition.JsonType = "object";
                        propertyDefinition.Description = property.Documentation;

                        PrimaryType dictionaryPrimaryType = dictionaryType.ValueType as PrimaryType;
                        if (dictionaryPrimaryType != null)
                        {
                            propertyDefinition.AdditionalProperties = ParsePrimaryType(dictionaryPrimaryType);
                        }
                        else
                        {
                            CompositeType dictionaryCompositeType = dictionaryType.ValueType as CompositeType;
                            if (dictionaryCompositeType != null)
                            {
                                propertyDefinition.AdditionalProperties = ParseCompositeType(dictionaryCompositeType, definitionMap);
                            }
                            else
                            {
                                Debug.Assert(false, "Unrecognized DictionaryType.ValueType: " + dictionaryType.ValueType.GetType());
                            }
                        }
                    }
                    else
                    {
                        EnumType enumType = propertyType as EnumType;
                        if (enumType != null)
                        {
                            propertyDefinition = ParseEnumType(enumType);
                            propertyDefinition.Description = property.Documentation;
                        }
                        else
                        {
                            PrimaryType primaryType = propertyType as PrimaryType;
                            if (primaryType != null)
                            {
                                propertyDefinition = ParsePrimaryType(primaryType);
                                propertyDefinition.Description = property.Documentation;
                                propertyDefinition.Format = primaryType.Format;

                                if (property.DefaultValue != null)
                                {
                                    propertyDefinition.AddEnum(property.DefaultValue);
                                }
                            }
                            else
                            {
                                SequenceType sequenceType = propertyType as SequenceType;
                                if (sequenceType != null)
                                {
                                    propertyDefinition.JsonType = "array";
                                    propertyDefinition.Description = property.Documentation;

                                    IType sequenceElementType = sequenceType.ElementType;

                                    CompositeType sequenceCompositeType = sequenceElementType as CompositeType;
                                    if (sequenceCompositeType != null)
                                    {
                                        propertyDefinition.Items = ParseCompositeType(sequenceCompositeType, definitionMap);
                                    }
                                    else
                                    {
                                        PrimaryType sequencePrimaryType = sequenceElementType as PrimaryType;
                                        if (sequencePrimaryType != null)
                                        {
                                            propertyDefinition.Items = ParsePrimaryType(sequencePrimaryType);
                                        }
                                        else
                                        {
                                            EnumType sequenceEnumType = sequenceElementType as EnumType;
                                            if (sequenceEnumType != null)
                                            {
                                                propertyDefinition = ParseEnumType(sequenceEnumType);
                                            }
                                            else
                                            {
                                                Debug.Assert(false, "Unrecognized SequenceType.ElementType: " + sequenceType.ElementType.GetType());
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    Debug.Assert(false, "Unrecognized property type: " + propertyType.GetType());
                                }
                            }
                        }
                    }
                }
            }

            return propertyDefinition;
        }
Exemplo n.º 8
0
        private static JsonSchema ParseProperty(Property property, IDictionary <string, JsonSchema> definitionMap)
        {
            JsonSchema propertyDefinition = null;

            if (!property.IsReadOnly)
            {
                propertyDefinition = new JsonSchema();

                IType propertyType = property.Type;

                CompositeType compositeType = propertyType as CompositeType;
                if (compositeType != null)
                {
                    propertyDefinition             = ParseCompositeType(compositeType, definitionMap);
                    propertyDefinition.Description = property.Documentation;
                }
                else
                {
                    DictionaryType dictionaryType = propertyType as DictionaryType;
                    if (dictionaryType != null)
                    {
                        propertyDefinition.JsonType    = "object";
                        propertyDefinition.Description = property.Documentation;

                        PrimaryType dictionaryPrimaryType = dictionaryType.ValueType as PrimaryType;
                        if (dictionaryPrimaryType != null)
                        {
                            propertyDefinition.AdditionalProperties = ParsePrimaryType(dictionaryPrimaryType);
                        }
                        else
                        {
                            CompositeType dictionaryCompositeType = dictionaryType.ValueType as CompositeType;
                            if (dictionaryCompositeType != null)
                            {
                                propertyDefinition.AdditionalProperties = ParseCompositeType(dictionaryCompositeType, definitionMap);
                            }
                            else
                            {
                                Debug.Assert(false, "Unrecognized DictionaryType.ValueType: " + dictionaryType.ValueType.GetType());
                            }
                        }
                    }
                    else
                    {
                        EnumType enumType = propertyType as EnumType;
                        if (enumType != null)
                        {
                            propertyDefinition             = ParseEnumType(enumType);
                            propertyDefinition.Description = property.Documentation;
                        }
                        else
                        {
                            PrimaryType primaryType = propertyType as PrimaryType;
                            if (primaryType != null)
                            {
                                propertyDefinition             = ParsePrimaryType(primaryType);
                                propertyDefinition.Description = property.Documentation;
                                propertyDefinition.Format      = primaryType.Format;

                                if (property.DefaultValue != null)
                                {
                                    propertyDefinition.AddEnum(property.DefaultValue);
                                }
                            }
                            else
                            {
                                SequenceType sequenceType = propertyType as SequenceType;
                                if (sequenceType != null)
                                {
                                    propertyDefinition.JsonType    = "array";
                                    propertyDefinition.Description = property.Documentation;

                                    IType sequenceElementType = sequenceType.ElementType;

                                    CompositeType sequenceCompositeType = sequenceElementType as CompositeType;
                                    if (sequenceCompositeType != null)
                                    {
                                        propertyDefinition.Items = ParseCompositeType(sequenceCompositeType, definitionMap);
                                    }
                                    else
                                    {
                                        PrimaryType sequencePrimaryType = sequenceElementType as PrimaryType;
                                        if (sequencePrimaryType != null)
                                        {
                                            propertyDefinition.Items = ParsePrimaryType(sequencePrimaryType);
                                        }
                                        else
                                        {
                                            EnumType sequenceEnumType = sequenceElementType as EnumType;
                                            if (sequenceEnumType != null)
                                            {
                                                propertyDefinition = ParseEnumType(sequenceEnumType);
                                            }
                                            else
                                            {
                                                Debug.Assert(false, "Unrecognized SequenceType.ElementType: " + sequenceType.ElementType.GetType());
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    Debug.Assert(false, "Unrecognized property type: " + propertyType.GetType());
                                }
                            }
                        }
                    }
                }
            }

            return(propertyDefinition);
        }