示例#1
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns></returns>
        public SwaggerParameter CreatePrimitiveParameter(string name, string description, Type parameterType, IList<Attribute> parentAttributes)
        {
            var typeDescription = JsonObjectTypeDescription.FromType(parameterType, parentAttributes, _settings.DefaultEnumHandling);

            SwaggerParameter operationParameter;
            if (typeDescription.IsEnum)
            {
                // TODO(incompatibility): We use "schema" even it is not allowed in non-body parameters
                parameterType = parameterType.Name == "Nullable`1" ? parameterType.GetGenericTypeArguments().Single() : parameterType;
                operationParameter = new SwaggerParameter
                {
                    Type = typeDescription.Type, // Used as fallback for generators which do not check the "schema" property
                    Schema = new JsonSchema4
                    {
                        SchemaReference = _schemaGenerator.Generate<JsonSchema4>(parameterType, parentAttributes, _schemaResolver, _schemaDefinitionAppender)
                    }
                };
            }
            else
            {
                parameterType = typeDescription.Type.HasFlag(JsonObjectType.Object) ? typeof(string) : parameterType; // object types must be treated as string

                operationParameter = _schemaGenerator.Generate<SwaggerParameter>(parameterType, parentAttributes, _schemaResolver, _schemaDefinitionAppender);
                _schemaGenerator.ApplyPropertyAnnotations(operationParameter, parameterType, parentAttributes, typeDescription);
            }

            operationParameter.Name = name;
            operationParameter.IsRequired = parentAttributes?.Any(a => a.GetType().Name == "RequiredAttribute") ?? false;
            operationParameter.IsNullableRaw = typeDescription.IsNullable;

            if (description != string.Empty)
                operationParameter.Description = description;

            return operationParameter;
        }
示例#2
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns></returns>
        public async Task <SwaggerParameter> CreatePrimitiveParameterAsync(string name, string description, Type parameterType, IList <Attribute> parentAttributes)
        {
            var typeDescription = JsonObjectTypeDescription.FromType(parameterType, ResolveContract(parameterType), parentAttributes, _settings.DefaultEnumHandling);

            SwaggerParameter operationParameter;

            if (typeDescription.IsEnum)
            {
                // TODO(incompatibility): We use "schema" even it is not allowed in non-body parameters
                parameterType      = parameterType.Name == "Nullable`1" ? parameterType.GetGenericTypeArguments().Single() : parameterType;
                operationParameter = new SwaggerParameter
                {
                    Type   = typeDescription.Type, // Used as fallback for generators which do not check the "schema" property
                    Schema = new JsonSchema4
                    {
                        SchemaReference = await _schemaGenerator.GenerateAsync(parameterType, parentAttributes, _schemaResolver).ConfigureAwait(false)
                    }
                };
            }
            else
            {
                var hasTypeMapper = _settings.TypeMappers.Any(tm => tm.MappedType == parameterType);
                if (!hasTypeMapper)
                {
                    parameterType = typeDescription.Type.HasFlag(JsonObjectType.Object) ? typeof(string) : parameterType; // object types must be treated as string
                }
                operationParameter = await _schemaGenerator.GenerateAsync <SwaggerParameter>(parameterType, parentAttributes, _schemaResolver).ConfigureAwait(false);

                _schemaGenerator.ApplyPropertyAnnotations(operationParameter, new Newtonsoft.Json.Serialization.JsonProperty(), parameterType, parentAttributes, typeDescription);

                // check if the type mapper did not properly change the type to a primitive
                if (hasTypeMapper && typeDescription.Type.HasFlag(JsonObjectType.Object) && operationParameter.Type == JsonObjectType.Object)
                {
                    operationParameter.Type = JsonObjectType.String; // enforce string as default
                }
            }

            operationParameter.Name          = name;
            operationParameter.IsRequired    = parentAttributes?.Any(a => a.GetType().Name == "RequiredAttribute") ?? false;
            operationParameter.IsNullableRaw = typeDescription.IsNullable;

            if (typeDescription.Type.HasFlag(JsonObjectType.Array))
            {
                operationParameter.CollectionFormat = SwaggerParameterCollectionFormat.Multi;
            }

            if (description != string.Empty)
            {
                operationParameter.Description = description;
            }

            return(operationParameter);
        }