Ejemplo 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 OpenApiParameter CreatePrimitiveParameter(
            OperationProcessorContext context,
            ExtendedApiParameterDescription extendedApiParameter)
        {
            var contextualParameterType = extendedApiParameter.ParameterType
                                          .ToContextualType(extendedApiParameter.Attributes);

            var description        = extendedApiParameter.GetDocumentation();
            var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(
                extendedApiParameter.ApiParameter.Name, description, contextualParameterType);

            var exampleValue = extendedApiParameter.PropertyInfo != null?
                               context.SchemaGenerator.GenerateExample(extendedApiParameter.PropertyInfo.ToContextualAccessor()) : null;

            var hasExampleValue = exampleValue != null;
            var hasDefaultValue = extendedApiParameter.ParameterInfo?.HasDefaultValue == true;

            if (hasExampleValue || hasDefaultValue)
            {
                var defaultValue = hasDefaultValue ? context.SchemaGenerator
                                   .ConvertDefaultValue(contextualParameterType, extendedApiParameter.ParameterInfo.DefaultValue) : null;

                if (_settings.SchemaType == SchemaType.Swagger2)
                {
                    operationParameter.Default = defaultValue;
                    operationParameter.Example = exampleValue;
                }
                else if (operationParameter.Schema.HasReference)
                {
                    if (_settings.AllowReferencesWithProperties)
                    {
                        operationParameter.Schema = new JsonSchema
                        {
                            Default   = defaultValue,
                            Example   = exampleValue,
                            Reference = operationParameter.Schema,
                        };
                    }
                    else
                    {
                        operationParameter.Schema = new JsonSchema
                        {
                            Default = defaultValue,
                            Example = exampleValue,
                            OneOf   = { operationParameter.Schema },
                        };
                    }
                }
                else
                {
                    operationParameter.Schema.Default = defaultValue;
                    operationParameter.Schema.Example = exampleValue;
                }
            }

            operationParameter.IsRequired = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault);
            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);
        }
Ejemplo n.º 4
0
        private OpenApiParameter CreatePrimitiveParameter(
            OperationProcessorContext context,
            ExtendedApiParameterDescription extendedApiParameter)
        {
            var contextualParameter = extendedApiParameter.ParameterType.ToContextualType(extendedApiParameter.Attributes);

            var description        = extendedApiParameter.GetDocumentation();
            var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(
                extendedApiParameter.ApiParameter.Name, description, contextualParameter);

            if (extendedApiParameter.ParameterInfo?.HasDefaultValue == true)
            {
                var defaultValue = context.SchemaGenerator
                                   .ConvertDefaultValue(contextualParameter, extendedApiParameter.ParameterInfo.DefaultValue);

                if (_settings.SchemaType == SchemaType.Swagger2)
                {
                    operationParameter.Default = defaultValue;
                }
                else if (operationParameter.Schema.HasReference)
                {
                    operationParameter.Schema = new JsonSchema
                    {
                        Default = defaultValue,
                        OneOf   = { operationParameter.Schema }
                    };
                }
                else
                {
                    operationParameter.Schema.Default = defaultValue;
                }
            }

            operationParameter.IsRequired = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault);
            return(operationParameter);
        }
Ejemplo n.º 5
0
        private OpenApiParameter AddBodyParameter(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter)
        {
            OpenApiParameter operationParameter;

            var contextualParameterType = extendedApiParameter.ParameterType
                                          .ToContextualType(extendedApiParameter.Attributes);

            var typeDescription = _settings.ReflectionService.GetDescription(contextualParameterType, _settings);
            var isNullable      = _settings.AllowNullableBodyParameters && typeDescription.IsNullable;
            var operation       = context.OperationDescription.Operation;

            var parameterType = extendedApiParameter.ParameterType;

            if (parameterType.Name == "XmlDocument" || parameterType.InheritsFromTypeName("XmlDocument", TypeNameStyle.Name))
            {
                operation.TryAddConsumes("application/xml");
                operationParameter = new OpenApiParameter
                {
                    Name   = extendedApiParameter.ApiParameter.Name,
                    Kind   = OpenApiParameterKind.Body,
                    Schema = new JsonSchema
                    {
                        Type          = JsonObjectType.String,
                        IsNullableRaw = isNullable
                    },
                    IsNullableRaw = isNullable,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    Description   = extendedApiParameter.GetDocumentation()
                };
            }
            else if (parameterType.IsAssignableToTypeName("System.IO.Stream", TypeNameStyle.FullName))
            {
                operation.TryAddConsumes("application/octet-stream");
                operationParameter = new OpenApiParameter
                {
                    Name   = extendedApiParameter.ApiParameter.Name,
                    Kind   = OpenApiParameterKind.Body,
                    Schema = new JsonSchema
                    {
                        Type          = JsonObjectType.String,
                        Format        = JsonFormatStrings.Byte,
                        IsNullableRaw = isNullable
                    },
                    IsNullableRaw = isNullable,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    Description   = extendedApiParameter.GetDocumentation()
                };
            }
            else // body from type
            {
                operationParameter = new OpenApiParameter
                {
                    Name          = extendedApiParameter.ApiParameter.Name,
                    Kind          = OpenApiParameterKind.Body,
                    IsRequired    = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault),
                    IsNullableRaw = isNullable,
                    Description   = extendedApiParameter.GetDocumentation(),
                    Schema        = context.SchemaGenerator.GenerateWithReferenceAndNullability <JsonSchema>(
                        contextualParameterType, isNullable, schemaResolver: context.SchemaResolver)
                };
            }

            operation.Parameters.Add(operationParameter);
            return(operationParameter);
        }