Esempio n. 1
0
        public bool GenerateCode()
        {
            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            NamespaceDeclarationSyntax @namespace;

            if (ApiSchema.IsSchemaEnumOrPropertyEnum())
            {
                IsEnum = true;

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

                var apiEnumSchema = ApiSchema.GetEnumSchema();

                // Create an enum
                var enumDeclaration = SyntaxEnumFactory.Create(apiEnumSchema.Item1.EnsureFirstCharacterToUpper(), apiEnumSchema.Item2);

                if (enumDeclaration.HasAttributeOfAttributeType(typeof(FlagsAttribute)))
                {
                    // Add using statement to compilationUnit
                    compilationUnit = compilationUnit.AddUsingStatements(new[] { "System" });
                }

                if (enumDeclaration.HasAttributeOfAttributeType(typeof(SuppressMessageAttribute)))
                {
                    // Add using statement to compilationUnit
                    compilationUnit = compilationUnit.AddUsingStatements(new[] { "System.Diagnostics.CodeAnalysis" });
                }

                // Add the enum to the namespace.
                @namespace = @namespace.AddMembers(enumDeclaration);
            }
            else
            {
                // Create a namespace
                @namespace = IsSharedContract
                    ? SyntaxProjectFactory.CreateNamespace(ApiProjectOptions, NameConstants.Contracts)
                    : SyntaxProjectFactory.CreateNamespace(ApiProjectOptions, NameConstants.Contracts, FocusOnSegmentName);

                // Create class
                var classDeclaration = SyntaxClassDeclarationFactory.Create(ApiSchemaKey.EnsureFirstCharacterToUpper())
                                       .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                       .WithLeadingTrivia(SyntaxDocumentationFactory.Create(ApiSchema));

                // Create class-properties and add to class
                if (ApiSchema.Properties != null)
                {
                    if (ApiSchema.Type == OpenApiDataTypeConstants.Array)
                    {
                        var(key, _) = ApiProjectOptions.Document.Components.Schemas.FirstOrDefault(x => x.Key.Equals(ApiSchema.Title, StringComparison.OrdinalIgnoreCase));
                        if (string.IsNullOrEmpty(ApiSchema.Title))
                        {
                            ApiSchema.Title = ApiSchemaKey;
                            key             = ApiSchemaKey;
                        }

                        if (string.IsNullOrEmpty(ApiSchema.Items.Title))
                        {
                            ApiSchema.Items.Title = ApiSchemaKey;
                        }

                        var title = key != null
                            ? $"{ApiSchema.Title.EnsureFirstCharacterToUpperAndSingular()}List"
                            : ApiSchema.Title.EnsureFirstCharacterToUpper();

                        var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateListAuto(ApiSchema.Items.Title, title)
                                                  .WithLeadingTrivia(SyntaxDocumentationFactory.CreateSummary($"A list of {ApiSchema.Items.Title}."));
                        classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                    }
                    else
                    {
                        foreach (var property in ApiSchema.Properties)
                        {
                            var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                                property,
                                ApiSchema.Required,
                                ApiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes)
                                                      .WithLeadingTrivia(SyntaxDocumentationFactory.Create(property.Value));
                            classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                        }
                    }

                    var methodDeclaration = SyntaxMethodDeclarationFactory.CreateToStringMethod(ApiSchema.Properties);
                    if (methodDeclaration != null)
                    {
                        methodDeclaration = methodDeclaration.WithLeadingTrivia(SyntaxDocumentationFactory.CreateForOverrideToString());
                        classDeclaration  = classDeclaration.AddMembers(methodDeclaration);
                    }
                }

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

                // 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);
        }
Esempio n. 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);
    }
Esempio n. 3
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);
        }
        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);
        }
Esempio n. 5
0
    private NamespaceDeclarationSyntax GenerateCodeForOtherThanEnum(
        ref CompilationUnitSyntax?compilationUnit)
    {
        // Create a namespace
        var @namespace = IsSharedContract
            ? SyntaxProjectFactory.CreateNamespace(ApiProjectOptions, NameConstants.Contracts)
            : SyntaxProjectFactory.CreateNamespace(ApiProjectOptions, NameConstants.Contracts, FocusOnSegmentName);

        // Create class
        var classDeclaration = SyntaxClassDeclarationFactory.Create(ApiSchemaKey.EnsureFirstCharacterToUpper())
                               .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                               .WithLeadingTrivia(SyntaxDocumentationFactory.Create(ApiSchema));

        var hasAnyPropertiesAsArrayWithFormatTypeBinary = ApiSchema.HasAnyPropertiesAsArrayWithFormatTypeBinary();

        // Create class-properties and add to class
        if (ApiSchema.Properties is not null)
        {
            if (ApiSchema.IsTypeArray() ||
                hasAnyPropertiesAsArrayWithFormatTypeBinary)
            {
                var(key, _) = ApiProjectOptions.Document.Components.Schemas.FirstOrDefault(x =>
                                                                                           x.Key.Equals(ApiSchema.Title, StringComparison.OrdinalIgnoreCase));
                if (string.IsNullOrEmpty(ApiSchema.Title))
                {
                    ApiSchema.Title = ApiSchemaKey;
                    key             = ApiSchemaKey;
                }

                if (ApiSchema.Items is not null &&
                    string.IsNullOrEmpty(ApiSchema.Items.Title))
                {
                    ApiSchema.Items.Title = ApiSchemaKey;
                }

                var title = key is not null
                    ? $"{ApiSchema.Title.EnsureFirstCharacterToUpperAndSingular()}List"
                    : ApiSchema.Title.EnsureFirstCharacterToUpper();

                var propertyDeclaration = hasAnyPropertiesAsArrayWithFormatTypeBinary
                    ? SyntaxPropertyDeclarationFactory.CreateListAuto("IFormFile", ApiSchema.ExtractPropertyNameWhenHasAnyPropertiesOfArrayWithFormatTypeBinary())
                                          .WithLeadingTrivia(
                    SyntaxDocumentationFactory.CreateSummary("A list of File(s)."))
                    : SyntaxPropertyDeclarationFactory.CreateListAuto(ApiSchema.Items !.Title, title)
                                          .WithLeadingTrivia(
                    SyntaxDocumentationFactory.CreateSummary($"A list of {ApiSchema.Items.Title}."));

                classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
            }
            else
            {
                foreach (var property in ApiSchema.Properties)
                {
                    var propertyDeclaration = SyntaxPropertyDeclarationFactory.CreateAuto(
                        property,
                        ApiSchema.Required,
                        ApiProjectOptions.UseNullableReferenceTypes)
                                              .WithLeadingTrivia(SyntaxDocumentationFactory.Create(property.Value));
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }

            var methodDeclaration = SyntaxMethodDeclarationFactory.CreateToStringMethod(ApiSchema.Properties);
            if (methodDeclaration is not null)
            {
                methodDeclaration = methodDeclaration.WithLeadingTrivia(SyntaxDocumentationFactory.CreateForOverrideToString());
                classDeclaration  = classDeclaration.AddMembers(methodDeclaration);
            }
        }

        // Add using statement to compilationUnit
        compilationUnit = compilationUnit !.AddUsingStatements(ProjectApiFactory.CreateUsingListForContractModel(ApiSchema));

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