private static (string, OpenApiSchema) ConsolidateSchemaObjectTypes( OpenApiSchema apiSchema) { var schemaKey = string.Empty; if (apiSchema.Reference?.Id is not null) { schemaKey = apiSchema.Reference.Id.EnsureFirstCharacterToUpper(); } else if (apiSchema.IsTypeArray() && apiSchema.Items?.Reference?.Id is not null) { schemaKey = apiSchema.Items.Reference.Id.EnsureFirstCharacterToUpper(); apiSchema = apiSchema.Items; } else if (apiSchema.OneOf.Any() && apiSchema.OneOf.First().Reference?.Id is not null) { schemaKey = apiSchema.OneOf.First().Reference.Id.EnsureFirstCharacterToUpper(); apiSchema = apiSchema.OneOf.First(); } else if (apiSchema.AllOf.Count == 2 && (Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[0].Reference?.Id, StringComparison.OrdinalIgnoreCase) || Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[1].Reference?.Id, StringComparison.OrdinalIgnoreCase))) { if (!Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[0].Reference?.Id, StringComparison.OrdinalIgnoreCase)) { schemaKey = apiSchema.AllOf[0].GetModelName(); } if (!Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[1].Reference?.Id, StringComparison.OrdinalIgnoreCase)) { schemaKey = apiSchema.AllOf[1].GetModelName(); } } return(schemaKey, apiSchema); }
public static void AppendVarDataModelOrListOfModel( int indentSpaces, StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, OpenApiSchema schema, HttpStatusCode httpStatusCode, SchemaMapLocatedAreaType locatedAreaType, KeyValuePair <string, OpenApiSchema>?badPropertySchema = null, bool asJsonBody = false, int maxItemsForList = 3, int depthHierarchy = 0, int maxDepthHierarchy = 2) { ArgumentNullException.ThrowIfNull(endpointMethodMetadata); var trailingChar = TrailingCharType.SemiColon; if (asJsonBody) { trailingChar = TrailingCharType.None; } switch (locatedAreaType) { case SchemaMapLocatedAreaType.Parameter: break; case SchemaMapLocatedAreaType.RequestBody: if (schema.IsTypeArray()) { var indentSpacesForData = indentSpaces; if (asJsonBody) { sb.AppendLine(indentSpaces, "var sb = new StringBuilder();"); indentSpacesForData -= 4; } if (schema.HasItemsWithFormatTypeBinary()) { if (asJsonBody) { throw new NotSupportedException("JSON not supported when RequestBody is type Array and format type is Binary."); } sb.AppendLine(indentSpaces, "var data = GetTestFiles();"); } else { AppendVarDataEqualNewListOfModel( indentSpacesForData, sb, endpointMethodMetadata, new KeyValuePair <string, OpenApiSchema>("data", schema), trailingChar, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } if (asJsonBody) { sb.AppendLine(indentSpaces, "var data = sb.ToString();"); } } else { if (asJsonBody) { sb.AppendLine(indentSpaces, "var sb = new StringBuilder();"); } else { GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb); } var modelName = schema.GetModelName(); AppendModel( indentSpaces, sb, endpointMethodMetadata, modelName, schema, trailingChar, 0, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); if (asJsonBody) { sb.AppendLine(indentSpaces, "var data = sb.ToString();"); } } break; case SchemaMapLocatedAreaType.Response: var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.StatusCode == httpStatusCode); if (GenerateXunitTestPartsHelper.IsListKind(contractReturnTypeName.FullModelName)) { AppendVarDataEqualNewListOfModel( indentSpaces, sb, endpointMethodMetadata, new KeyValuePair <string, OpenApiSchema>("data", contractReturnTypeName.Schema !), trailingChar, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } else { GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb); AppendModel( indentSpaces, sb, endpointMethodMetadata, contractReturnTypeName.FullModelName, contractReturnTypeName.Schema !, trailingChar, 0, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } break; default: throw new ArgumentOutOfRangeException(nameof(locatedAreaType), locatedAreaType, message: null); } }