Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor schema
  • Loading branch information
Gijsreyn committed Nov 1, 2025
commit 1abe3a6f77cf73c6dcbf2a526886b54d8c77ebff
72 changes: 59 additions & 13 deletions src/Cli/dotnet/Commands/Tool/Dsc/ToolDscSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#nullable disable

using System.CommandLine;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli.Commands.Tool.Dsc;
Expand All @@ -20,8 +22,27 @@ public override int Execute()
{
try
{
// For now, return a basic schema
// TODO: Generate proper JSON schema when System.Text.Json.Schema is available
// Generate schema dynamically from DscToolState model
var toolProperties = new Dictionary<string, object>();
var requiredProperties = new List<string>();

foreach (var prop in typeof(DscToolState).GetProperties())
{
var jsonPropertyAttr = prop.GetCustomAttribute<JsonPropertyNameAttribute>();
if (jsonPropertyAttr == null) continue;

string propertyName = jsonPropertyAttr.Name;
var propertySchema = GetPropertySchema(prop);

toolProperties[propertyName] = propertySchema;

// packageId is required
if (propertyName == "packageId")
{
requiredProperties.Add(propertyName);
}
}

var schema = new
{
type = "object",
Expand All @@ -33,17 +54,8 @@ public override int Execute()
items = new
{
type = "object",
properties = new
{
packageId = new { type = "string" },
version = new { type = "string" },
commands = new { type = "array", items = new { type = "string" } },
scope = new { type = "string", @enum = new[] { "Global", "Local", "ToolPath" } },
toolPath = new { type = "string" },
manifestPath = new { type = "string" },
_exist = new { type = "boolean" }
},
required = new[] { "packageId" }
properties = toolProperties,
required = requiredProperties.ToArray()
}
}
}
Expand All @@ -59,4 +71,38 @@ public override int Execute()
return 1;
}
}

private static object GetPropertySchema(PropertyInfo prop)
{
var underlyingType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;

// Check for enum
if (underlyingType.IsEnum)
{
var enumValues = Enum.GetNames(underlyingType);
return new { type = "string", @enum = enumValues };
}

// Check for List<string>
if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition() == typeof(List<>))
{
var itemType = underlyingType.GetGenericArguments()[0];
if (itemType == typeof(string))
{
return new { type = "array", items = new { type = "string" } };
}
}

// Map CLR types to JSON schema types
if (underlyingType == typeof(string))
return new { type = "string" };
if (underlyingType == typeof(bool))
return new { type = "boolean" };
if (underlyingType == typeof(int) || underlyingType == typeof(long))
return new { type = "integer" };
if (underlyingType == typeof(double) || underlyingType == typeof(float))
return new { type = "number" };

return new { type = "string" };
}
}
Loading