public ApiOperationSchemaMap(string schemaKey, SchemaMapLocatedAreaType locatedArea, string path, OperationType operationType, string?parentSchemaKey) { this.SchemaKey = schemaKey; this.LocatedArea = locatedArea; this.Path = path; this.OperationType = operationType; this.ParentSchemaKey = parentSchemaKey; this.SegmentName = OpenApiOperationSchemaMapHelper.GetSegmentName(this.Path); }
private static void Collect( IEnumerable <KeyValuePair <string, OpenApiSchema> > apiSchemas, SchemaMapLocatedAreaType areaType, string apiPath, OperationType apiOperationType, string parentApiSchema, List <ApiOperationSchemaMap> list) { foreach (var apiSchema in apiSchemas) { CollectSchema( apiSchema.Value, areaType, apiPath, apiOperationType, parentApiSchema, list); } }
private static void Collect( IDictionary <string, OpenApiSchema> componentsSchemas, IEnumerable <KeyValuePair <string, OpenApiSchema> > propertySchemas, SchemaMapLocatedAreaType areaType, string apiPath, OperationType apiOperationType, string parentApiSchema, List <ApiOperationSchemaMap> list) { foreach (var propertySchema in propertySchemas) { CollectSchema( componentsSchemas, propertySchema.Value, areaType, apiPath, apiOperationType, parentApiSchema, list); } }
private static void CollectSchema( OpenApiSchema?apiSchema, SchemaMapLocatedAreaType locatedArea, string apiPath, OperationType apiOperationType, string?parentApiSchema, List <ApiOperationSchemaMap> list) { if (apiSchema == null) { return; } var schemaKey = string.Empty; if (apiSchema.Reference?.Id != null) { schemaKey = apiSchema.Reference.Id.EnsureFirstCharacterToUpper(); } else if (apiSchema.Items?.Reference?.Id != null) { schemaKey = apiSchema.Items.Reference.Id.EnsureFirstCharacterToUpper(); } 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(); } } if (schemaKey.Length == 0 || schemaKey == nameof(ProblemDetails) || schemaKey.Equals(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.OrdinalIgnoreCase)) { return; } list.Add(new ApiOperationSchemaMap(schemaKey, locatedArea, apiPath, apiOperationType, parentApiSchema)); Collect( apiSchema.Properties.ToList(), locatedArea, apiPath, apiOperationType, schemaKey, list); if (apiSchema.Items != null && apiSchema.Type == OpenApiDataTypeConstants.Array && apiSchema.Items.Reference?.Id != null && schemaKey != apiSchema.Items.Reference.Id) { list.Add(new ApiOperationSchemaMap(apiSchema.Items.Reference.Id, locatedArea, apiPath, apiOperationType, schemaKey)); Collect( apiSchema.Items.Properties.ToList(), locatedArea, apiPath, apiOperationType, apiSchema.Items.Reference.Id, list); } }
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); } }
private static void CollectSchema( IDictionary <string, OpenApiSchema> componentsSchemas, OpenApiSchema?apiSchema, SchemaMapLocatedAreaType locatedArea, string apiPath, OperationType apiOperationType, string?parentApiSchema, List <ApiOperationSchemaMap> list) { if (apiSchema is null) { return; } string schemaKey; (schemaKey, apiSchema) = ConsolidateSchemaObjectTypes(apiSchema); if (schemaKey.Length == 0 || schemaKey == nameof(ProblemDetails) || schemaKey.Equals(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.OrdinalIgnoreCase)) { return; } var apiOperationSchemaMap = new ApiOperationSchemaMap(schemaKey, locatedArea, apiPath, apiOperationType, parentApiSchema); if (list.Any(x => x.Equals(apiOperationSchemaMap))) { return; } list.Add(apiOperationSchemaMap); if (apiSchema.Properties.Any()) { Collect( componentsSchemas, apiSchema.Properties.ToList(), locatedArea, apiPath, apiOperationType, schemaKey, list); } 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))) { string?subSchemaKey = null; if (!Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[0].Reference?.Id, StringComparison.OrdinalIgnoreCase)) { subSchemaKey = apiSchema.AllOf[0].GetModelName(); } if (!Microsoft.OpenApi.Models.NameConstants.Pagination.Equals(apiSchema.AllOf[1].Reference?.Id, StringComparison.OrdinalIgnoreCase)) { subSchemaKey = apiSchema.AllOf[1].GetModelName(); } if (subSchemaKey is not null) { var subApiSchema = componentsSchemas.Single(x => x.Key.Equals(schemaKey, StringComparison.OrdinalIgnoreCase)).Value; Collect( componentsSchemas, subApiSchema.Properties.ToList(), locatedArea, apiPath, apiOperationType, subSchemaKey, list); } } else if (apiSchema.IsTypeArray() && apiSchema.Items?.Reference?.Id is not null) { var subSchemaKey = apiSchema.Items.GetModelName(); var subApiOperationSchemaMap = new ApiOperationSchemaMap(subSchemaKey, locatedArea, apiPath, apiOperationType, parentApiSchema); if (!list.Any(x => x.Equals(subApiOperationSchemaMap))) { list.Add(subApiOperationSchemaMap); var subApiSchema = componentsSchemas.Single(x => x.Key.Equals(subSchemaKey, StringComparison.OrdinalIgnoreCase)).Value; if (subApiSchema.Properties.Any()) { Collect( componentsSchemas, subApiSchema.Properties.ToList(), locatedArea, apiPath, apiOperationType, subSchemaKey, list); } } } }
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) { var trailingChar = TrailingCharType.SemiColon; if (asJsonBody) { trailingChar = TrailingCharType.None; } switch (locatedAreaType) { case SchemaMapLocatedAreaType.Parameter: break; case SchemaMapLocatedAreaType.RequestBody: if (schema.Type == OpenApiDataTypeConstants.Array) { int indentSpacesForData = indentSpaces; if (asJsonBody) { sb.AppendLine(indentSpaces, "var sb = new StringBuilder();"); indentSpacesForData = indentSpacesForData - 4; } 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.Item1 == httpStatusCode); if (GenerateXunitTestPartsHelper.IsListKind(contractReturnTypeName.Item2)) { AppendVarDataEqualNewListOfModel( indentSpaces, sb, endpointMethodMetadata, new KeyValuePair<string, OpenApiSchema>("data", contractReturnTypeName.Item3!), trailingChar, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } else { GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb); AppendModel( indentSpaces, sb, endpointMethodMetadata, contractReturnTypeName.Item2, contractReturnTypeName.Item3!, trailingChar, 0, maxItemsForList, depthHierarchy, maxDepthHierarchy, badPropertySchema, asJsonBody); } break; default: throw new ArgumentOutOfRangeException(nameof(locatedAreaType), locatedAreaType, null); } }