Skip to content

Commit

Permalink
feat: validate specs before writing in simplebuild
Browse files Browse the repository at this point in the history
We had an issue where invalid specs would be written if there were no
paths. We would not check what we were writing was invalid so it would
pass all CI checks then fail at runtime when something tried to use
them.

This patch ensures that will never happen by verifying what we write is
valid.
  • Loading branch information
jgresty committed Oct 15, 2024
1 parent 4889a49 commit 61c4ed1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions internal/simplebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Build(
return err
}

err = writer.Write(doc)
err = writer.Write(ctx, doc)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +153,11 @@ func (ops Operations) Build(startVersion vervet.Version) DocSet {
output[idx] = VersionedDoc{
Doc: &openapi3.T{
OpenAPI: "3.0.3",
Paths: openapi3.NewPaths(),
Info: &openapi3.Info{
Title: "Snyk API",
Version: "1.0.0",
},
Paths: openapi3.NewPaths(),
},
VersionDate: versionDate,
}
Expand Down
10 changes: 8 additions & 2 deletions internal/simplebuild/output.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simplebuild

import (
"context"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -59,13 +60,18 @@ func NewWriter(cfg config.Output, appendOutputFiles bool) (*DocWriter, error) {

// Write writes compiled specs to a single directory in YAML and JSON formats.
// Call Finalize after to populate other directories.
func (out *DocWriter) Write(doc VersionedDoc) error {
func (out *DocWriter) Write(ctx context.Context, doc VersionedDoc) error {
err := doc.Doc.Validate(ctx)
if err != nil {
return fmt.Errorf("invalid compiled document: %w", err)
}

// We write to the first directory then copy the entire directory
// afterwards
dir := out.paths[0]

versionDir := path.Join(dir, doc.VersionDate.Format(time.DateOnly))
err := os.MkdirAll(versionDir, 0755)
err = os.MkdirAll(versionDir, 0755)
if err != nil {
return fmt.Errorf("make output directory: %w", err)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/simplebuild/output_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simplebuild

import (
"context"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -123,14 +124,15 @@ func TestDocSet_WriteOutputs(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
if tt.setup != nil {
tt.setup(t, tt.args)
}

writer, err := NewWriter(tt.args.cfg, tt.args.appendOutputFiles)
c.Assert(err, qt.IsNil)
for _, doc := range tt.docs {
err = writer.Write(doc)
err = writer.Write(ctx, doc)
c.Assert(err, qt.IsNil)
}
err = writer.Finalize()
Expand All @@ -142,4 +144,8 @@ func TestDocSet_WriteOutputs(t *testing.T) {
}

var minimalSpec = `---
openapi: 3.0.3
info:
title: minimal spec
version: 1.0.0
paths: {}`

0 comments on commit 61c4ed1

Please sign in to comment.