private StatementSyntax[] CreateInvokeExecuteAsyncMethodBlockLocalRequestBuilder()
    {
        var equalsClauseSyntax = SyntaxFactory.EqualsValueClause(
            SyntaxFactory.InvocationExpression(
                SyntaxMemberAccessExpressionFactory.Create(nameof(IHttpMessageFactory.FromTemplate), "httpMessageFactory"))
            .WithArgumentList(CreateOneStringArg($"{ApiProjectOptions.RouteBase}{ApiUrlPath}")));

        var requestBuilderSyntax = SyntaxFactory.LocalDeclarationStatement(
            SyntaxFactory.VariableDeclaration(
                SyntaxFactory.IdentifierName("var"))
            .WithVariables(
                SyntaxFactory.SingletonSeparatedList(
                    SyntaxFactory.VariableDeclarator(
                        SyntaxFactory.Identifier("requestBuilder"))
                    .WithInitializer(equalsClauseSyntax))));

        var result = new List <StatementSyntax>
        {
            requestBuilderSyntax,
        };

        if (HasParametersOrRequestBody)
        {
            if (GlobalPathParameters.Any())
            {
                result.AddRange(GlobalPathParameters.Select(CreateExpressionStatementForWithMethodParameterMap));
            }

            if (ApiOperation.Parameters is not null)
            {
                result.AddRange(ApiOperation.Parameters.Select(CreateExpressionStatementForWithMethodParameterMap));
            }

            var bodySchema = ApiOperation.RequestBody?.Content.GetSchemaByFirstMediaType();
            if (bodySchema is not null)
            {
                result.Add(CreateExpressionStatementForWithMethodBodyMap());
            }
        }

        return(result.ToArray());
    }
Пример #2
0
    public bool GenerateCode()
    {
        var parameterTypeName = ApiOperation.GetOperationName() + NameConstants.ContractParameters;

        // Create compilationUnit
        var compilationUnit = SyntaxFactory.CompilationUnit();

        // Create a namespace
        var @namespace = SyntaxProjectFactory.CreateNamespace(
            ApiProjectOptions,
            NameConstants.Contracts,
            FocusOnSegmentName);

        // Create class
        var classDeclaration = SyntaxClassDeclarationFactory.Create(parameterTypeName)
                               .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                               .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameters(ApiOperation, FocusOnSegmentName));

        // Add properties to the class
        if (GlobalPathParameters.Any())
        {
            foreach (var parameter in GlobalPathParameters)
            {
                var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                    parameter,
                    ApiProjectOptions.UseNullableReferenceTypes,
                    ApiProjectOptions.IsForClient)
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
            }
        }

        if (ApiOperation.Parameters is not null)
        {
            foreach (var parameter in ApiOperation.Parameters)
            {
                var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                    parameter,
                    ApiProjectOptions.UseNullableReferenceTypes,
                    ApiProjectOptions.IsForClient)
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
            }
        }

        var requestSchema = ApiOperation.RequestBody?.Content?.GetSchemaByFirstMediaType();

        if (ApiOperation.RequestBody is not null &&
            requestSchema is not null)
        {
            var isFormatTypeOfBinary      = requestSchema.IsFormatTypeBinary();
            var isItemsOfFormatTypeBinary = requestSchema.HasItemsWithFormatTypeBinary();

            var requestBodyType = "string?";
            if (requestSchema.Reference is not null)
            {
                requestBodyType = requestSchema.Reference.Id.EnsureFirstCharacterToUpper();
            }
            else if (isFormatTypeOfBinary)
            {
                requestBodyType = "IFormFile";
            }
            else if (isItemsOfFormatTypeBinary)
            {
                requestBodyType = "IFormFile";
            }
            else if (requestSchema.Items is not null)
            {
                requestBodyType = requestSchema.Items.Reference.Id.EnsureFirstCharacterToUpper();
            }

            PropertyDeclarationSyntax propertyDeclaration;
            if (requestSchema.IsTypeArray())
            {
                if (ApiProjectOptions.IsForClient)
                {
                    propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateListAuto(requestBodyType, NameConstants.Request)
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }
                else
                {
                    propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateListAuto(requestBodyType, NameConstants.Request);

                    propertyDeclaration = requestSchema.HasItemsWithFormatTypeBinary()
                        ? propertyDeclaration.AddFromFormAttribute()
                        : propertyDeclaration.AddFromBodyAttribute();

                    propertyDeclaration = propertyDeclaration
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }
            }
            else
            {
                if (ApiProjectOptions.IsForClient)
                {
                    propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameterLocation: null,
                        isNullable: false,
                        isRequired: true,
                        requestBodyType,
                        NameConstants.Request,
                        ApiProjectOptions.UseNullableReferenceTypes,
                        initializer: null)
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }
                else
                {
                    propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameterLocation: null,
                        isNullable: false,
                        isRequired: true,
                        requestBodyType,
                        NameConstants.Request,
                        ApiProjectOptions.UseNullableReferenceTypes,
                        initializer: null);

                    propertyDeclaration = requestSchema.HasAnyPropertiesWithFormatTypeBinary() || requestSchema.HasAnyPropertiesAsArrayWithFormatTypeBinary()
                        ? propertyDeclaration.AddFromFormAttribute()
                        : propertyDeclaration.AddFromBodyAttribute();

                    propertyDeclaration = propertyDeclaration
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }
            }

            classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
        }

        var methodDeclaration = SyntaxMethodDeclarationFactory.CreateToStringMethod(GlobalPathParameters, ApiOperation.Parameters, ApiOperation.RequestBody);

        if (methodDeclaration is not null)
        {
            methodDeclaration = methodDeclaration.WithLeadingTrivia(SyntaxDocumentationFactory.CreateForOverrideToString());
            classDeclaration  = classDeclaration.AddMembers(methodDeclaration);
        }

        // Add using statement to compilationUnit
        compilationUnit = compilationUnit.AddUsingStatements(
            ProjectApiFactory.CreateUsingListForContractParameter(
                GlobalPathParameters,
                ApiOperation.Parameters,
                ApiOperation.RequestBody,
                ApiProjectOptions.IsForClient));

        // Add the class to the namespace.
        @namespace = @namespace.AddMembers(classDeclaration);

        // Add namespace to compilationUnit
        compilationUnit = compilationUnit.AddMembers(@namespace);

        // Set code property
        Code = compilationUnit;
        return(true);
    }
Пример #3
0
        public bool GenerateCode()
        {
            var interfaceTypeName = "I" + ApiOperation.GetOperationName() + NameConstants.ContractHandler;
            var parameterTypeName = ApiOperation.GetOperationName() + NameConstants.ContractParameters;
            var resultTypeName    = ApiOperation.GetOperationName() + NameConstants.ContractResult;

            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Contracts,
                FocusOnSegmentName);

            // Create interface
            var interfaceDeclaration = SyntaxInterfaceDeclarationFactory.Create(interfaceTypeName)
                                       .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                       .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForInterface(ApiOperation, FocusOnSegmentName));

            // Create interface-method
            var methodDeclaration = SyntaxMethodDeclarationFactory.CreateInterfaceMethod(parameterTypeName, resultTypeName, HasParametersOrRequestBody)
                                    .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForInterfaceMethod(GlobalPathParameters.Any() || ApiOperation.HasParametersOrRequestBody()));

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(ProjectContractInterfaceFactory.CreateUsingList());

            // Add interface-method to interface
            interfaceDeclaration = interfaceDeclaration.AddMembers(methodDeclaration);

            // Add the interface to the namespace.
            @namespace = @namespace.AddMembers(interfaceDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }
        public bool GenerateCode()
        {
            var parameterTypeName = ApiOperation.GetOperationName() + NameConstants.ContractParameters;

            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Contracts,
                FocusOnSegmentName);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.Create(parameterTypeName)
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameters(ApiOperation, FocusOnSegmentName));

            // Add properties to the class
            if (GlobalPathParameters.Any())
            {
                foreach (var parameter in GlobalPathParameters)
                {
                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(parameter, ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes)
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            if (ApiOperation.Parameters != null)
            {
                foreach (var parameter in ApiOperation.Parameters)
                {
                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(parameter, ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes)
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            if (ApiOperation.RequestBody?.Content != null)
            {
                foreach (var item in ApiOperation.RequestBody.Content.Values)
                {
                    if (item.Schema.Reference == null)
                    {
                        continue;
                    }

                    var requestBodyType = item.Schema.Reference.Id.EnsureFirstCharacterToUpper();

                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        null,
                        false,
                        true,
                        requestBodyType,
                        NameConstants.Request,
                        ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                        null)
                                              .AddFromBodyAttribute()
                                              .AddValidationAttribute(new RequiredAttribute())
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            var methodDeclaration = SyntaxMethodDeclarationFactory.CreateToStringMethod(GlobalPathParameters, ApiOperation.Parameters, ApiOperation.RequestBody);

            if (methodDeclaration != null)
            {
                methodDeclaration = methodDeclaration.WithLeadingTrivia(SyntaxDocumentationFactory.CreateForOverrideToString());
                classDeclaration  = classDeclaration.AddMembers(methodDeclaration);
            }

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(ProjectContractPropertyFactory.CreateUsingList(GlobalPathParameters, ApiOperation.Parameters, ApiOperation.RequestBody));

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }
Пример #5
0
        public bool GenerateCode()
        {
            var parameterTypeName = ApiOperation.GetOperationName() + NameConstants.ContractParameters;

            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Contracts,
                FocusOnSegmentName);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.Create(parameterTypeName)
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameters(ApiOperation, FocusOnSegmentName));

            // Add properties to the class
            if (GlobalPathParameters.Any())
            {
                foreach (var parameter in GlobalPathParameters)
                {
                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameter,
                        ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                        ApiProjectOptions.ForClient)
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            if (ApiOperation.Parameters != null)
            {
                foreach (var parameter in ApiOperation.Parameters)
                {
                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameter,
                        ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                        ApiProjectOptions.ForClient)
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(parameter));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            var requestSchema = ApiOperation.RequestBody?.Content?.GetSchema();

            if (ApiOperation.RequestBody != null && requestSchema != null)
            {
                var requestBodyType = requestSchema.Reference != null
                    ? requestSchema.Reference.Id.EnsureFirstCharacterToUpper()
                    : requestSchema.Items.Reference.Id.EnsureFirstCharacterToUpper();

                PropertyDeclarationSyntax propertyDeclaration;
                if (requestSchema.Type == OpenApiDataTypeConstants.Array)
                {
                    propertyDeclaration = ApiProjectOptions.ForClient
                        ? SyntaxPropertyDeclarationFactory.CreateListAuto(
                        requestBodyType,
                        NameConstants.Request)
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody))
                        : SyntaxPropertyDeclarationFactory.CreateListAuto(
                        requestBodyType,
                        NameConstants.Request)
                                          .AddFromBodyAttribute()
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }
                else
                {
                    propertyDeclaration = ApiProjectOptions.ForClient
                        ? SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameterLocation: null,
                        isNullable: false,
                        isRequired: true,
                        requestBodyType,
                        NameConstants.Request,
                        ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                        initializer: null)
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody))
                        : SyntaxPropertyDeclarationFactory.CreateAuto(
                        parameterLocation: null,
                        isNullable: false,
                        isRequired: true,
                        requestBodyType,
                        NameConstants.Request,
                        ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                        initializer: null)
                                          .AddFromBodyAttribute()
                                          .AddValidationAttribute(new RequiredAttribute())
                                          .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForParameter(ApiOperation.RequestBody));
                }

                classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
            }

            var methodDeclaration = SyntaxMethodDeclarationFactory.CreateToStringMethod(GlobalPathParameters, ApiOperation.Parameters, ApiOperation.RequestBody);

            if (methodDeclaration != null)
            {
                methodDeclaration = methodDeclaration.WithLeadingTrivia(SyntaxDocumentationFactory.CreateForOverrideToString());
                classDeclaration  = classDeclaration.AddMembers(methodDeclaration);
            }

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(
                ProjectContractPropertyFactory.CreateUsingList(
                    GlobalPathParameters,
                    ApiOperation.Parameters,
                    ApiOperation.RequestBody,
                    ApiProjectOptions.ForClient));

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }