internal static string GetTrailingChar(TrailingCharType trailingChar) { return(trailingChar switch { TrailingCharType.None => string.Empty, TrailingCharType.Comma => ",", TrailingCharType.SemiColon => ";", TrailingCharType.Colon => ":", _ => throw new ArgumentOutOfRangeException(nameof(trailingChar), trailingChar, null) });
public static int AppendModelSimplePropertyForString( int indentSpaces, StringBuilder sb, string propertyName, KeyValuePair <string, OpenApiSchema> schemaProperty, string propertyValueGenerated, int countString, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { if ("null".Equals(propertyValueGenerated, StringComparison.Ordinal)) { if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy, propertyName, "null", trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = null,"); } } else { if (!schemaProperty.Value.IsFormatTypeEmail() && !schemaProperty.Value.IsRuleValidationString()) { if (countString > 0) { propertyValueGenerated = $"{propertyValueGenerated}{countString}"; } countString++; } if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, propertyName, propertyValueGenerated, trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = \"{propertyValueGenerated}\","); } } return(countString); }
public static void AppendModelSimplePropertyForIFormFile( int indentSpaces, StringBuilder sb, string propertyName, string propertyValueGenerated, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { if (asJsonBody) { throw new NotSupportedException("IFormFile"); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = GetTestFile(),"); } }
public static void AppendModelSimplePropertyDefault( int indentSpaces, StringBuilder sb, string propertyName, string propertyValueGenerated, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy, propertyName, propertyValueGenerated, trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = {propertyValueGenerated},"); } }
public static void AppendModelSimplePropertyForDateTimeOffset( int indentSpaces, StringBuilder sb, string propertyName, string propertyValueGenerated, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, propertyName, propertyValueGenerated, trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = DateTimeOffset.Parse(\"{propertyValueGenerated}\"),"); } }
public static int AppendModelSimpleProperty( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, KeyValuePair <string, OpenApiSchema> schemaProperty, string dataType, bool isRequired, string propertyValueGenerated, int countString, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { ArgumentNullException.ThrowIfNull(endpointMethodMetadata); ArgumentNullException.ThrowIfNull(propertyValueGenerated); var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper(); var isHandled = false; if (isRequired && OpenApiDataTypeConstants.Array.Equals(dataType, StringComparison.OrdinalIgnoreCase)) { var itemsDataType = schemaProperty.Value.Items.GetDataType(); if (asJsonBody) { switch (itemsDataType) { case OpenApiDataTypeConstants.String: sb.AppendLine(indentSpaces, $"sb.AppendLine(\" {WrapInQuotes(propertyName)}: [ {WrapInQuotes("Hallo")}, {WrapInQuotes("World")} ]{GenerateCodeHelper.GetTrailingChar(trailingChar)}\");"); isHandled = true; break; case OpenApiDataTypeConstants.Integer: sb.AppendLine(indentSpaces, $"sb.AppendLine(\" {WrapInQuotes(propertyName)}: [ 42, 17 ]{GenerateCodeHelper.GetTrailingChar(trailingChar)}\");"); isHandled = true; break; case OpenApiDataTypeConstants.Boolean: sb.AppendLine(indentSpaces, $"sb.AppendLine(\" {WrapInQuotes(propertyName)}: [ true, false, true ]{GenerateCodeHelper.GetTrailingChar(trailingChar)}\");"); isHandled = true; break; case "IFormFile": throw new NotSupportedException("IFormFile is not supported when working with Json."); } } else { switch (itemsDataType) { case OpenApiDataTypeConstants.String: sb.AppendLine(indentSpaces + 4, $"{propertyName} = new List<string>() {{ \"Hallo\", \"World\" }},"); isHandled = true; break; case OpenApiDataTypeConstants.Integer: sb.AppendLine(indentSpaces + 4, $"{propertyName} = new List<int>() {{ 42, 17 }},"); isHandled = true; break; case OpenApiDataTypeConstants.Boolean: sb.AppendLine(indentSpaces + 4, $"{propertyName} = new List<bool>() {{ true, false, true }},"); isHandled = true; break; case "IFormFile": sb.AppendLine(indentSpaces + 4, $"{propertyName} = GetTestFiles(),"); isHandled = true; break; } } } if (isHandled) { return(countString); } switch (dataType) { case "string": countString = AppendModelSimplePropertyForString( indentSpaces, sb, propertyName, schemaProperty, propertyValueGenerated, countString, asJsonBody, depthHierarchy, trailingChar); break; case "DateTimeOffset": AppendModelSimplePropertyForDateTimeOffset( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; case "Guid": AppendModelSimplePropertyForGuid( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; case "Uri": AppendModelSimplePropertyForUri( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; case "IFormFile": AppendModelSimplePropertyForIFormFile( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; default: var enumDataType = GetDataTypeIfEnum(schemaProperty, endpointMethodMetadata.ComponentsSchemas); if (enumDataType is null) { AppendModelSimplePropertyDefault( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); } else { if (propertyValueGenerated.Contains('=', StringComparison.Ordinal)) { propertyValueGenerated = propertyValueGenerated.Split('=').First().Trim(); } if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, propertyName, propertyValueGenerated, trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = {enumDataType}.{propertyValueGenerated},"); } } break; } return(countString); }
public static int AppendModelSimpleProperty( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, KeyValuePair <string, OpenApiSchema> schemaProperty, string dataType, string propertyValueGenerated, int countString, bool asJsonBody, int depthHierarchy, TrailingCharType trailingChar) { var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper(); switch (dataType) { case "string": countString = AppendModelSimplePropertyForString( indentSpaces, sb, propertyName, schemaProperty, propertyValueGenerated, countString, asJsonBody, depthHierarchy, trailingChar); break; case "DateTimeOffset": AppendModelSimplePropertyForDateTimeOffset( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; case "Guid": AppendModelSimplePropertyForGuid( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; case "Uri": AppendModelSimplePropertyForUri( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); break; default: var enumDataType = GetDataTypeIfEnum(schemaProperty, endpointMethodMetadata.ComponentsSchemas); if (enumDataType == null) { AppendModelSimplePropertyDefault( indentSpaces, sb, propertyName, propertyValueGenerated, asJsonBody, depthHierarchy, trailingChar); } else { if (propertyValueGenerated.Contains("=", StringComparison.Ordinal)) { propertyValueGenerated = propertyValueGenerated.Split('=').First().Trim(); } if (asJsonBody) { sb.AppendLine( indentSpaces, WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, propertyName, propertyValueGenerated, trailingChar)); } else { sb.AppendLine( indentSpaces + 4, $"{propertyName} = {enumDataType}.{propertyValueGenerated},"); } } break; } return(countString); }
public static void AppendVarDataEqualNewListOfModel( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, KeyValuePair <string, OpenApiSchema> schemaProperty, TrailingCharType trailingChar, int maxItemsForList, int depthHierarchy, int maxDepthHierarchy, KeyValuePair <string, OpenApiSchema>?badPropertySchema, bool asJsonBody) { ArgumentNullException.ThrowIfNull(sb); var modelName = schemaProperty.Value.GetModelName(); var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName); if (depthHierarchy > maxDepthHierarchy) { if (asJsonBody) { // TODO Missing Json support. } else { sb.AppendLine( indentSpaces, $"var {schemaProperty.Key} = new List<{renderModelName}>(){GenerateCodeHelper.GetTrailingChar(trailingChar)}"); return; } } if (!asJsonBody) { GenerateXunitTestPartsHelper.AppendPartVarDataEqualNewListOf( indentSpaces, sb, schemaProperty.Key, renderModelName); sb.AppendLine(); sb.AppendLine(indentSpaces, "{"); } var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(modelName); for (var i = 0; i < maxItemsForList; i++) { var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, i, maxItemsForList); var indentSpacesForItem = indentSpaces + 4; if (!asJsonBody) { indentSpacesForItem = indentSpaces + 4; GenerateXunitTestPartsHelper.AppendPartDataNew(indentSpacesForItem, sb); } AppendModel( indentSpacesForItem, sb, endpointMethodMetadata, modelName, modelSchema, trailingCharForProperty, i + 1, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } if (!asJsonBody) { sb.AppendLine( indentSpaces, $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}"); } }
public static void AppendDataEqualNewListOfModel( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, KeyValuePair <string, OpenApiSchema> schemaProperty, TrailingCharType trailingChar, int maxItemsForList, int depthHierarchy, int maxDepthHierarchy, KeyValuePair <string, OpenApiSchema>?badPropertySchema, bool asJsonBody) { var modelName = schemaProperty.Value.GetModelName(); var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName); var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper(); var jsonSpacesCount = (depthHierarchy * 2) + 2; if (depthHierarchy > maxDepthHierarchy) { if (asJsonBody) { // TODO Missing Json support. } else { sb.AppendLine( indentSpaces, $"{propertyName} = new List<{renderModelName}>()" + GenerateCodeHelper.GetTrailingChar(trailingChar)); return; } } if (asJsonBody) { var useForBadRequest = badPropertySchema is not null && schemaProperty.Key.Equals(badPropertySchema.Value.Key, StringComparison.Ordinal); if (useForBadRequest) { sb.AppendLine( indentSpaces - jsonSpacesCount, GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy - 1, propertyName, "null", trailingChar)); return; } sb.AppendLine( indentSpaces - jsonSpacesCount, GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy - 1, propertyName, "[", TrailingCharType.None)); } else { GenerateXunitTestPartsHelper.AppendPartDataEqualNewListOf( indentSpaces, sb, propertyName, modelName); sb.AppendLine(); sb.AppendLine(indentSpaces, "{"); } var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(modelName); var currentIndentSpaces = asJsonBody ? indentSpaces - jsonSpacesCount : indentSpaces + 4; for (int i = 0; i < maxItemsForList; i++) { var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, i, maxItemsForList); if (!asJsonBody) { GenerateXunitTestPartsHelper.AppendPartDataNew(currentIndentSpaces, sb); } AppendModel( currentIndentSpaces, sb, endpointMethodMetadata, modelName, modelSchema, trailingCharForProperty, i + 1, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } if (asJsonBody) { var jsonSpaces = string.Empty.PadLeft((depthHierarchy - 1) * 2); sb.AppendLine( indentSpaces - jsonSpacesCount, GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces} ]{GenerateCodeHelper.GetTrailingChar(trailingChar)}")); } else { sb.AppendLine( indentSpaces, $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}"); } }
public static void AppendModelComplexProperty( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, KeyValuePair <string, OpenApiSchema> schemaProperty, string dataType, TrailingCharType trailingChar, int itemNumber, int maxItemsForList, int depthHierarchy, int maxDepthHierarchy, KeyValuePair <string, OpenApiSchema>?badPropertySchema, bool asJsonBody) { var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper(); if (depthHierarchy > maxDepthHierarchy) { if (asJsonBody) { // TODO Missing Json support. } else { sb.AppendLine( indentSpaces, $"{propertyName} = null{GenerateCodeHelper.GetTrailingChar(trailingChar)}"); return; } } if (!asJsonBody) { indentSpaces += 4; GenerateXunitTestPartsHelper.AppendPartDataEqualNew( indentSpaces, sb, propertyName); } var schemaPropertyValue = schemaProperty.Value; if (schemaProperty.Value.Properties.Count == 0 && schemaProperty.Value.OneOf.Count > 0) { schemaPropertyValue = schemaProperty.Value.OneOf.First(); } var modelName = schemaProperty.Key.EnsureFirstCharacterToUpper(); AppendModel( indentSpaces, sb, endpointMethodMetadata, dataType, schemaPropertyValue, trailingChar, itemNumber, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody, modelName); }
public static void AppendModel( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, string modelName, OpenApiSchema schema, TrailingCharType trailingChar, int itemNumber, int maxItemsForList, int depthHierarchy, int maxDepthHierarchy, KeyValuePair <string, OpenApiSchema>?badPropertySchema, bool asJsonBody, string?parentModelNameToJsonBody = null) { ArgumentNullException.ThrowIfNull(sb); ArgumentNullException.ThrowIfNull(schema); var countString = 1; var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName); var jsonSpaces = string.Empty.PadLeft(depthHierarchy * 2); if (asJsonBody) { sb.AppendLine( indentSpaces, string.IsNullOrEmpty(parentModelNameToJsonBody) ? GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}{{") : GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}\\\"{parentModelNameToJsonBody}\\\": {{")); } else { sb.AppendLine(renderModelName); sb.AppendLine(indentSpaces, "{"); } foreach (var schemaProperty in schema.Properties) { var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, schemaProperty, schema.Properties); var useForBadRequest = badPropertySchema is not null && schemaProperty.Key.Equals(badPropertySchema.Value.Key, StringComparison.Ordinal); var dataType = schemaProperty.Value.GetDataType(); var propertyValueGenerated = GenerateXunitTestPartsHelper.PropertyValueGenerator( schemaProperty, endpointMethodMetadata.ComponentsSchemas, useForBadRequest, itemNumber, customValue: null); if ("NEW-INSTANCE-LIST".Equals(propertyValueGenerated, StringComparison.Ordinal)) { AppendDataEqualNewListOfModel( indentSpaces + 4, sb, endpointMethodMetadata, schemaProperty, trailingCharForProperty, maxItemsForList, depthHierarchy + 1, maxDepthHierarchy, badPropertySchema, asJsonBody); } else if ("NEW-INSTANCE".Equals(propertyValueGenerated, StringComparison.Ordinal)) { AppendModelComplexProperty( indentSpaces, sb, endpointMethodMetadata, schemaProperty, dataType, trailingCharForProperty, itemNumber, maxItemsForList, depthHierarchy + 1, maxDepthHierarchy, badPropertySchema, asJsonBody); } else { var countResult = GenerateXunitTestPartsHelper.AppendModelSimpleProperty( indentSpaces, sb, endpointMethodMetadata, schemaProperty, dataType, schema.Required.Contains(schemaProperty.Key), propertyValueGenerated, countString, asJsonBody, depthHierarchy, trailingCharForProperty); if (countResult > 1) { countString += 1; } } } sb.AppendLine( indentSpaces, asJsonBody ? GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}") : $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}"); }
internal static string GetTrailingChar( TrailingCharType trailingChar) => trailingChar switch {
public void WrapInStringBuilderAppendLineWithKeyAndValueQuotes(string expected, int depthHierarchy, string key, string value, TrailingCharType trailingChar) => Assert.Equal( expected, GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, key, value, trailingChar));
public void GetTrailingCharForProperty(TrailingCharType expected, bool asJsonBody, int currentItem, int totalItems) => Assert.Equal( expected, GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, currentItem, totalItems));