diff --git a/ReflectorNet/src/Utils/Json/JsonSchema.Internal.cs b/ReflectorNet/src/Utils/Json/JsonSchema.Internal.cs index 2158712..8889fa1 100644 --- a/ReflectorNet/src/Utils/Json/JsonSchema.Internal.cs +++ b/ReflectorNet/src/Utils/Json/JsonSchema.Internal.cs @@ -125,6 +125,7 @@ JsonNode GenerateSchemaFromType(Reflector reflector, Type type, JsonObject defin if (defines.ContainsKey(typeId) == false) { // Add placeholder first to prevent infinite recursion + defines[typeId] = new JsonObject { [Type] = Array }; defines[typeId] = new JsonObject { [Type] = Array, diff --git a/ReflectorNet/src/Utils/Json/JsonSchema.cs b/ReflectorNet/src/Utils/Json/JsonSchema.cs index 28df10e..f620a50 100644 --- a/ReflectorNet/src/Utils/Json/JsonSchema.cs +++ b/ReflectorNet/src/Utils/Json/JsonSchema.cs @@ -145,9 +145,6 @@ public JsonNode GetSchema(Reflector reflector, Type type, JsonObject? defines = schema = schemeConvertor.GetSchema(); - if (definesNeeded && !defineContainsType) - defines[typeId] = schema; - foreach (var defType in schemeConvertor.GetDefinedTypes()) { var defTypeId = defType.GetSchemaTypeId(); @@ -182,23 +179,43 @@ public JsonNode GetSchema(Reflector reflector, Type type, JsonObject? defines = defines[defTypeId] = defSchema; } + // Add placeholder to prevent infinite recursion if (definesNeeded && !defineContainsType) - defines[typeId] = schema; + defines[typeId] = new JsonObject { [Type] = Object }; - schema = new JsonObject { [Type] = Object, [AdditionalProperties] = true }; + if (genericArgs.Length >= 2) + { + // Use the value type's schema for additionalProperties + var valueTypeSchema = GetSchema(reflector, genericArgs[1], defines); + schema = new JsonObject { [Type] = Object, [AdditionalProperties] = valueTypeSchema }; + } + else + { + schema = new JsonObject { [Type] = Object, [AdditionalProperties] = true }; + } } else { - schema = GenerateSchemaFromType(reflector, type, defines); + // Add placeholder to prevent infinite recursion + if (definesNeeded && !defineContainsType) + defines[typeId] = new JsonObject { [Type] = Object }; - if (definesNeeded && defines.Count > 0) - schema[Defs] = defines; + schema = GenerateSchemaFromType(reflector, type, defines); } // Get description from the type if available var description = TypeUtils.GetDescription(type); if (!string.IsNullOrEmpty(description)) schema[Description] = JsonValue.Create(description); + + if (definesNeeded) + { + if (!defineContainsType) + defines[typeId] = schema.DeepClone(); + + if (defines.Count > 0) + schema[Defs] = defines; + } } catch (Exception ex) {