public void GetModelName_EnsureFirstCharacterToUpper(string expected, OpenApiSchema openApiSchema, bool ensureFirstCharacterToUpper) { // Act var actual = openApiSchema.GetModelName(ensureFirstCharacterToUpper); // Assert Assert.Equal(expected, actual); }
public void GetModelName(string expected, OpenApiSchema openApiSchema) { // Act var actual = openApiSchema.GetModelName(); // Assert Assert.Equal(expected, actual); }
public static bool HasAnySharedModelOrEnum( this OpenApiSchema schema, List <ApiOperationSchemaMap> apiOperationSchemaMaps, bool includeProperties = true) { ArgumentNullException.ThrowIfNull(schema); if (!schema.IsObjectReferenceTypeDeclared()) { return(false); } if (schema.IsSchemaEnumOrPropertyEnum()) { return(true); } var modelName = schema.GetModelName(); if (apiOperationSchemaMaps.IsShared(modelName)) { return(true); } if (!includeProperties) { return(false); } if (!schema.HasAnyProperties()) { return(false); } foreach (var(_, openApiSchema) in schema.Properties) { if (openApiSchema.HasAnySharedModelOrEnum(apiOperationSchemaMaps)) { return(true); } if (openApiSchema.OneOf is not null && openApiSchema.OneOf.Count > 0 && openApiSchema.OneOf.Any(x => x.HasAnySharedModelOrEnum(apiOperationSchemaMaps))) { return(true); } if (openApiSchema.IsArrayReferenceTypeDeclared() && openApiSchema.Items.HasAnySharedModelOrEnum(apiOperationSchemaMaps)) { return(true); } } return(false); }
private static List <LogKeyValueItem> ValidateSchemaModelNameCasing( ApiOptionsValidation validationOptions, OpenApiSchema schema) { var logItems = new List <LogKeyValueItem>(); var logCategory = validationOptions.StrictMode ? LogCategoryType.Error : LogCategoryType.Warning; var modelName = schema.GetModelName(false); if (!modelName.IsCasingStyleValid(validationOptions.ModelNameCasingStyle)) { logItems.Add(LogItemHelper.Create(logCategory, ValidationRuleNameConstants.Schema06, $"Object '{modelName}' is not using {validationOptions.ModelNameCasingStyle}.")); } return(logItems); }
public static bool HasAnySharedModel( this OpenApiSchema schema, List <ApiOperationSchemaMap> apiOperationSchemaMaps) { ArgumentNullException.ThrowIfNull(schema); if (!schema.IsObjectReferenceTypeDeclared()) { return(false); } var modelName = schema.GetModelName(); if (apiOperationSchemaMaps.IsShared(modelName)) { return(true); } return(schema.HasAnyProperties() && schema.Properties.Any(x => HasAnySharedModel(x.Value, apiOperationSchemaMaps))); }
private static void AppendNewModel(int indentSpaces, StringBuilder sb, IDictionary <string, OpenApiSchema> componentsSchemas, OpenApiSchema schema, string?badRequestPropertyName, int itemNumber, string?variableName) { if (sb == null) { throw new ArgumentNullException(nameof(sb)); } if (componentsSchemas == null) { throw new ArgumentNullException(nameof(componentsSchemas)); } if (schema == null) { throw new ArgumentNullException(nameof(schema)); } int countString = 0; sb.AppendLine( indentSpaces, variableName == null ? $"new {schema.GetModelName()}" : $"var {variableName} = new {schema.GetModelName()}"); sb.AppendLine(indentSpaces, "{"); foreach (var schemaProperty in schema.Properties) { var useForBadRequest = !string.IsNullOrEmpty(badRequestPropertyName) && schemaProperty.Key.Equals(badRequestPropertyName, StringComparison.Ordinal); string dataType = schemaProperty.Value.GetDataType(); string propertyValueGenerated = PropertyValueGenerator(schemaProperty, componentsSchemas, useForBadRequest, itemNumber, null); switch (dataType) { case "string": if (!schemaProperty.Value.IsFormatTypeOfEmail()) { if (countString > 0) { propertyValueGenerated = $"{propertyValueGenerated}{countString}"; } countString++; } sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = \"{propertyValueGenerated}\","); break; case "DateTimeOffset": sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = DateTimeOffset.Parse(\"{propertyValueGenerated}\"),"); break; case "Guid": sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = Guid.Parse(\"{propertyValueGenerated}\"),"); break; case "Uri": sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = new Uri(\"{propertyValueGenerated}\"),"); break; default: string?enumDataType = GetDataTypeIfEnum(schemaProperty, componentsSchemas); if (enumDataType == null) { sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = {propertyValueGenerated},"); } else { if (propertyValueGenerated.Contains("=", StringComparison.Ordinal)) { propertyValueGenerated = propertyValueGenerated.Split('=').First().Trim(); } sb.AppendLine( indentSpaces + 4, $"{schemaProperty.Key.EnsureFirstCharacterToUpper()} = {enumDataType}.{propertyValueGenerated},"); } break; } } sb.AppendLine( indentSpaces, variableName == null ? "}," : "};"); }
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); } }
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); } }