Пример #1
0
        private OpenApiSchema GeneratePrimitiveSchema(SerializerContract serializerContract)
        {
            var schema = new OpenApiSchema
            {
                Type   = serializerContract.DataType,
                Format = serializerContract.DataFormat
            };

            if (serializerContract.EnumValues != null)
            {
                schema.Enum = serializerContract.EnumValues
                              .Select(value => OpenApiAnyFactory.CreateFor(schema, value))
                              .ToList();
            }

            return(schema);
        }
Пример #2
0
        private OpenApiSchema GeneratePrimitiveSchema(DataContract dataContract)
        {
            var schema = new OpenApiSchema
            {
                Type   = dataContract.DataType.ToString().ToLower(CultureInfo.InvariantCulture),
                Format = dataContract.DataFormat
            };

            if (dataContract.EnumValues != null)
            {
                schema.Enum = dataContract.EnumValues
                              .Distinct()
                              .Select(value => OpenApiAnyFactory.CreateFor(schema, value))
                              .ToList();
            }

            return(schema);
        }
Пример #3
0
        private static IOpenApiAny ConvertToOpenApiType(Type memberType, OpenApiSchema schema, string stringValue)
        {
            object typedValue;

            try
            {
                typedValue = TypeDescriptor.GetConverter(memberType).ConvertFrom(
                    context: null,
                    culture: CultureInfo.InvariantCulture,
                    stringValue);
            }
            catch (Exception)
            {
                return(null);
            }

            return(OpenApiAnyFactory.CreateFor(schema, typedValue));
        }
Пример #4
0
        private OpenApiSchema GenerateSchemaFromFormParameters(
            IEnumerable <ApiParameterDescription> formParameters,
            SchemaRepository schemaRepository)
        {
            var properties            = new Dictionary <string, OpenApiSchema>();
            var requiredPropertyNames = new List <string>();

            foreach (var formParameter in formParameters)
            {
                var name = _options.DescribeAllParametersInCamelCase
                    ? formParameter.Name.ToCamelCase()
                    : formParameter.Name;

                var schema = (formParameter.ModelMetadata != null)
                    ? _schemaGenerator.GenerateSchema(formParameter.ModelMetadata.ModelType, schemaRepository)
                    : new OpenApiSchema {
                    Type = "string"
                };

                var defaultValue = formParameter.CustomAttributes().OfType <DefaultValueAttribute>().FirstOrDefault()?.Value
                                   ?? formParameter.ParameterInfo()?.DefaultValue;

                // NOTE: Oddly, ParameterInfo.DefaultValue returns DBNull if not optional, hence the additional check below
                if (schema.Reference == null && defaultValue != null && defaultValue != DBNull.Value)
                {
                    schema.Default = OpenApiAnyFactory.CreateFor(schema, defaultValue);
                }

                properties.Add(name, schema);

                if (formParameter.IsFromPath() || formParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType())))
                {
                    requiredPropertyNames.Add(name);
                }
            }

            return(new OpenApiSchema
            {
                Type = "object",
                Properties = properties,
                Required = new SortedSet <string>(requiredPropertyNames)
            });
        }
        private void ApplyParameterMetadata(OpenApiSchema schema, Type type, ParameterInfo parameterInfo)
        {
            if (schema.Reference != null && _generatorOptions.UseAllOfToExtendReferenceSchemas)
            {
                schema.AllOf = new[] { new OpenApiSchema {
                                           Reference = schema.Reference
                                       } };
                schema.Reference = null;
            }

            if (schema.Reference == null)
            {
                schema.Nullable = type.IsReferenceOrNullableType();

                schema.ApplyCustomAttributes(parameterInfo.GetCustomAttributes());

                if (parameterInfo.HasDefaultValue)
                {
                    schema.Default = OpenApiAnyFactory.CreateFor(schema, parameterInfo.DefaultValue);
                }
            }
        }
Пример #6
0
        public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository)
        {
            var isNullable = type.IsNullable(out Type innerType);

            var enumType = isNullable
                ? innerType
                : type;

            //Test to determine if the serializer will treat as string or not
            var describeAsString = JsonSerializer.Serialize(enumType.GetEnumValues().GetValue(0), _serializerOptions).StartsWith("\"");

            var schema = describeAsString
                ? EnumTypeMap[typeof(string)]()
                : EnumTypeMap[enumType.GetEnumUnderlyingType()]();

            if (describeAsString)
            {
                schema.Enum = enumType.GetEnumValues()
                              .Cast <object>()
                              .Select(value =>
                {
                    var stringValue = JsonSerializer.Serialize(value, _serializerOptions).Replace("\"", string.Empty);
                    return(OpenApiAnyFactory.CreateFor(schema, stringValue));
                })
                              .ToList();
            }
            else
            {
                schema.Enum = enumType.GetEnumValues()
                              .Cast <object>()
                              .Select(value => OpenApiAnyFactory.CreateFor(schema, value))
                              .ToList();
            }

            schema.Nullable = isNullable;

            return(schema);
        }
 private static void ApplyDefaultValueAttribute(OpenApiSchema schema, DefaultValueAttribute defaultValueAttribute)
 {
     schema.Default = OpenApiAnyFactory.CreateFor(schema, defaultValueAttribute.Value);
 }