Exemplo n.º 1
0
        public void When_generating_operation_id()
        {
            //// Arrange
            var document = new SwaggerDocument();

            document.Paths["path"] = new SwaggerPathItem
            {
                {
                    SwaggerOperationMethod.Get,
                    new SwaggerOperation {
                    }
                },
                {
                    SwaggerOperationMethod.Post,
                    new SwaggerOperation {
                    }
                }
            };

            //// Act
            document.GenerateOperationIds();

            //// Assert
            Assert.IsTrue(document.Operations.GroupBy(o => o.Operation.OperationId).All(g => g.Count() == 1));
        }
Exemplo n.º 2
0
        private List <TOperationModel> GetOperations(SwaggerDocument document)
        {
            document.GenerateOperationIds();

            return(document.Paths
                   .SelectMany(pair => pair.Value.Select(p => new { Path = pair.Key.TrimStart('/'), HttpMethod = p.Key, Operation = p.Value }))
                   .Select(tuple =>
            {
                var operationModel = CreateOperationModel(tuple.Operation, BaseSettings);
                operationModel.ControllerName = BaseSettings.OperationNameGenerator.GetClientName(document, tuple.Path, tuple.HttpMethod, tuple.Operation);
                operationModel.Path = tuple.Path;
                operationModel.HttpMethod = tuple.HttpMethod;
                operationModel.OperationName = BaseSettings.OperationNameGenerator.GetOperationName(document, tuple.Path, tuple.HttpMethod, tuple.Operation);
                return operationModel;
            })
                   .ToList());
        }
Exemplo n.º 3
0
        internal List <OperationModel> GetOperations(SwaggerDocument document)
        {
            document.GenerateOperationIds();

            var operations = document.Paths
                             .SelectMany(pair => pair.Value.Select(p => new { Path = pair.Key.Trim('/'), HttpMethod = p.Key, Operation = p.Value }))
                             .Select(tuple =>
            {
                var operation       = tuple.Operation;
                var exceptionSchema = (Resolver as SwaggerToCSharpTypeResolver)?.ExceptionSchema;
                var responses       = operation.Responses.Select(response => new ResponseModel(response, exceptionSchema, this)).ToList();

                var defaultResponse = responses.SingleOrDefault(r => r.StatusCode == "default");
                if (defaultResponse != null)
                {
                    responses.Remove(defaultResponse);
                }

                return(new OperationModel
                {
                    Path = tuple.Path,
                    HttpMethod = tuple.HttpMethod,
                    Operation = tuple.Operation,
                    OperationName = BaseSettings.OperationNameGenerator.GetOperationName(document, tuple.Path, tuple.HttpMethod, tuple.Operation),

                    ResultType = GetResultType(operation),
                    HasResultType = HasResultType(operation),
                    ResultDescription = GetResultDescription(operation),

                    ExceptionType = GetExceptionType(operation),
                    HasFormParameters = operation.ActualParameters.Any(p => p.Kind == SwaggerParameterKind.FormData),
                    Responses = responses,
                    DefaultResponse = defaultResponse,
                    Parameters = operation.ActualParameters.Select(p => new ParameterModel(
                                                                       ResolveParameterType(p), operation, p, p.Name, GetParameterVariableName(p), BaseSettings.CodeGeneratorSettings, this)).ToList(),
                });
            }).ToList();

            return(operations);
        }
Exemplo n.º 4
0
        public void When_generating_operation_id()
        {
            //// Arrange
            var document = new SwaggerDocument();
            document.Paths["path"] = new SwaggerOperations
            {
                {
                    SwaggerOperationMethod.Get,
                    new SwaggerOperation { }
                },
                {
                    SwaggerOperationMethod.Post,
                    new SwaggerOperation { }
                }
            };

            //// Act
            document.GenerateOperationIds();

            //// Assert
            Assert.IsTrue(document.Operations.GroupBy(o => o.Operation.OperationId).All(g => g.Count() == 1));
        }
Exemplo n.º 5
0
        internal List<OperationModel> GetOperations(SwaggerDocument document)
        {
            document.GenerateOperationIds();

            var operations = document.Paths
                .SelectMany(pair => pair.Value.Select(p => new { Path = pair.Key.Trim('/'), HttpMethod = p.Key, Operation = p.Value }))
                .Select(tuple =>
                {
                    var operation = tuple.Operation;
                    var exceptionSchema = (Resolver as SwaggerToCSharpTypeResolver)?.ExceptionSchema;
                    var responses = operation.Responses.Select(response => new ResponseModel(response, exceptionSchema, this)).ToList();

                    var defaultResponse = responses.SingleOrDefault(r => r.StatusCode == "default");
                    if (defaultResponse != null)
                        responses.Remove(defaultResponse);

                    return new OperationModel
                    {
                        Path = tuple.Path,
                        HttpMethod = tuple.HttpMethod,
                        Operation = tuple.Operation,
                        OperationName = BaseSettings.OperationNameGenerator.GetOperationName(document, tuple.Path, tuple.HttpMethod, tuple.Operation),

                        ResultType = GetResultType(operation),
                        HasResultType = HasResultType(operation),
                        ResultDescription = GetResultDescription(operation),

                        ExceptionType = GetExceptionType(operation),
                        HasFormParameters = operation.ActualParameters.Any(p => p.Kind == SwaggerParameterKind.FormData),
                        Responses = responses,
                        DefaultResponse = defaultResponse,
                        Parameters = operation.ActualParameters.Select(p => new ParameterModel(
                            ResolveParameterType(p), operation, p, p.Name, GetParameterVariableName(p), BaseSettings.CodeGeneratorSettings, this)).ToList(),
                    };
                }).ToList();
            return operations;
        }