private static bool AppendNewRequestModel( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, HttpStatusCode httpStatusCode) { var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchemaByFirstMediaType(); if (schema is null) { return(false); } GenerateXunitTestHelper.AppendVarDataModelOrListOfModel( indentSpaces, sb, endpointMethodMetadata, schema, httpStatusCode, SchemaMapLocatedAreaType.RequestBody); return(true); }
private static void AppendNewRequestModelForBadRequest( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, HttpStatusCode httpStatusCode, KeyValuePair <string, OpenApiSchema> badPropertySchema) { var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchemaByFirstMediaType(); if (schema is null) { return; } GenerateXunitTestHelper.AppendVarDataModelOrListOfModel( indentSpaces, sb, endpointMethodMetadata, schema, httpStatusCode, SchemaMapLocatedAreaType.RequestBody, badPropertySchema, asJsonBody: true); }
private static void AppendContentForExecuteAsynchronous( StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, HttpStatusCode httpStatusCode) { var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.StatusCode == httpStatusCode); var returnTypeName = contractReturnTypeName.FullModelName; switch (returnTypeName) { case "string": sb.AppendLine( 12, httpStatusCode == HttpStatusCode.Created ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());" : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(\"Hallo world\"));"); break; case "bool": sb.AppendLine( 12, httpStatusCode == HttpStatusCode.Created ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());" : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(true));"); break; case "int": case "long": sb.AppendLine( 12, httpStatusCode == HttpStatusCode.Created ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());" : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(42));"); break; case "float": case "double": sb.AppendLine( 12, httpStatusCode == HttpStatusCode.Created ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());" : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(42.2));"); break; case "Guid": sb.AppendLine( 12, httpStatusCode == HttpStatusCode.Created ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());" : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(System.Guid.NewGuid()));"); break; case "byte[]": sb.AppendLine(12, "var bytes = System.Text.Encoding.UTF8.GetBytes(\"Hello World\");"); sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(bytes, \"dummy.txt\"));"); break; default: { var singleReturnTypeName = OpenApiDocumentSchemaModelNameHelper.GetRawModelName(returnTypeName); var simpleTypePair = SimpleTypeHelper.BeautifySimpleTypeLookup.FirstOrDefault(x => x.Value == singleReturnTypeName); if (simpleTypePair.Key is not null) { GenerateXunitTestHelper.AppendVarDataListSimpleType( 12, sb, simpleTypePair.Value); sb.AppendLine(); } else { var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(singleReturnTypeName); GenerateXunitTestHelper.AppendVarDataModelOrListOfModel( 12, sb, endpointMethodMetadata, modelSchema, httpStatusCode, SchemaMapLocatedAreaType.Response); sb.AppendLine(); } if (contractReturnTypeName.Schema is null || GenerateXunitTestPartsHelper.IsListKind(returnTypeName)) { if (returnTypeName.StartsWith(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.Ordinal)) { if (endpointMethodMetadata.ContractParameter is not null) { var queryParameters = endpointMethodMetadata.ContractParameter.ApiOperation.Parameters.GetAllFromQuery(); var sPageSize = "10"; if (queryParameters.Find(x => x.Name.Equals("PageSize", StringComparison.OrdinalIgnoreCase)) is not null) { sPageSize = "parameters.PageSize"; } var sQueryString = "null"; if (queryParameters.Find(x => x.Name.Equals("QueryString", StringComparison.OrdinalIgnoreCase)) is not null) { sQueryString = "parameters.QueryString"; } var sContinuationToken = "null"; if (queryParameters.Find(x => x.Name.Equals("ContinuationToken", StringComparison.OrdinalIgnoreCase)) is not null) { sContinuationToken = "parameters.ContinuationToken"; } sb.AppendLine(12, $"var paginationData = new {contractReturnTypeName.FullModelName}(data, {sPageSize}, {sQueryString}, {sContinuationToken});"); } else { sb.AppendLine(12, $"var paginationData = new {contractReturnTypeName.FullModelName}(data, 10, null, null);"); } sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(paginationData));"); } else { sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(data));"); } }