Esempio n. 1
0
        private void ApplyMemberMetadata(OpenApiSchema schema, Type type, MemberInfo memberInfo)
        {
            if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null)
            {
                schema.AllOf = new[] { new OpenApiSchema {
                                           Reference = schema.Reference
                                       } };
                schema.Reference = null;
            }

            if (schema.Reference == null)
            {
                var customAttributes = memberInfo.GetInlineAndMetadataAttributes();

                schema.Nullable = type.IsReferenceOrNullableType();

                var defaultValueAttribute = customAttributes.OfType <DefaultValueAttribute>().FirstOrDefault();
                if (defaultValueAttribute != null)
                {
                    var defaultAsJson = _serializerBehavior.Serialize(defaultValueAttribute.Value);
                    schema.Default = OpenApiAnyFactory.CreateFromJson(defaultAsJson); // TODO: address assumption that serializer returns JSON
                }

                var obsoleteAttribute = customAttributes.OfType <ObsoleteAttribute>().FirstOrDefault();
                if (obsoleteAttribute != null)
                {
                    schema.Deprecated = true;
                }

                schema.ApplyValidationAttributes(customAttributes);
            }
        }
Esempio n. 2
0
        public void Given_MinLengthAttribute_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result(DataType dataType, string expected)
        {
            var schema = new OpenApiSchema
            {
                Type   = "string",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new DataTypeAttribute(dataType)
            };

            schema.ApplyValidationAttributes(attributes);

            schema.Format.Should().Be(expected);
        }
Esempio n. 3
0
        public void Given_RegularExpressionAttribute_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result(string pattern)
        {
            var schema = new OpenApiSchema
            {
                Type   = "string",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new RegularExpressionAttribute(pattern)
            };

            schema.ApplyValidationAttributes(attributes);

            schema.Pattern.Should().Be(pattern);
        }
Esempio n. 4
0
        public void Given_MaxLengthAttribute_With_ArrayType_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result(int length)
        {
            var schema = new OpenApiSchema
            {
                Type   = "array",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new MaxLengthAttribute(length)
            };

            schema.ApplyValidationAttributes(attributes);

            schema.MaxItems.Should().Be(length);
        }
Esempio n. 5
0
        public void Given_RangeAttribute_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result(int min, int max)
        {
            var schema = new OpenApiSchema
            {
                Type   = "string",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new RangeAttribute(min, max)
            };

            schema.ApplyValidationAttributes(attributes);

            schema.Minimum.Should().Be(min);
            schema.Maximum.Should().Be(max);
        }
Esempio n. 6
0
        public void Given_RequiredAttribute_NoStringType_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result()
        {
            var schema = new OpenApiSchema
            {
                Type   = "object",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new RequiredAttribute()
                {
                    AllowEmptyStrings = false
                },
            };

            schema.ApplyValidationAttributes(attributes);

            schema.MinLength.Should().BeNull();
        }
Esempio n. 7
0
        public void Given_StringLengthAttribute_When_ApplyValidationAttributes_Invoked_Then_It_Should_Return_Result()
        {
            var schema = new OpenApiSchema
            {
                Type   = "string",
                Format = null,
                Items  = null,
            };

            var attributes = new List <ValidationAttribute>()
            {
                new StringLengthAttribute(15)
                {
                    MinimumLength = 10
                },
            };

            schema.ApplyValidationAttributes(attributes);

            schema.MinLength.Should().Be(10);
            schema.MaxLength.Should().Be(15);
        }
Esempio n. 8
0
        private void ApplyParameterMetadata(OpenApiSchema schema, Type type, ParameterInfo parameterInfo)
        {
            if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null)
            {
                schema.AllOf = new[] { new OpenApiSchema {
                                           Reference = schema.Reference
                                       } };
                schema.Reference = null;
            }

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

                if (parameterInfo.HasDefaultValue)
                {
                    var defaultAsJson = _serializerBehavior.Serialize(parameterInfo.DefaultValue);
                    schema.Default = OpenApiAnyFactory.CreateFromJson(defaultAsJson);
                }

                schema.ApplyValidationAttributes(parameterInfo.GetCustomAttributes());
            }
        }