Esempio n. 1
0
        private async Task <SwaggerParameter> AddBodyParameterAsync(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter)
        {
            SwaggerParameter operationParameter;

            var operation     = context.OperationDescription.Operation;
            var parameterType = extendedApiParameter.ParameterType;

            if (parameterType.Name == "XmlDocument" || parameterType.InheritsFrom("XmlDocument", TypeNameStyle.Name))
            {
                operation.Consumes = new List <string> {
                    "application/xml"
                };
                operationParameter = new SwaggerParameter
                {
                    Name   = extendedApiParameter.ApiParameter.Name,
                    Kind   = SwaggerParameterKind.Body,
                    Schema = new JsonSchema4 {
                        Type = JsonObjectType.String
                    },
                    IsNullableRaw = true,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    Description   = await extendedApiParameter.GetDocumentationAsync().ConfigureAwait(false)
                };
            }
            else if (parameterType.IsAssignableTo("System.IO.Stream", TypeNameStyle.FullName))
            {
                operation.Consumes = new List <string> {
                    "application/octet-stream"
                };
                operationParameter = new SwaggerParameter
                {
                    Name   = extendedApiParameter.ApiParameter.Name,
                    Kind   = SwaggerParameterKind.Body,
                    Schema = new JsonSchema4 {
                        Type = JsonObjectType.String, Format = JsonFormatStrings.Byte
                    },
                    IsNullableRaw = true,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    Description   = await extendedApiParameter.GetDocumentationAsync().ConfigureAwait(false)
                };
            }
            else
            {
                var typeDescription = _settings.ReflectionService.GetDescription(extendedApiParameter.ParameterType, extendedApiParameter.Attributes, _settings);

                operationParameter = new SwaggerParameter
                {
                    Name          = extendedApiParameter.ApiParameter.Name,
                    Kind          = SwaggerParameterKind.Body,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    IsNullableRaw = typeDescription.IsNullable,
                    Description   = await extendedApiParameter.GetDocumentationAsync().ConfigureAwait(false),
                    Schema        = await context.SchemaGenerator.GenerateWithReferenceAndNullabilityAsync <JsonSchema4>(
                        extendedApiParameter.ParameterType, extendedApiParameter.Attributes, isNullable : false, schemaResolver : context.SchemaResolver).ConfigureAwait(false)
                };
            }

            operation.Parameters.Add(operationParameter);
            return(operationParameter);
        }
        private async Task <SwaggerParameter> CreatePrimitiveParameterAsync(
            OperationProcessorContext context,
            ExtendedApiParameterDescription extendedApiParameter)
        {
            var operationParameter = await context.SwaggerGenerator.CreatePrimitiveParameterAsync(
                extendedApiParameter.ApiParameter.Name,
                await extendedApiParameter.GetDocumentationAsync().ConfigureAwait(false),
                extendedApiParameter.ParameterType,
                extendedApiParameter.Attributes).ConfigureAwait(false);

            if (extendedApiParameter.ParameterInfo?.HasDefaultValue == true)
            {
                operationParameter.Default = extendedApiParameter.ParameterInfo.DefaultValue;
            }

            operationParameter.IsRequired = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault);
            return(operationParameter);
        }