/// <inheritdoc /> public void Apply(OpenApiSchema schema, SchemaFilterContext context) { if (_validatorFactory == null) { _logger.LogWarning(0, "ValidatorFactory is not provided. Please register FluentValidation."); return; } if (schema == null) { return; } IValidator?validator = null; try { validator = _validatorFactory.GetValidator(context.Type); } catch (Exception e) { _logger.LogWarning(0, e, $"GetValidator for type '{context.Type}' fails."); } if (validator == null) { return; } var schemaProvider = new SwashbuckleSchemaProvider(context.SchemaRepository, context.SchemaGenerator, _schemaGenerationSettings.SchemaIdSelector); var schemaContext = new SchemaGenerationContext( schema: schema, schemaType: context.Type, rules: _rules, schemaGenerationOptions: _schemaGenerationOptions, schemaGenerationSettings: _schemaGenerationSettings, schemaProvider: schemaProvider); ApplyRulesToSchema(context, validator, schemaContext); try { AddRulesFromIncludedValidators(context, validator, schemaContext); } catch (Exception e) { _logger.LogWarning(0, e, $"Applying IncludeRules for type '{context.Type}' fails."); } }
private void ApplyInternal(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters == null) { return; } if (_validatorFactory == null) { _logger.LogWarning(0, "ValidatorFactory is not provided. Please register FluentValidation."); return; } var schemaProvider = new SwashbuckleSchemaProvider(context.SchemaRepository, context.SchemaGenerator, _schemaGenerationSettings.SchemaIdSelector); foreach (var operationParameter in operation.Parameters) { var apiParameterDescription = context.ApiDescription.ParameterDescriptions.FirstOrDefault(description => description.Name.Equals(operationParameter.Name, StringComparison.InvariantCultureIgnoreCase)); var modelMetadata = apiParameterDescription?.ModelMetadata; if (modelMetadata != null) { var parameterType = modelMetadata.ContainerType; if (parameterType == null) { continue; } var validator = _validatorFactory.GetValidator(parameterType); if (validator == null) { continue; } OpenApiSchema schema = schemaProvider.GetSchemaForType(parameterType); if (schema.Properties != null && schema.Properties.Count > 0) { var schemaPropertyName = operationParameter.Name; KeyValuePair <string, OpenApiSchema> apiProperty = schema.Properties.FirstOrDefault(property => property.Key.EqualsIgnoreAll(schemaPropertyName)); if (apiProperty.Key != null) { schemaPropertyName = apiProperty.Key; } else { var propertyInfo = parameterType.GetProperty(schemaPropertyName); if (propertyInfo != null && _schemaGenerationSettings.NameResolver != null) { schemaPropertyName = _schemaGenerationSettings.NameResolver?.GetPropertyName(propertyInfo); } } var schemaContext = new SchemaGenerationContext( schema: schema, schemaType: parameterType, rules: _rules, schemaGenerationOptions: _schemaGenerationOptions, schemaGenerationSettings: _schemaGenerationSettings, schemaProvider: schemaProvider); FluentValidationSchemaBuilder.ApplyRulesToSchema( schemaType: parameterType, schemaPropertyNames: new[] { schemaPropertyName }, validator: validator, logger: _logger, schemaGenerationContext: schemaContext); if (schema.Required != null) { operationParameter.Required = schema.Required.Contains(schemaPropertyName, IgnoreAllStringComparer.Instance); } var parameterSchema = operationParameter.Schema; if (parameterSchema != null) { if (schema.Properties.TryGetValue(schemaPropertyName.ToLowerCamelCase(), out var property) || schema.Properties.TryGetValue(schemaPropertyName, out property)) { // Copy from property schema to parameter schema. parameterSchema.MinLength = property.MinLength; parameterSchema.Nullable = property.Nullable; parameterSchema.MaxLength = property.MaxLength; parameterSchema.Pattern = property.Pattern; parameterSchema.Minimum = property.Minimum; parameterSchema.Maximum = property.Maximum; parameterSchema.ExclusiveMaximum = property.ExclusiveMaximum; parameterSchema.ExclusiveMinimum = property.ExclusiveMinimum; parameterSchema.AllOf = property.AllOf; } } } } } }