|
| 1 | +using Models.Request; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace Gemini.NET.Helpers |
| 5 | +{ |
| 6 | + public static class OpenApiSchemaGenerator |
| 7 | + { |
| 8 | + public static ResponseSchema AsOpenApiSchema<T>() |
| 9 | + { |
| 10 | + return ConvertToSchema(typeof(T), new HashSet<Type>()); |
| 11 | + } |
| 12 | + |
| 13 | + private static ResponseSchema ConvertToSchema(Type type, HashSet<Type> processedTypes) |
| 14 | + { |
| 15 | + if (processedTypes.Contains(type)) |
| 16 | + { |
| 17 | + return new ResponseSchema("object"); |
| 18 | + } |
| 19 | + processedTypes.Add(type); |
| 20 | + |
| 21 | + var schema = new ResponseSchema("object") |
| 22 | + { |
| 23 | + Properties = [], |
| 24 | + Required = [] |
| 25 | + }; |
| 26 | + |
| 27 | + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 28 | + foreach (var prop in properties) |
| 29 | + { |
| 30 | + string propName = prop.Name; |
| 31 | + Type propType = prop.PropertyType; |
| 32 | + ResponseSchema propSchema; |
| 33 | + |
| 34 | + // Xử lý đặc biệt cho SearchSuggestions |
| 35 | + if (propName == "SearchSuggestions") |
| 36 | + { |
| 37 | + propSchema = new ResponseSchema("string"); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + // Lấy kiểu của property theo định nghĩa OpenAPI |
| 42 | + var (openApiType, itemsSchema, enumValues) = GetOpenApiType(propType, processedTypes); |
| 43 | + propSchema = new ResponseSchema(openApiType); |
| 44 | + |
| 45 | + if (enumValues != null) |
| 46 | + { |
| 47 | + propSchema.Enum = enumValues; |
| 48 | + } |
| 49 | + if (openApiType == "array" && itemsSchema != null) |
| 50 | + { |
| 51 | + propSchema.Items = itemsSchema; |
| 52 | + } |
| 53 | + // Nếu kiểu là object (không phải primitive hay string) thì tiến hành convert nested object |
| 54 | + if (openApiType == "object" && !propType.IsPrimitive && propType != typeof(string)) |
| 55 | + { |
| 56 | + propSchema = ConvertToSchema(propType, processedTypes); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Nếu property được đánh dấu là required (ví dụ dùng RequiredMemberAttribute) |
| 61 | + if (Attribute.IsDefined(prop, typeof(System.Runtime.CompilerServices.RequiredMemberAttribute))) |
| 62 | + { |
| 63 | + schema.Required.Add(propName); |
| 64 | + } |
| 65 | + schema.Properties[propName] = propSchema; |
| 66 | + } |
| 67 | + |
| 68 | + if (schema.Required.Count == 0) |
| 69 | + { |
| 70 | + schema.Required = null; |
| 71 | + } |
| 72 | + |
| 73 | + if (schema.Properties.Count == 0) |
| 74 | + { |
| 75 | + schema.Properties = null; |
| 76 | + } |
| 77 | + |
| 78 | + return schema; |
| 79 | + } |
| 80 | + |
| 81 | + // Hàm trả về kiểu openapi, schema của items (nếu array) và danh sách enum nếu có |
| 82 | + private static (string type, ResponseSchema? itemsSchema, List<string>? enumValues) GetOpenApiType(Type type, HashSet<Type> processedTypes) |
| 83 | + { |
| 84 | + List<string>? enumValues = null; |
| 85 | + ResponseSchema? itemsSchema = null; |
| 86 | + |
| 87 | + if (type.IsEnum) |
| 88 | + { |
| 89 | + enumValues = Enum.GetNames(type).ToList(); |
| 90 | + return ("string", null, enumValues); |
| 91 | + } |
| 92 | + |
| 93 | + if (type == typeof(string)) |
| 94 | + return ("string", null, null); |
| 95 | + if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(sbyte)) |
| 96 | + return ("integer", null, null); |
| 97 | + if (type == typeof(float) || type == typeof(double) || type == typeof(decimal)) |
| 98 | + return ("number", null, null); |
| 99 | + if (type == typeof(bool)) |
| 100 | + return ("boolean", null, null); |
| 101 | + |
| 102 | + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) |
| 103 | + { |
| 104 | + var itemType = type.GetGenericArguments()[0]; |
| 105 | + var (itemOpenApiType, innerItemsSchema, innerEnumValues) = GetOpenApiType(itemType, processedTypes); |
| 106 | + itemsSchema = new ResponseSchema(itemOpenApiType); |
| 107 | + if (innerEnumValues != null) |
| 108 | + { |
| 109 | + itemsSchema.Enum = innerEnumValues; |
| 110 | + } |
| 111 | + // Nếu item là object (và không phải primitive hay string) thì chuyển đổi nested object |
| 112 | + if (itemOpenApiType == "object" && !itemType.IsPrimitive && itemType != typeof(string)) |
| 113 | + { |
| 114 | + itemsSchema = ConvertToSchema(itemType, processedTypes); |
| 115 | + } |
| 116 | + return ("array", itemsSchema, null); |
| 117 | + } |
| 118 | + |
| 119 | + return ("object", null, null); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments