/// <summary> /// Applies the filter to the specified operation using the given context. /// </summary> /// <param name="operation">The operation to apply the filter to.</param> /// <param name="context">The current operation filter context.</param> public void Apply(Operation operation, OperationFilterContext context) { if (operation.Parameters == null) { return; } // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/412 // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/413 foreach (NonBodyParameter parameter in operation.Parameters.OfType <NonBodyParameter>()) { ApiParameterDescription description = context.ApiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name); ApiParameterRouteInfo routeInfo = description.RouteInfo; if (parameter.Description == null) { parameter.Description = description.ModelMetadata?.Description; } if (routeInfo == null) { continue; } if (parameter.Default == null) { parameter.Default = routeInfo.DefaultValue; } parameter.Required |= !routeInfo.IsOptional; } }
public static void ApplyRouteConstraints(this OpenApiSchema schema, ApiParameterRouteInfo routeInfo) { foreach (var constraint in routeInfo.Constraints) { if (constraint is MinRouteConstraint minRouteConstraint) { ApplyMinRouteConstraint(schema, minRouteConstraint); } else if (constraint is MaxRouteConstraint maxRouteConstraint) { ApplyMaxRouteConstraint(schema, maxRouteConstraint); } else if (constraint is MinLengthRouteConstraint minLengthRouteConstraint) { ApplyMinLengthRouteConstraint(schema, minLengthRouteConstraint); } else if (constraint is MaxLengthRouteConstraint maxLengthRouteConstraint) { ApplyMaxLengthRouteConstraint(schema, maxLengthRouteConstraint); } else if (constraint is RangeRouteConstraint rangeRouteConstraint) { ApplyRangeRouteConstraint(schema, rangeRouteConstraint); } else if (constraint is RegexRouteConstraint regexRouteConstraint) { ApplyRegexRouteConstraint(schema, regexRouteConstraint); } else if (constraint is LengthRouteConstraint lengthRouteConstraint) { ApplyLengthRouteConstraint(schema, lengthRouteConstraint); } else if (constraint is LongRouteConstraint || constraint is FloatRouteConstraint || constraint is DecimalRouteConstraint) { schema.Type = "number"; } else if (constraint is IntRouteConstraint) { schema.Type = "integer"; } else if (constraint is GuidRouteConstraint || constraint is StringRouteConstraint) { schema.Type = "string"; } else if (constraint is BoolRouteConstraint) { schema.Type = "boolean"; } } }
/// <summary> /// Applies the filter to the specified operation using the given context. /// </summary> /// <param name="operation">The operation to apply the filter to.</param> /// <param name="context">The current operation filter context.</param> public void Apply(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters == null) { return; } foreach (OpenApiParameter parameter in operation.Parameters) { ApiParameterDescription description = context.ApiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name); ApiParameterRouteInfo routeInfo = description.RouteInfo; if (parameter.Description == null) { parameter.Description = description.ModelMetadata?.Description; } if (routeInfo == null) { continue; } if (parameter.Schema.Default == null && description.DefaultValue != null) { parameter.Schema.Default = OpenApiAnyFactory.CreateFor(parameter.Schema, description.DefaultValue); } parameter.Required |= !routeInfo.IsOptional; } }
private OpenApiSchema GenerateSchemaForParameter( Type modelType, SchemaRepository schemaRepository, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo) { var dataContract = GetDataContractFor(modelType); var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, parameterInfo, out var knownTypesDataContracts) ? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts, parameterInfo) : GenerateConcreteSchema(dataContract, schemaRepository, parameterInfo); if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null) { schema.AllOf = new[] { new OpenApiSchema { Reference = schema.Reference } }; schema.Reference = null; } if (schema.Reference == null) { var customAttributes = parameterInfo.GetCustomAttributes(); var defaultValue = parameterInfo.HasDefaultValue ? parameterInfo.DefaultValue : customAttributes.OfType <DefaultValueAttribute>().FirstOrDefault()?.Value; if (defaultValue != null) { var defaultAsJson = dataContract.JsonConverter(defaultValue); schema.Default = OpenApiAnyFactory.CreateFromJson(defaultAsJson); } schema.ApplyValidationAttributes(customAttributes); if (routeInfo != null) { schema.ApplyRouteConstraints(routeInfo); } ApplyFilters(schema, modelType, schemaRepository, parameterInfo: parameterInfo); } return(schema); }
public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints() { var constraints = new List <IRouteConstraint>() { new IntRouteConstraint(), }; var routeInfo = new ApiParameterRouteInfo { Constraints = constraints }; var parameterInfo = typeof(FakeController) .GetMethod(nameof(FakeController.ActionWithParameter)) .GetParameters() .First(); var schema = Subject().GenerateSchema(typeof(string), new SchemaRepository(), parameterInfo: parameterInfo, routeInfo: routeInfo); Assert.Equal("integer", schema.Type); }
private OpenApiSchema GenerateSchema( Type type, SchemaRepository schemaRepository, PropertyInfo propertyInfo = null, ParameterInfo parameterInfo = null, ApiParameterRouteInfo routeInfo = null) { try { return(_schemaGenerator.GenerateSchema(type, schemaRepository, propertyInfo, parameterInfo, routeInfo)); } catch (Exception ex) { throw new SwaggerGeneratorException( message: $"Failed to generate schema for type - {type}. See inner exception", innerException: ex); } }
public OpenApiSchema GenerateSchema( Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo = null, ParameterInfo parameterInfo = null, ApiParameterRouteInfo routeInfo = null) { if (memberInfo != null) { return(GenerateSchemaForMember(modelType, schemaRepository, memberInfo, parameterInfo)); } if (parameterInfo != null) { return(GenerateSchemaForParameter(modelType, schemaRepository, parameterInfo, routeInfo)); } return(GenerateSchemaForType(modelType, schemaRepository, parameterInfo)); }
public static void ApplyRouteConstraints(this OpenApiSchema schema, ApiParameterRouteInfo routeInfo) { foreach (var constraint in routeInfo.Constraints) { if (constraint is MinRouteConstraint minRouteConstraint) { ApplyMinRouteConstraint(schema, minRouteConstraint); } else if (constraint is MaxRouteConstraint maxRouteConstraint) { ApplyMaxRouteConstraint(schema, maxRouteConstraint); } else if (constraint is MinLengthRouteConstraint minLengthRouteConstraint) { ApplyMinLengthRouteConstraint(schema, minLengthRouteConstraint); } else if (constraint is MaxLengthRouteConstraint maxLengthRouteConstraint) { ApplyMaxLengthRouteConstraint(schema, maxLengthRouteConstraint); } else if (constraint is RangeRouteConstraint rangeRouteConstraint) { ApplyRangeRouteConstraint(schema, rangeRouteConstraint); } else if (constraint is RegexRouteConstraint regexRouteConstraint) { ApplyRegexRouteConstraint(schema, regexRouteConstraint); } else if (constraint is LengthRouteConstraint lengthRouteConstraint) { ApplyLengthRouteConstraint(schema, lengthRouteConstraint); } else if (constraint is FloatRouteConstraint or DecimalRouteConstraint) { schema.Type = "number"; }
public void GenerateSchema_GeneratesSchema_IfParameterHasMultipleConstraints() { var maxLength = 3; var minLength = 1; var constraints = new List <IRouteConstraint>() { new MaxLengthRouteConstraint(3), new MinLengthRouteConstraint(minLength) }; var routeInfo = new ApiParameterRouteInfo { Constraints = constraints }; var parameterInfo = typeof(FakeController) .GetMethod(nameof(FakeController.ActionWithParameter)) .GetParameters() .First(); var schema = Subject().GenerateSchema(typeof(string), new SchemaRepository(), parameterInfo: parameterInfo, routeInfo: routeInfo); Assert.Equal(maxLength, schema.MaxLength); Assert.Equal(minLength, schema.MinLength); }