Example #1
0
        private OpenApiParameter GenerateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            apiParameter.GetAdditionalMetadata(
                apiDescription,
                out ParameterInfo parameterInfo,
                out PropertyInfo propertyInfo,
                out IEnumerable <object> parameterOrPropertyAttributes);

            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location = ParameterLocationMap.ContainsKey(apiParameter.Source)
                ? ParameterLocationMap[apiParameter.Source]
                : ParameterLocation.Query;

            var isRequired = (apiParameter.IsFromPath()) ||
                             parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var defaultValue = parameterInfo?.DefaultValue
                               ?? parameterOrPropertyAttributes.OfType <DefaultValueAttribute>().FirstOrDefault()?.Value;

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

            if (defaultValue != null && schema.Reference == null)
            {
                schema.Default = OpenApiAnyFactory.TryCreateFrom(defaultValue, out IOpenApiAny openApiAny)
                    ? openApiAny
                    : null;
            }

            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(apiParameter, _schemaGenerator, schemaRepository, parameterInfo, propertyInfo);

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
Example #2
0
        private OpenApiSchema GeneratePropertySchema(JsonProperty jsonProperty, object[] attributes, SchemaRepository schemaRepository)
        {
            var schema = _schemaGenerator.GenerateSchemaFor(jsonProperty.PropertyType, schemaRepository);

            schema.WriteOnly = jsonProperty.Writable && !jsonProperty.Readable;
            schema.ReadOnly  = jsonProperty.Readable && !jsonProperty.Writable;

            foreach (var attribute in attributes)
            {
                if (attribute is DefaultValueAttribute defaultValue)
                {
                    schema.Default = OpenApiAnyFactory.TryCreateFrom(defaultValue.Value, out IOpenApiAny openApiAny)
                        ? openApiAny
                        : schema.Default;
                }
                else if (attribute is RegularExpressionAttribute regex)
                {
                    schema.Pattern = regex.Pattern;
                }
                else if (attribute is RangeAttribute range)
                {
                    schema.Maximum = decimal.TryParse(range.Maximum.ToString(), out decimal maximum)
                        ? maximum
                        : schema.Maximum;

                    schema.Minimum = decimal.TryParse(range.Minimum.ToString(), out decimal minimum)
                        ? minimum
                        : schema.Minimum;
                }
                else if (attribute is MinLengthAttribute minLength)
                {
                    schema.MinLength = minLength.Length;
                }
                else if (attribute is MaxLengthAttribute maxLength)
                {
                    schema.MaxLength = maxLength.Length;
                }
                else if (attribute is StringLengthAttribute stringLength)
                {
                    schema.MinLength = stringLength.MinimumLength;
                    schema.MaxLength = stringLength.MaximumLength;
                }
                else if (attribute is DataTypeAttribute dataTypeAttribute && schema.Type == "string")
                {
                    schema.Format = DataTypeFormatMap.TryGetValue(dataTypeAttribute.DataType, out string format)
                        ? format
                        : schema.Format;
                }
            }

            return(schema);
        }
Example #3
0
        private static IOpenApiAny ConvertToOpenApiType(string value, Type type)
        {
            object typedExample;

            try
            {
                typedExample = TypeDescriptor.GetConverter(type).ConvertFrom(value);
            }
            catch (Exception)
            {
                return(new OpenApiString(value));
            }

            return(OpenApiAnyFactory.TryCreateFrom(typedExample, out IOpenApiAny openApiAny)
                ? openApiAny
                : new OpenApiString(value));
        }
Example #4
0
        private OpenApiSchema GenerateEnumSchema(JsonPrimitiveContract jsonPrimitiveContract)
        {
            var stringEnumConverter = (jsonPrimitiveContract.Converter as StringEnumConverter)
                                      ?? _serializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            var describeAsString = Options.DescribeAllEnumsAsStrings ||
                                   (stringEnumConverter != null);

            var describeInCamelCase = Options.DescribeStringEnumsInCamelCase ||
                                      (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

            var enumType           = jsonPrimitiveContract.UnderlyingType;
            var enumUnderlyingType = describeAsString ? typeof(string) : enumType.GetEnumUnderlyingType();

            var schema = FactoryMethodMap[enumUnderlyingType]();

            if (describeAsString)
            {
                schema.Enum = enumType.GetEnumNames()
                              .Select(name =>
                {
                    name = describeInCamelCase ? name.ToCamelCase() : name;
                    return((IOpenApiAny)(new OpenApiString(name)));
                })
                              .ToList();
            }
            else
            {
                schema.Enum = enumType.GetEnumValues()
                              .Cast <object>()
                              .Select(value =>
                {
                    value = Convert.ChangeType(value, enumUnderlyingType);
                    return(OpenApiAnyFactory.TryCreateFrom(value, out IOpenApiAny openApiAny) ? openApiAny : null);
                })
                              .ToList();
            }

            return(schema);
        }