Exemplo n.º 1
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                ?? _jsonSerializerSettings.Converters.OfType<StringEnumConverter>().FirstOrDefault();

            if (_settings.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _settings.DescribeStringEnumsInCamelCase
                    || (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                return new Schema
                {
                    Type = "string",
                    Enum = (camelCase)
                        ? Enum.GetNames(type).Select(name => name.ToCamelCase()).ToArray()
                        : Enum.GetNames(type)
                };
            }

            return new Schema
            {
                Type = "integer",
                Format = "int32",
                Enum = Enum.GetValues(type).Cast<object>().ToArray()
            };
        }
Exemplo n.º 2
0
        private string method_35(JsonWriter jsonWriter_0, DictionaryEntry dictionaryEntry_0, JsonContract jsonContract_1, out bool bool_1)
        {
            string str;
            object key = dictionaryEntry_0.Key;
            JsonPrimitiveContract contract = jsonContract_1 as JsonPrimitiveContract;

            if (contract != null)
            {
                if ((contract.Enum17_0 != Enum17.DateTime) && (contract.Enum17_0 != Enum17.DateTimeNullable))
                {
                    if ((contract.Enum17_0 != Enum17.DateTimeOffset) && (contract.Enum17_0 != Enum17.DateTimeOffsetNullable))
                    {
                        bool_1 = true;
                        return(Convert.ToString(key, CultureInfo.InvariantCulture));
                    }
                    bool_1 = false;
                    StringWriter writer2 = new StringWriter(CultureInfo.InvariantCulture);
                    Class184.smethod_21(writer2, (DateTimeOffset)key, jsonWriter_0.DateFormatHandling, jsonWriter_0.DateFormatString, jsonWriter_0.Culture);
                    return(writer2.ToString());
                }
                bool_1 = false;
                StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
                Class184.smethod_16(writer, (DateTime)key, jsonWriter_0.DateFormatHandling, jsonWriter_0.DateFormatString, jsonWriter_0.Culture);
                return(writer.ToString());
            }
            if (smethod_0(key, key.GetType(), out str))
            {
                bool_1 = true;
                return(str);
            }
            bool_1 = true;
            return(key.ToString());
        }
Exemplo n.º 3
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            StringEnumConverter stringEnumConverter = (primitiveContract.Converter as StringEnumConverter) ?? this._jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault <StringEnumConverter>();

            if (this._describeAllEnumsAsStrings || stringEnumConverter != null)
            {
                bool   flag   = this._describeStringEnumsInCamelCase || (stringEnumConverter != null && stringEnumConverter.CamelCaseText);
                Schema schema = new Schema();
                schema.type = "string";
                Schema         arg_92_0 = schema;
                IList <object> arg_92_1;
                if (!flag)
                {
                    arg_92_1 = type.GetEnumNamesForSerialization();
                }
                else
                {
                    arg_92_1 = (from name in type.GetEnumNamesForSerialization()
                                select name.ToCamelCase()).ToArray <string>();
                }
                arg_92_0.@enum = arg_92_1;
                return(schema);
            }
            return(new Schema
            {
                type = "integer",
                format = "int32",
                @enum = type.GetEnumValues().Cast <object>().ToArray <object>()
            });
        }
Exemplo n.º 4
0
        public Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            if (_options.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _options.DescribeStringEnumsInCamelCase ||
                                (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                var enumValues = type.GetEnumNamesForSerialization(_options.IgnoreObsoleteEnumConstants);

                return(new Schema
                {
                    type = "string",
                    @enum = camelCase
                        ? enumValues.Select(name => name.ToCamelCase()).ToArray()
                        : enumValues
                });
            }

            return(new Schema
            {
                type = "integer",
                format = "int32",
                @enum = type.GetEnumValuesForSerialization(_options.IgnoreObsoleteEnumConstants)
            });
        }
Exemplo n.º 5
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType)
                       ?? primitiveContract.UnderlyingType;

            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                var converter        = primitiveContract.Converter;
                var describeAsString = _options.DescribeAllEnumsAsStrings ||
                                       (converter != null && converter.GetType() == typeof(StringEnumConverter));

                return(describeAsString
                    ? new Schema {
                    Type = "string", Enum = Enum.GetNames(type)
                }
                    : new Schema {
                    Type = "integer", Format = "int32", Enum = Enum.GetValues(type).Cast <object>().ToArray()
                });
            }

            if (PrimitiveTypeMap.ContainsKey(type))
            {
                return(PrimitiveTypeMap[type]());
            }

            // None of the above, fallback to string
            return(new Schema {
                Type = "string"
            });
        }
Exemplo n.º 6
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            if (_settings.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _settings.DescribeStringEnumsInCamelCase ||
                                (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                var enumNames = type.GetFields(BindingFlags.Public | BindingFlags.Static)
                                .Select(f =>
                {
                    var enumMemberAttribute = f.GetCustomAttributes().OfType <EnumMemberAttribute>().FirstOrDefault();
                    var serializeName       = (enumMemberAttribute == null) ? f.Name : enumMemberAttribute.Value;
                    return(camelCase ? serializeName.ToCamelCase() : serializeName);
                });

                return(new Schema
                {
                    Type = "string",
                    Enum = enumNames.ToArray()
                });
            }

            return(new Schema
            {
                Type = "integer",
                Format = "int32",
                Enum = Enum.GetValues(type).Cast <object>().ToArray()
            });
        }
Exemplo n.º 7
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            if (_describeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _describeStringEnumsInCamelCase ||
                                (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                return(new Schema
                {
                    type = "string",
                    @enum = camelCase
                        ? type.GetEnumNamesForSerialization().Select(name => name.ToCamelCase()).ToArray()
                        : type.GetEnumNamesForSerialization()
                });
            }

            return(new Schema
            {
                type = "integer",
                format = "int32",
                @enum = type.GetEnumValues().Cast <object>().ToArray()
            });
        }
Exemplo n.º 8
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
            {
                return(CreateEnumSchema(primitiveContract, type));
            }

            switch (type.FullName)
            {
            case "System.Int16":
            case "System.UInt16":
            case "System.Int32":
            case "System.UInt32":
                return(new Schema {
                    type = "integer", format = "int32"
                });

            case "System.Int64":
            case "System.UInt64":
                return(new Schema {
                    type = "integer", format = "int64"
                });

            case "System.Single":
                return(new Schema {
                    type = "number", format = "float"
                });

            case "System.Double":
            case "System.Decimal":
                return(new Schema {
                    type = "number", format = "double"
                });

            case "System.Byte":
            case "System.SByte":
                return(new Schema {
                    type = "string", format = "byte"
                });

            case "System.Boolean":
                return(new Schema {
                    type = "boolean"
                });

            case "System.DateTime":
            case "System.DateTimeOffset":
                return(new Schema {
                    type = "string", format = "date-time"
                });

            default:
                return(new Schema {
                    type = "string"
                });
            }
        }
 public static JsonPrimitiveContract Copy(this JsonPrimitiveContract source) =>
 new JsonPrimitiveContract(source.UnderlyingType)
 {
     Converter               = source.Converter,
     CreatedType             = source.CreatedType,
     DefaultCreator          = source.DefaultCreator,
     DefaultCreatorNonPublic = source.DefaultCreatorNonPublic,
     IsReference             = source.IsReference
 };
Exemplo n.º 10
0
        public void CreateDefinitionSchema_Null()
        {
            var contract = new JsonPrimitiveContract(typeof(int));
            var mock     = new Mock <JsonSerializerSettings>();

            var schema = new SchemaRegistry(mock.Object, new SwaggerGeneratorOptions());

            Assert.Throws <InvalidOperationException>(() => schema.CreateDefinitionSchema(contract));
        }
Exemplo n.º 11
0
        private OpenApiSchema GenerateEnumSchema(Type enumType, JsonPrimitiveContract jsonPrimitiveContract)
        {
            var stringEnumConverter = (jsonPrimitiveContract.Converter as StringEnumConverter)
                                      ?? JsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            if (SchemaGeneratorOptions.DescribeAllEnumsAsStrings || (stringEnumConverter != null))
            {
                var describeInCamelCase = SchemaGeneratorOptions.DescribeStringEnumsInCamelCase
                    #if NETCOREAPP3_0
                                          || (stringEnumConverter != null && stringEnumConverter.NamingStrategy is CamelCaseNamingStrategy);
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            var enumDesc = new System.Text.StringBuilder();

            if (_options.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _options.DescribeStringEnumsInCamelCase ||
                                (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                var enumNames = type.GetFields(BindingFlags.Public | BindingFlags.Static)
                                .Select(f =>
                {
                    var name = f.Name;

                    var enumMemberAttribute = f.GetCustomAttributes().OfType <EnumMemberAttribute>().FirstOrDefault();
                    if (enumMemberAttribute != null && enumMemberAttribute.Value != null)
                    {
                        name = enumMemberAttribute.Value;
                    }
                    name = camelCase ? name.ToCamelCase() : name;
                    if (Attribute.GetCustomAttribute(f, typeof(DescriptionAttribute)) is DescriptionAttribute descAttr)
                    {
                        enumDesc.Append($"<br/>{name}:{descAttr.Description}");
                    }
                    return(name);
                });

                return(new Schema
                {
                    Type = "string",
                    Description = enumDesc.ToString(),
                    Enum = enumNames.ToArray()
                });
            }
            else
            {
                type.GetFields(BindingFlags.Public | BindingFlags.Static).ToList().ForEach(f =>
                {
                    if (Attribute.GetCustomAttribute(f, typeof(DescriptionAttribute)) is DescriptionAttribute descAttr)
                    {
                        enumDesc.Append($"<br/>{f.GetValue(null)}:{descAttr.Description}");
                    }
                });
                return(new Schema
                {
                    Type = "integer",
                    Format = "int32",
                    Description = enumDesc.ToString(),
                    Enum = Enum.GetValues(type).Cast <object>().ToArray()
                });
            }
        }
        public void CreateEnumSchema_empty()
        {
            var contract = new JsonPrimitiveContract(typeof(int));
            var mock     = new Mock <JsonSerializerSettings>();
            var opt      = new SwaggerGeneratorOptions();

            var schema = new SchemaRegistry(mock.Object, opt);
            var enu    = schema.CreateEnumSchema(contract, typeof(int));

            Assert.IsNotNull(enu);
        }
Exemplo n.º 14
0
 /// <inheritdoc />
 public void Handle(IJsonContractRequest request)
 {
     if (request.dataType.IsEnum && !Attribute.IsDefined(request.dataType, typeof(FlagsAttribute)))
     {
         var converter = new EnumJsonConverter(request.dataType);
         var contract  = new JsonPrimitiveContract(request.dataType)
         {
             Converter = converter
         };
         request.Return(contract);
     }
 }
Exemplo n.º 15
0
 // Token: 0x0600098B RID: 2443 RVA: 0x00036B98 File Offset: 0x00034D98
 private void method_8(JsonWriter jsonWriter_0, object object_0, JsonPrimitiveContract jsonPrimitiveContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
 {
     if (jsonPrimitiveContract_0.method_5() == (Enum3)40 && this.method_36(TypeNameHandling.Objects, jsonPrimitiveContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1))
     {
         jsonWriter_0.WriteStartObject();
         this.method_24(jsonWriter_0, jsonPrimitiveContract_0.CreatedType);
         jsonWriter_0.WritePropertyName("$value", false);
         JsonWriter.smethod_2(jsonWriter_0, jsonPrimitiveContract_0.method_5(), object_0);
         jsonWriter_0.WriteEndObject();
         return;
     }
     JsonWriter.smethod_2(jsonWriter_0, jsonPrimitiveContract_0.method_5(), object_0);
 }
        public void CreateEnumSchema_camelCase()
        {
            var contract = new JsonPrimitiveContract(typeof(int));
            var mock     = new Mock <JsonSerializerSettings>();
            var opt      = new SwaggerGeneratorOptions(
                describeAllEnumsAsStrings: true,
                describeStringEnumsInCamelCase: true
                );

            var schema = new SchemaRegistry(mock.Object, opt);
            var enu    = schema.CreateEnumSchema(contract, typeof(int));

            Assert.IsNotNull(enu);
        }
Exemplo n.º 17
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType)
                ?? primitiveContract.UnderlyingType;

            if (type.GetTypeInfo().IsEnum)
                return CreateEnumSchema(primitiveContract, type);

            if (PrimitiveTypeMap.ContainsKey(type))
                return PrimitiveTypeMap[type]();

            // None of the above, fallback to string
            return new Schema { Type = "string" };
        }
Exemplo n.º 18
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            var camelCase = _describeStringEnumsInCamelCase ||
                            (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

            string[] names = type.GetEnumNamesForSerialization().ToArray();

            if (camelCase)
            {
                names = names.Select(name => name.ToCamelCase()).ToArray();
            }

            var textEnumSchema = new Schema
            {
                type  = "string",
                @enum = names
            };

            var values = type.GetEnumValues().Cast <int>().ToArray();

            var intEnumSchema = new Schema
            {
                type   = "integer",
                format = "int32",
                @enum  = values.Cast <object>().ToArray()
            };

            var namesAndValuesAreSame = textEnumSchema.@enum == [email protected] <string>();

            if (_describeAllEnumsAsStrings || stringEnumConverter != null)
            {
                if (!namesAndValuesAreSame)
                {
                    textEnumSchema.vendorExtensions.Add("x-values", intEnumSchema.@enum);
                }

                return(textEnumSchema);
            }

            if (!namesAndValuesAreSame)
            {
                intEnumSchema.vendorExtensions.Add("x-names", textEnumSchema.@enum);
            }

            return(intEnumSchema);
        }
Exemplo n.º 19
0
 private void method_6(JsonWriter jsonWriter_0, object object_0, JsonPrimitiveContract jsonPrimitiveContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
 {
     if ((jsonPrimitiveContract_0.Enum17_0 == Enum17.Bytes) && this.method_33(TypeNameHandling.Objects, jsonPrimitiveContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1))
     {
         jsonWriter_0.WriteStartObject();
         this.method_21(jsonWriter_0, jsonPrimitiveContract_0.CreatedType);
         jsonWriter_0.WritePropertyName("$value", false);
         JsonWriter.smethod_3(jsonWriter_0, jsonPrimitiveContract_0.Enum17_0, object_0);
         jsonWriter_0.WriteEndObject();
     }
     else
     {
         JsonWriter.smethod_3(jsonWriter_0, jsonPrimitiveContract_0.Enum17_0, object_0);
     }
 }
Exemplo n.º 20
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            Type index = primitiveContract.UnderlyingType.IsNullable()
                ? Nullable.GetUnderlyingType(primitiveContract.UnderlyingType)
                : primitiveContract.UnderlyingType;

            if (index.GetTypeInfo().IsEnum)
            {
                return(this.CreateEnumSchema(primitiveContract, index));
            }
            if (AsyncApiSchemaRegistry.PrimitiveTypeMap.ContainsKey(index))
            {
                return(AsyncApiSchemaRegistry.PrimitiveTypeMap[index]());
            }
            return(new Schema()
            {
                Type = "string"
            });
        }
        private OpenApiSchema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            if (_options.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _options.DescribeStringEnumsInCamelCase ||
                                (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                var enumNames = type.GetFields(BindingFlags.Public | BindingFlags.Static)
                                .Select(f =>
                {
                    var name = f.Name;

                    var enumMemberAttribute = f.GetCustomAttributes().OfType <EnumMemberAttribute>().FirstOrDefault();
                    if (enumMemberAttribute != null && enumMemberAttribute.Value != null)
                    {
                        name = enumMemberAttribute.Value;
                    }

                    return(camelCase ? name.ToCamelCase() : name);
                });

                return(new OpenApiSchema
                {
                    Type = "string",
                    Enum = enumNames
                           .Select(name => new OpenApiString(name))
                           .ToList <IOpenApiAny>()
                });
            }

            return(new OpenApiSchema
            {
                Type = "integer",
                Format = "int32",
                Enum = Enum.GetValues(type).Cast <int>()
                       .Select(value => new OpenApiInteger(value))
                       .ToList <IOpenApiAny>()
            });
        }
        private OpenApiSchema GenerateEnumSchema(JsonPrimitiveContract jsonPrimitiveContract)
        {
            var stringEnumConverter = (jsonPrimitiveContract.Converter as StringEnumConverter)
                                      ?? _serializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            var describeAsString = Options.DescribeAllEnumsAsStrings ||
                                   (stringEnumConverter != null);

            var describeInCamelCase = Options.DescribeStringEnumsInCamelCase ||
                                      (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

            var enumType           = jsonPrimitiveContract.UnderlyingType;
            var enumUnderlyingType = describeAsString ? typeof(string) : enumType.GetEnumUnderlyingType();

            var schema = FactoryMethodMap[enumUnderlyingType]();

            if (describeAsString)
            {
                schema.Enum = enumType.GetEnumNames()
                              .Distinct()
                              .Select(name =>
                {
                    name = describeInCamelCase ? name.ToCamelCase() : name;
                    return((IOpenApiAny)(new OpenApiString(name)));
                })
                              .ToList();
            }
            else
            {
                schema.Enum = enumType.GetEnumValues()
                              .Cast <object>()
                              .Distinct()
                              .Select(value =>
                {
                    value = Convert.ChangeType(value, enumUnderlyingType);
                    return(OpenApiAnyFactory.TryCreateFor(schema, value, out IOpenApiAny openApiAny) ? openApiAny : null);
                })
                              .ToList();
            }

            return(schema);
        }
Exemplo n.º 23
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            StringEnumConverter stringEnumConverter = primitiveContract.Converter as StringEnumConverter ??
                                                      this._jsonSerializerSettings.Converters
                                                      .OfType <StringEnumConverter>()
                                                      .FirstOrDefault <StringEnumConverter>();

            if (this._options.DescribeAllEnumsAsStrings || stringEnumConverter != null)
            {
                bool camelCase = this._options.DescribeStringEnumsInCamelCase ||
                                 stringEnumConverter != null && stringEnumConverter.CamelCaseText;
                IEnumerable <string> source =
                    ((IEnumerable <FieldInfo>)type.GetFields(BindingFlags.Static | BindingFlags.Public))
                    .Select <FieldInfo, string>((Func <FieldInfo, string>)(f =>
                {
                    string name = f.Name;
                    EnumMemberAttribute enumMemberAttribute = f.GetCustomAttributes().OfType <EnumMemberAttribute>()
                                                              .FirstOrDefault <EnumMemberAttribute>();
                    if (enumMemberAttribute != null && enumMemberAttribute.Value != null)
                    {
                        name = enumMemberAttribute.Value;
                    }
                    if (!camelCase)
                    {
                        return(name);
                    }
                    return(name.ToCamelCase());
                }));
                return(new Schema()
                {
                    Type = "string",
                    Enum = source.ToArray()
                });
            }

            return(new Schema()
            {
                Type = "integer",
                Format = "int32",
                Enum = (IList <object>)Enum.GetValues(type).Cast <object>().ToArray <object>()
            });
        }
Exemplo n.º 24
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            StringEnumConverter stringEnumConverter = ((JsonContract)primitiveContract).get_Converter() as StringEnumConverter ?? ((IEnumerable)this._jsonSerializerSettings.get_Converters()).OfType <StringEnumConverter>().FirstOrDefault <StringEnumConverter>();

            if (this._describeAllEnumsAsStrings || stringEnumConverter != null)
            {
                bool flag = this._describeStringEnumsInCamelCase || stringEnumConverter != null && stringEnumConverter.get_CamelCaseText();
                return(new Schema()
                {
                    type = "string",
                    @enum = flag ? (IList <object>)((IEnumerable <string>)type.GetEnumNamesForSerialization()).Select <string, string>((Func <string, string>)(name => name.ToCamelCase())).ToArray <string>() : (IList <object>)type.GetEnumNamesForSerialization()
                });
            }
            return(new Schema()
            {
                type = "integer",
                format = "int32",
                @enum = (IList <object>)type.GetEnumValues().Cast <object>().ToArray <object>()
            });
        }
Exemplo n.º 25
0
        private ApiModel ResolveApiPrimitive(JsonPrimitiveContract jsonPrimitiveContract)
        {
            var type = jsonPrimitiveContract.UnderlyingType;

            if (!type.IsEnum)
            {
                return(new ApiPrimitive(type));
            }

            var stringEnumConverter = (jsonPrimitiveContract.Converter as StringEnumConverter)
                                      ?? _jsonSerializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            // Temporary shim to support obsolete config options
            if (stringEnumConverter == null && _options.DescribeAllEnumsAsStrings)
            {
                stringEnumConverter = new StringEnumConverter(_options.DescribeStringEnumsInCamelCase);
            }
            ;

            if (stringEnumConverter == null)
            {
                return(new ApiPrimitive(
                           type: type,
                           isStringEnum: false,
                           apiEnumValues: type.GetEnumValues().Cast <object>()));
            }

            var enumValues = type.GetFields(BindingFlags.Public | BindingFlags.Static)
                             .Select(field =>
            {
                var enumMemberAttribute = field.GetCustomAttributes <EnumMemberAttribute>().FirstOrDefault();
                var memberName          = enumMemberAttribute?.Value ?? field.Name;
                return(GetConvertedEnumName(memberName, stringEnumConverter));
            })
                             .Distinct();

            return(new ApiPrimitive(
                       type: type,
                       isStringEnum: true,
                       apiEnumValues: enumValues));
        }
Exemplo n.º 26
0
    private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
    {
      if (contract.TypeCode == PrimitiveTypeCode.Bytes)
      {
        // if type name handling is enabled then wrap the base64 byte string in an object with the type name
        bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
        if (includeTypeDetails)
        {
          writer.WriteStartObject();
          WriteTypeProperty(writer, contract.CreatedType);
          writer.WritePropertyName(JsonTypeReflector.ValuePropertyName, false);

          JsonWriter.WriteValue(writer, contract.TypeCode, value);

          writer.WriteEndObject();
          return;
        }
      }

      JsonWriter.WriteValue(writer, contract.TypeCode, value);
    }
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            // If Nullable<T>, use the type argument
            var type = primitiveContract.UnderlyingType.IsNullable()
                ? Nullable.GetUnderlyingType(primitiveContract.UnderlyingType)
                : primitiveContract.UnderlyingType;

            if (type.GetTypeInfo().IsEnum)
            {
                return(CreateEnumSchema(primitiveContract, type));
            }

            if (PrimitiveTypeMap.ContainsKey(type))
            {
                return(PrimitiveTypeMap[type]());
            }

            // None of the above, fallback to string
            return(new Schema {
                Type = "string"
            });
        }
Exemplo n.º 28
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
            {
                var converter        = primitiveContract.Converter;
                var describeAsString = _describeAllEnumsAsStrings ||
                                       (converter != null && converter.GetType() == typeof(StringEnumConverter));

                return(describeAsString
                    ? new Schema {
                    type = "string", @enum = type.GetEnumNames()
                }
                    : new Schema {
                    type = "integer", format = "int32", @enum = type.GetEnumValues().Cast <object>().ToArray()
                });
            }

            switch (type.FullName)
            {
            case "System.Int16":
            case "System.UInt16":
            case "System.Int32":
            case "System.UInt32":
                return(new Schema {
                    type = "integer", format = "int32"
                });

            case "System.Int64":
            case "System.UInt64":
                return(new Schema {
                    type = "integer", format = "int64"
                });

            case "System.Single":
                return(new Schema {
                    type = "number", format = "float"
                });

            case "System.Double":
            case "System.Decimal":
                return(new Schema {
                    type = "number", format = "double"
                });

            case "System.Byte":
            case "System.SByte":
                return(new Schema {
                    type = "string", format = "byte"
                });

            case "System.Boolean":
                return(new Schema {
                    type = "boolean"
                });

            case "System.DateTime":
            case "System.DateTimeOffset":
                return(new Schema {
                    type = "string", format = "date-time"
                });

            default:
                return(new Schema {
                    type = "string"
                });
            }
        }
Exemplo n.º 29
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            Schema schema;

            if (primitiveContract.PropertyInfo() != null && primitiveContract.PropertyInfo().CustomAttributes != null)
            {
                //var isIgnored = primitiveContract.PropertyInfo().CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(SwaggerIgnore));
                var isIgnored = primitiveContract.PropertyInfo().GetCustomAttribute <SwaggerIgnore>();
                if (isIgnored != null)
                {
                    return(null);
                }

                //var customRouteName = primitiveContract.PropertyInfo().GetCustomAttribute<SwaggerRouteName>();
                //if (customRouteName != null)
                //{
                //    //
                //}
            }

            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
            {
                return(CreateEnumSchema(primitiveContract, type));
            }

            switch (type.FullName)
            {
            case "System.Int16":
            case "System.UInt16":
            case "System.Int32":
            case "System.UInt32":
                schema = new Schema {
                    type = "integer", format = "int32"
                };
                break;

            case "System.Int64":
            case "System.UInt64":
                schema = new Schema {
                    type = "integer", format = "int64"
                };
                break;

            case "System.Single":
                schema = new Schema {
                    type = "number", format = "float"
                };
                break;

            case "System.Double":
            case "System.Decimal":
                schema = new Schema {
                    type = "number", format = "double"
                };
                break;

            case "System.Byte":
            case "System.SByte":
                schema = new Schema {
                    type = "string", format = "byte"
                };
                break;

            case "System.Boolean":
                schema = new Schema {
                    type = "boolean"
                };
                break;

            case "System.DateTime":
            case "System.DateTimeOffset":
                schema = new Schema {
                    type = "string", format = "date-time"
                };
                break;

            default:
                schema = new Schema {
                    type = "string"
                };
                break;
            }

            schema.WithValidationProperties(primitiveContract);

            return(schema);
        }
Exemplo n.º 30
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            Type underlyingType = Nullable.GetUnderlyingType(((JsonContract)primitiveContract).get_UnderlyingType());

            if ((object)underlyingType == null)
            {
                underlyingType = ((JsonContract)primitiveContract).get_UnderlyingType();
            }
            Type type = underlyingType;

            if (type.IsEnum)
            {
                return(this.CreateEnumSchema(primitiveContract, type));
            }
            switch (type.FullName)
            {
            case "System.Boolean":
                return(new Schema()
                {
                    type = "boolean"
                });

            case "System.Byte":
            case "System.SByte":
            case "System.Int16":
            case "System.UInt16":
            case "System.Int32":
            case "System.UInt32":
                return(new Schema()
                {
                    type = "integer",
                    format = "int32"
                });

            case "System.Int64":
            case "System.UInt64":
                return(new Schema()
                {
                    type = "integer",
                    format = "int64"
                });

            case "System.Single":
                return(new Schema()
                {
                    type = "number",
                    format = "float"
                });

            case "System.Double":
            case "System.Decimal":
                return(new Schema()
                {
                    type = "number",
                    format = "double"
                });

            case "System.Byte[]":
                return(new Schema()
                {
                    type = "string",
                    format = "byte"
                });

            case "System.DateTime":
            case "System.DateTimeOffset":
                return(new Schema()
                {
                    type = "string",
                    format = "date-time"
                });

            case "System.Guid":
                return(new Schema()
                {
                    type = "string",
                    format = "uuid",
                    example = (object)Guid.Empty
                });

            default:
                return(new Schema()
                {
                    type = "string"
                });
            }
        }
Exemplo n.º 31
0
    // Token: 0x060009AA RID: 2474 RVA: 0x00038324 File Offset: 0x00036524
    private string method_38(JsonWriter jsonWriter_0, object object_0, JsonContract jsonContract_0, out bool bool_0)
    {
        if (jsonContract_0.enum8_0 == (Enum8)3)
        {
            JsonPrimitiveContract jsonPrimitiveContract = (JsonPrimitiveContract)jsonContract_0;
            switch (jsonPrimitiveContract.method_5())
            {
            case (Enum3)22:
            case (Enum3)23:
            {
                float num = (float)object_0;
                bool_0 = false;
                return(num.ToString("R", CultureInfo.InvariantCulture));
            }

            case (Enum3)24:
            case (Enum3)25:
            {
                double num2 = (double)object_0;
                bool_0 = false;
                return(num2.ToString("R", CultureInfo.InvariantCulture));
            }

            case (Enum3)26:
            case (Enum3)27:
            {
                DateTime dateTime_ = Class25.smethod_2((DateTime)object_0, jsonWriter_0.DateTimeZoneHandling);
                bool_0 = false;
                StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                Class25.smethod_24(stringWriter, dateTime_, jsonWriter_0.DateFormatHandling, jsonWriter_0.DateFormatString, jsonWriter_0.Culture);
                return(stringWriter.ToString());
            }

            case (Enum3)28:
            case (Enum3)29:
            {
                bool_0 = false;
                StringWriter stringWriter2 = new StringWriter(CultureInfo.InvariantCulture);
                Class25.IxisRjgvuh(stringWriter2, (DateTimeOffset)object_0, jsonWriter_0.DateFormatHandling, jsonWriter_0.DateFormatString, jsonWriter_0.Culture);
                return(stringWriter2.ToString());
            }

            default:
            {
                bool_0 = true;
                string result;
                if (jsonPrimitiveContract.bool_2 && Class53.smethod_3(jsonPrimitiveContract.type_0, object_0, null, out result))
                {
                    return(result);
                }
                return(Convert.ToString(object_0, CultureInfo.InvariantCulture));
            }
            }
        }
        else
        {
            string result2;
            if (Class119.smethod_0(object_0, object_0.GetType(), out result2))
            {
                bool_0 = true;
                return(result2);
            }
            bool_0 = true;
            return(object_0.ToString());
        }
    }