コード例 #1
0
        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 != 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)}");
            }
        }
コード例 #2
0
        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)
        {
            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 (int i = 0; i < maxItemsForList; i++)
            {
                var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, i, maxItemsForList);
                int 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)}");
            }
        }