public static void Generate( ILogger logger, HostProjectOptions hostProjectOptions, EndpointMethodMetadata endpointMethodMetadata) { ArgumentNullException.ThrowIfNull(logger); ArgumentNullException.ThrowIfNull(hostProjectOptions); ArgumentNullException.ThrowIfNull(endpointMethodMetadata); var sb = new StringBuilder(); AppendUsingStatements(sb, hostProjectOptions, endpointMethodMetadata); sb.AppendLine(); GenerateCodeHelper.AppendGeneratedCodeWarningComment(sb, hostProjectOptions.ToolNameAndVersion); AppendNamespaceAndClassStart(sb, hostProjectOptions, endpointMethodMetadata); AppendConstructor(sb, endpointMethodMetadata); AppendTestMethod(sb, endpointMethodMetadata); if (endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormData()) { AppendGetMultipartFormDataContentRequestMethod(sb, endpointMethodMetadata, endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormDataAndHasInlineSchemaFile()); } else if (endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartOctetStreamData()) { AppendGetSingleFormDataContentRequestMethod(sb, endpointMethodMetadata); } AppendNamespaceAndClassEnd(sb); SaveFile(logger, sb, hostProjectOptions, endpointMethodMetadata); }
private static void AppendTextContent( StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, HttpStatusCode testExpectedHttpStatusCode, ResponseTypeNameAndItemSchema contractReturnTypeName) { sb.AppendLine(8, "{"); if (endpointMethodMetadata.HasContractParameterAnyParametersOrRequestBody()) { sb.AppendLine(12, "// Arrange"); var headerParameters = endpointMethodMetadata.GetHeaderParameters(); if (headerParameters.Count > 0) { foreach (var headerParameter in headerParameters) { var propertyValueGenerated = PropertyValueGenerator(headerParameter, endpointMethodMetadata.ComponentsSchemas, useForBadRequest: false, customValue: null); sb.AppendLine( 12, $"HttpClient.DefaultRequestHeaders.Add(\"{headerParameter.Name}\", \"{propertyValueGenerated}\");"); } sb.AppendLine(); } var isContractParameterRequestBodyUsedAsMultipartOctetStreamData = endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartOctetStreamData(); if (isContractParameterRequestBodyUsedAsMultipartOctetStreamData) { sb.AppendLine(12, "var data = GetTestFile();"); } else { var isModelCreated = AppendNewRequestModel(12, sb, endpointMethodMetadata, contractReturnTypeName.StatusCode); if (!isModelCreated && endpointMethodMetadata.HttpOperation.IsRequestBodySupported()) { sb.AppendLine(12, "var data = \"{ }\";"); } } sb.AppendLine(); if (endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormData()) { AppendActHttpClientOperationForMultipartFormData( 12, sb, endpointMethodMetadata.HttpOperation, endpointMethodMetadata.GetRequestBodyModelName() !); } else if (isContractParameterRequestBodyUsedAsMultipartOctetStreamData) { AppendActHttpClientOperationForMultipartFormData( 12, sb, endpointMethodMetadata.HttpOperation, $"{endpointMethodMetadata.MethodName}{NameConstants.Request}"); } else { AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation, useData: true); } } else { AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation); } sb.AppendLine(); sb.AppendLine(12, "// Assert"); sb.AppendLine(12, "response.Should().NotBeNull();"); sb.AppendLine(12, $"response.StatusCode.Should().Be(HttpStatusCode.{testExpectedHttpStatusCode});"); if (testExpectedHttpStatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(contractReturnTypeName.FullModelName) && contractReturnTypeName.Schema is not null && !contractReturnTypeName.Schema.IsSimpleDataType() && !(endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartOctetStreamData() || endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormData())) { var modelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, contractReturnTypeName.FullModelName); sb.AppendLine(); sb.AppendLine(12, $"var responseData = await response.DeserializeAsync<{modelName}>(JsonSerializerOptions);"); sb.AppendLine(12, "responseData.Should().NotBeNull();"); } sb.AppendLine(8, "}"); }
private static List <string> GetUsingStatements( HostProjectOptions hostProjectOptions, EndpointMethodMetadata endpointMethodMetadata) { var systemList = new List <string> { "System.CodeDom.Compiler", "System.Net", "System.Threading.Tasks", }; if (endpointMethodMetadata.IsContractParameterRequestBodyUsingSystemNamespace()) { systemList.Add("System"); } if (endpointMethodMetadata.IsContractParameterRequestBodyUsingStringBuilder()) { systemList.Add("System.Text"); } if (endpointMethodMetadata.IsContractReturnTypeUsingList() || endpointMethodMetadata.IsContractParameterRequestBodyUsingSystemCollectionGenericNamespace()) { systemList.Add("System.Collections.Generic"); } var list = new List <string> { "Atc.XUnit", "FluentAssertions", "Xunit", }; if (endpointMethodMetadata.IsContractReturnTypeUsingPagination()) { list.Add("Atc.Rest.Results"); } if (!endpointMethodMetadata.IsContractReturnTypeUsingTaskName() && (endpointMethodMetadata.HasSharedModelOrEnumInContractParameterRequestBody() || endpointMethodMetadata.HasSharedModelOrEnumInContractReturnType(includeProperties: false))) { list.Add($"{hostProjectOptions.ProjectName}.Generated.Contracts"); } if (endpointMethodMetadata.IsContractParameterRequestBodyUsed() || endpointMethodMetadata.HasContractReturnTypeAsComplex()) { systemList.Add("System.Net.Http"); if (!endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartOctetStreamData() && !endpointMethodMetadata.IsContractReturnTypeUsingTaskName()) { list.Add($"{hostProjectOptions.ProjectName}.Generated.Contracts.{endpointMethodMetadata.SegmentName}"); } } else if (endpointMethodMetadata.HasContractReturnTypeAsComplexAsListOrPagination()) { systemList.Add("System.Net.Http"); } if (endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormData() || endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartOctetStreamData()) { list.Add("Microsoft.AspNetCore.Http"); } return(systemList .OrderBy(x => x) .Concat(list .OrderBy(x => x)) .ToList()); }