示例#1
0
        public static void PopulateParameter(IParameter parameter, SwaggerObject swaggerObject)
        {
            if (swaggerObject == null)
            {
                throw new ArgumentNullException("swaggerObject");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }
            parameter.IsRequired = swaggerObject.IsRequired;
            parameter.DefaultValue = swaggerObject.Default;

            if (swaggerObject.Enum != null
                && swaggerObject.Enum.Count == 1
                && !IsExpandableEnum(swaggerObject)
                && swaggerObject.IsRequired)
            {
                parameter.DefaultValue = swaggerObject.Enum[0];
                parameter.IsConstant = true;
            }

            var compositeType = parameter.Type as CompositeType;
            if (compositeType != null && compositeType.ComposedProperties.All(p => p.IsConstant))
            {
                parameter.DefaultValue = "{}";
                parameter.IsConstant = true;
            }

            parameter.Documentation = swaggerObject.Description;
            parameter.CollectionFormat = swaggerObject.CollectionFormat;
            var enumType = parameter.Type as EnumType;
            if (enumType != null)
            {
                if (parameter.Documentation == null)
                {
                    parameter.Documentation = string.Empty;
                }
                else
                {
                    parameter.Documentation = parameter.Documentation.TrimEnd('.') + ". ";
                }
                parameter.Documentation += "Possible values include: " +
                                           string.Join(", ", enumType.Values.Select(v =>
                                               string.Format(CultureInfo.InvariantCulture,
                                               "'{0}'", v.Name)));
            }
            swaggerObject.Extensions.ForEach(e => parameter.Extensions[e.Key] = e.Value);

            SetConstraints(parameter.Constraints, swaggerObject);
        }
示例#2
0
 public ObjectBuilder(SwaggerObject swaggerObject, SwaggerModeler modeler)
 {
     SwaggerObject = swaggerObject;
     Modeler = modeler;
 }
示例#3
0
        public static void SetConstraints(Dictionary<Constraint, string> constraints, SwaggerObject swaggerObject)
        {
            if (constraints == null)
            {
                throw new ArgumentNullException("constraints");
            }
            if (swaggerObject == null)
            {
                throw new ArgumentNullException("swaggerObject");
            }

            if (!string.IsNullOrEmpty(swaggerObject.Maximum) && !swaggerObject.ExclusiveMaximum)
            {
                constraints[Constraint.InclusiveMaximum] = swaggerObject.Maximum;
            }
            if (!string.IsNullOrEmpty(swaggerObject.Maximum) && swaggerObject.ExclusiveMaximum)
            {
                constraints[Constraint.ExclusiveMaximum] = swaggerObject.Maximum;
            }
            if (!string.IsNullOrEmpty(swaggerObject.Minimum) && !swaggerObject.ExclusiveMinimum)
            {
                constraints[Constraint.InclusiveMinimum] = swaggerObject.Minimum;
            }
            if (!string.IsNullOrEmpty(swaggerObject.Minimum) && swaggerObject.ExclusiveMinimum)
            {
                constraints[Constraint.ExclusiveMinimum] = swaggerObject.Minimum;
            }
            if (!string.IsNullOrEmpty(swaggerObject.MaxLength))
            {
                constraints[Constraint.MaxLength] = swaggerObject.MaxLength;
            }
            if (!string.IsNullOrEmpty(swaggerObject.MinLength))
            {
                constraints[Constraint.MinLength] = swaggerObject.MinLength;
            }
            if (!string.IsNullOrEmpty(swaggerObject.Pattern))
            {
                constraints[Constraint.Pattern] = swaggerObject.Pattern;
            }
            if (!string.IsNullOrEmpty(swaggerObject.MaxItems))
            {
                constraints[Constraint.MaxItems] = swaggerObject.MaxItems;
            }
            if (!string.IsNullOrEmpty(swaggerObject.MinItems))
            {
                constraints[Constraint.MinItems] = swaggerObject.MinItems;
            }
            if (!string.IsNullOrEmpty(swaggerObject.MultipleOf))
            {
                constraints[Constraint.MultipleOf] = swaggerObject.MultipleOf;
            }
            if (swaggerObject.UniqueItems)
            {
                constraints[Constraint.UniqueItems] = "true";
            }
        }
示例#4
0
 private static bool IsExpandableEnum(SwaggerObject swaggerObject)
 {
     if (swaggerObject.Extensions.ContainsKey(CodeGenerator.EnumObject))
     {
         var enumObject = swaggerObject.Extensions[CodeGenerator.EnumObject] as Newtonsoft.Json.Linq.JContainer;
         if (enumObject != null)
         {
             if (enumObject["modelAsString"] != null)
             {
                 return bool.Parse(enumObject["modelAsString"].ToString());
             }
         }
     }
     return false;
 }
示例#5
0
 private static bool IsSwaggerObjectConstant(SwaggerObject swaggerObject)
 {
     return (swaggerObject.Enum != null && swaggerObject.Enum.Count == 1 && swaggerObject.IsRequired);
 }