Esempio n. 1
0
        public void Apply(OpenApiSchema schema, IOpenApiSchemaFilterContext context)
        {
            TryApplyTypeComments(schema, context.Type);

            if (context.PropertyNames == null)
            {
                return;
            }

            foreach (var keyValuePair in context.PropertyNames)
            {
                if (!schema.Properties.TryGetValue(keyValuePair.Key, out OpenApiSchema propertySchema))
                {
                    continue;
                }

                if (propertySchema.Reference != null)
                {
                    // can't add descriptions to a reference schema
                    continue;
                }

                if (keyValuePair.Value == null)
                {
                    continue;
                }

                var memberInfo = context.Type.GetMember(keyValuePair.Value).FirstOrDefault();
                TryApplyMemberComments(propertySchema, memberInfo);
            }
        }
        public void Apply(OpenApiSchema schema, IOpenApiSchemaFilterContext schemaFilterContext)
        {
            _validatorTypes.TryGetValue(schemaFilterContext.Type, out var validatorType);
            if (validatorType == null || validatorType.GetConstructor(Type.EmptyTypes) == null)
            {
                return;
            }

            var validator = (IValidator)Activator.CreateInstance(validatorType);

            ApplyRulesToSchema(schema, schemaFilterContext, validator);
            AddRulesFromIncludedValidators(schema, schemaFilterContext, validator);
        }
        private void AddRulesFromIncludedValidators(OpenApiSchema schema, IOpenApiSchemaFilterContext schemaFilterContext, IValidator validator)
        {
            var childAdapters = (validator as IEnumerable <IValidationRule>)
                                .NotNull()
                                .OfType <IncludeRule>()
                                .Where(includeRule => includeRule.Condition == null && includeRule.AsyncCondition == null)
                                .SelectMany(includeRule => includeRule.Validators)
                                .OfType <ChildValidatorAdaptor>();

            foreach (var adapter in childAdapters)
            {
                var propertyValidatorContext = new PropertyValidatorContext(new ValidationContext(null), null, string.Empty);
                var includeValidator         = adapter.GetValidator(propertyValidatorContext);
                ApplyRulesToSchema(schema, schemaFilterContext, includeValidator);
                AddRulesFromIncludedValidators(schema, schemaFilterContext, includeValidator);
            }
        }
        private void ApplyRulesToSchema(OpenApiSchema schema, IOpenApiSchemaFilterContext schemaFilterContext, IValidator validator)
        {
            foreach (var key in schema?.Properties?.Keys ?? Array.Empty <string>())
            {
                var propertyName = schemaFilterContext.PropertyNames[key];
                var validators   = validator.GetValidatorsForMember(propertyName);

                foreach (var propertyValidator in validators)
                {
                    foreach (var rule in _rules)
                    {
                        if (rule.Matches(propertyValidator))
                        {
                            rule.Apply(new OpenApiFluentValidationRuleContext(schema, key, propertyValidator));
                        }
                    }
                }
            }
        }