Example #1
0
        private static void AddResponseHeaders(
            OpenApiOperation operation,
            ApiResponseType responseType,
            JsonSchemaResolver schemaResolver,
            JsonSchemaGenerator schemaGenerator)
        {
            var headers = (responseType.ModelMetadata as ApiModelMetadata)?.Headers;

            if (headers == null || headers.Length == 0)
            {
                return;
            }

            if (!operation.Responses.TryGetValue(responseType.StatusCode.ToString(), out var response))
            {
                return;
            }

            for (var i = 0; i < headers.Length; i++)
            {
                var metadata   = headers[i];
                var headerType = metadata.Type.ToContextualType();
                var schema     = schemaGenerator.GenerateWithReferenceAndNullability <JsonSchema>(headerType, schemaResolver);
                response.Headers.Add(metadata.Name, new OpenApiHeader {
                    Schema = schema
                });
            }
        }
Example #2
0
        private async Task <SignalrTypeOperation> GenerateOperationAsync(SignalrTypesCallBack method, JsonSchemaGenerator generator, SignalrTypeSchemaResolver resolver, SignalrTypeOperationType operationType)
        {
            var operation = new SignalrTypeOperation
            {
                Description = method.Description,
                Type        = operationType
            };

            foreach (var arg in method.SignalrTypesCallBackParameters)
            {
                var parameter = generator.GenerateWithReferenceAndNullability <SignalrTypeParameter>(
                    arg.ParameterType.ToContextualType(), arg.ParameterType.ToContextualType().IsNullableType, resolver, (p, s) =>
                {
                    p.Description = arg.Description;
                });

                operation.Parameters[arg.Name] = parameter;
            }

            var returnType =
                operationType == SignalrTypeOperationType.Observable
                    ? method.ReturnType.GetGenericArguments().First()
                : method.ReturnType == typeof(Task)
                    ? null
                : method.ReturnType.IsGenericType && method.ReturnType.BaseType == typeof(Task)
                    ? method.ReturnType.GetGenericArguments().First()
                    : method.ReturnType;

            operation.ReturnType = returnType == null ? null : generator.GenerateWithReferenceAndNullability <JsonSchema>(
                returnType.ToContextualType(), returnType.ToContextualType().IsNullableType, resolver, async(p, s) =>
            {
                p.Description = method.ReturnType.GetXmlDocsSummary();
            });

            return(operation);
        }
Example #3
0
        private async Task <SigSpecOperation> GenerateOperationAsync(Type type, MethodInfo method, JsonSchemaGenerator generator, SigSpecSchemaResolver resolver, SigSpecOperationType operationType)
        {
            var operation = new SigSpecOperation
            {
                Description = method.GetXmlDocsSummary(),
                Type        = operationType
            };

            foreach (var arg in method.GetParameters().Where(param => param.ParameterType != typeof(CancellationToken)))
            {
                var parameter = generator.GenerateWithReferenceAndNullability <SigSpecParameter>(
                    arg.ParameterType.ToContextualType(), arg.ParameterType.ToContextualType().IsNullableType, resolver, (p, s) =>
                {
                    p.Description = arg.GetXmlDocs();
                });

                operation.Parameters[arg.Name] = parameter;
            }

            var returnType =
                operationType == SigSpecOperationType.Observable
                    ? method.ReturnType.GetGenericArguments().First()
                : method.ReturnType == typeof(Task)
                    ? null
                : method.ReturnType.IsGenericType && method.ReturnType.BaseType == typeof(Task)
                    ? method.ReturnType.GetGenericArguments().First()
                    : method.ReturnType;

            operation.ReturnType = returnType == null ? null : generator.GenerateWithReferenceAndNullability <JsonSchema>(
                returnType.ToContextualType(), returnType.ToContextualType().IsNullableType, resolver, async(p, s) =>
            {
                p.Description = method.ReturnType.GetXmlDocsSummary();
            });

            return(operation);
        }
Example #4
0
        private async Task LoadDefaultSuccessResponseAsync(
            SwaggerOperation operation, MethodInfo methodInfo, string responseDescription,
            JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver)
        {
            var returnType = methodInfo.ReturnType;

            if (returnType == typeof(Task))
            {
                returnType = typeof(void);
            }
            else if (returnType.Name == "Task`1")
            {
                returnType = returnType.GenericTypeArguments[0];
            }

            if (IsVoidResponse(returnType))
            {
                operation.Responses[GetVoidResponseStatusCode()] = new SwaggerResponse
                {
                    Description = responseDescription
                };
            }
            else
            {
                IEnumerable <Attribute> attributes;
                try
                {
                    attributes = methodInfo.ReturnParameter?.GetCustomAttributes(true).OfType <Attribute>();
                }
                catch
                {
                    attributes = methodInfo.ReturnParameter?.GetCustomAttributes(false).OfType <Attribute>();
                }

                var typeDescription = _settings.ReflectionService.GetDescription(returnType, attributes, _settings);
                operation.Responses["200"] = new SwaggerResponse
                {
                    Description   = responseDescription,
                    IsNullableRaw = typeDescription.IsNullable,
                    Schema        = await schemaGenerator.GenerateWithReferenceAndNullability <JsonSchema4>(
                        returnType, attributes, typeDescription.IsNullable, schemaResolver)
                                    .ConfigureAwait(false)
                };
            }
        }
Example #5
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The parameter.</returns>
        public async Task <SwaggerParameter> CreateBodyParameterAsync(string name, ParameterInfo parameter)
        {
            var attributes = parameter.GetCustomAttributes();

            var isRequired      = IsParameterRequired(parameter);
            var typeDescription = _settings.ReflectionService.GetDescription(parameter.ParameterType, attributes, _settings);

            var operationParameter = new SwaggerParameter
            {
                Name          = name,
                Kind          = SwaggerParameterKind.Body,
                IsRequired    = isRequired,
                IsNullableRaw = typeDescription.IsNullable,
                Description   = await parameter.GetDescriptionAsync(attributes).ConfigureAwait(false),
                Schema        = await _schemaGenerator.GenerateWithReferenceAndNullability <JsonSchema4>(
                    parameter.ParameterType, attributes, !isRequired, _schemaResolver).ConfigureAwait(false)
            };

            return(operationParameter);
        }