From 599a6ecff8a459a9ea39965da338e719efbc0ba1 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 16:24:07 +0000 Subject: [PATCH] feat(build): validate SDK specs before generation --- cmd/build/cmd_sdk.go | 8 +++++-- cmd/build/cmd_sdk_test.go | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 cmd/build/cmd_sdk_test.go diff --git a/cmd/build/cmd_sdk.go b/cmd/build/cmd_sdk.go index d158583..5c7d5e0 100644 --- a/cmd/build/cmd_sdk.go +++ b/cmd/build/cmd_sdk.go @@ -23,6 +23,10 @@ func runBuildSDK(ctx context.Context, specPath, lang, version string, dryRun boo return coreerr.E("build.SDK", "failed to get working directory", err) } + return runBuildSDKInDir(ctx, projectDir, specPath, lang, version, dryRun) +} + +func runBuildSDKInDir(ctx context.Context, projectDir, specPath, lang, version string, dryRun bool) error { // Load config config := sdk.DefaultConfig() if specPath != "" { @@ -40,8 +44,8 @@ func runBuildSDK(ctx context.Context, specPath, lang, version string, dryRun boo } cli.Blank() - // Detect spec - detectedSpec, err := s.DetectSpec() + // Validate the spec before generating anything. + detectedSpec, err := s.ValidateSpec(ctx) if err != nil { cli.Print("%s %v\n", buildErrorStyle.Render(i18n.T("common.label.error")), err) return err diff --git a/cmd/build/cmd_sdk_test.go b/cmd/build/cmd_sdk_test.go new file mode 100644 index 0000000..745f59a --- /dev/null +++ b/cmd/build/cmd_sdk_test.go @@ -0,0 +1,44 @@ +package buildcmd + +import ( + "context" + "testing" + + "dappco.re/go/core/build/internal/ax" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const validBuildOpenAPISpec = `openapi: "3.0.0" +info: + title: Test API + version: "1.0.0" +paths: + /health: + get: + operationId: getHealth + responses: + "200": + description: OK +` + +func TestRunBuildSDKInDir_ValidSpecDryRun_Good(t *testing.T) { + tmpDir := t.TempDir() + require.NoError(t, ax.WriteFile(ax.Join(tmpDir, "openapi.yaml"), []byte(validBuildOpenAPISpec), 0o644)) + + err := runBuildSDKInDir(context.Background(), tmpDir, "", "go", "", true) + require.NoError(t, err) +} + +func TestRunBuildSDKInDir_InvalidDocument_Bad(t *testing.T) { + tmpDir := t.TempDir() + require.NoError(t, ax.WriteFile(ax.Join(tmpDir, "openapi.yaml"), []byte(`openapi: "3.0.0" +info: + title: Test API +paths: {} +`), 0o644)) + + err := runBuildSDKInDir(context.Background(), tmpDir, "", "", "", true) + require.Error(t, err) + assert.Contains(t, err.Error(), "invalid OpenAPI spec") +}