コード例 #1
0
        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);
        }
コード例 #2
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);
        }
コード例 #3
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);
        }