Skip to content

Commit

Permalink
Merge pull request #384 from snyk/fix/default-values
Browse files Browse the repository at this point in the history
fix: add default values to openapi docs
  • Loading branch information
jgresty authored Oct 15, 2024
2 parents 1a43f58 + 61c4ed1 commit efb5185
Show file tree
Hide file tree
Showing 10 changed files with 1,394 additions and 8 deletions.
3 changes: 3 additions & 0 deletions internal/backstage/backstage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestLoadVersionsNoApis(t *testing.T) {
- Registry_2023-06-01_experimental
- Registry_2023-06-02_experimental
- Registry_2023-06-03_experimental
- Registry_2024-10-15_ga
---
`[1:]+string(vervetAPIs))
}
Expand Down Expand Up @@ -164,6 +165,7 @@ func TestLoadVersionsSomeApis(t *testing.T) {
- Registry_2023-06-01_experimental
- Registry_2023-06-02_experimental
- Registry_2023-06-03_experimental
- Registry_2024-10-15_ga
- someOtherApi
---
`[1:]+string(vervetAPIs))
Expand Down Expand Up @@ -193,6 +195,7 @@ func TestDoesNotOutputStabilitiesAfterPivotDate(t *testing.T) {
- Registry_2023-06-01
- Registry_2023-06-02
- Registry_2023-06-03
- Registry_2024-10-15
---
`[1:]+string(vervetAPIs))
}
14 changes: 13 additions & 1 deletion internal/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"testing"
"text/template"
"time"

qt "github.com/frankban/quicktest"

Expand Down Expand Up @@ -157,7 +158,10 @@ func TestCompilerSmokePaths(t *testing.T) {
}

func assertOutputsEqual(c *qt.C, refDir, testDir string) {
err := fs.WalkDir(os.DirFS(refDir), ".", func(path string, d fs.DirEntry, err error) error {
pivotDate, err := time.Parse("2006-01-02", "2024-10-15")
c.Assert(err, qt.IsNil)

err = fs.WalkDir(os.DirFS(refDir), ".", func(path string, d fs.DirEntry, err error) error {
c.Assert(err, qt.IsNil)
if d.IsDir() {
return nil
Expand All @@ -166,6 +170,14 @@ func assertOutputsEqual(c *qt.C, refDir, testDir string) {
// only comparing compiled specs here
return nil
}
specDate, err := time.Parse("2006-01-02", path[:10])
c.Assert(err, qt.IsNil)
if !specDate.Before(pivotDate) {
// After pivot date we exclusively use simplebuild so don't emit
// specs from this pipeline
return nil
}

outputFile, err := os.ReadFile(filepath.Join(testDir, path))
c.Assert(err, qt.IsNil)
refFile, err := os.ReadFile(filepath.Join(refDir, path))
Expand Down
11 changes: 9 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 @@ -151,7 +151,14 @@ func (ops Operations) Build(startVersion vervet.Version) DocSet {
output := make(DocSet, len(versionDates))
for idx, versionDate := range versionDates {
output[idx] = VersionedDoc{
Doc: &openapi3.T{},
Doc: &openapi3.T{
OpenAPI: "3.0.3",
Info: &openapi3.Info{
Title: "Snyk API",
Version: "1.0.0",
},
Paths: openapi3.NewPaths(),
},
VersionDate: versionDate,
}
for path, spec := range filteredOps {
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: {}`
19 changes: 19 additions & 0 deletions testdata/catalog-vervet-apis-with-pivot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ spec:
owner: someone-else
definition:
$text: output/2023-06-03~experimental/spec.json
---
# Generated by vervet, DO NOT EDIT
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: Registry_2024-10-15
title: Registry 2024-10-15
annotations:
api.snyk.io/generated-by: vervet
labels:
api.snyk.io/version-date: "2024-10-15"
tags:
- 2024-10
spec:
type: openapi
lifecycle: production
owner: someone-else
definition:
$text: output/2024-10-15/spec.json
23 changes: 23 additions & 0 deletions testdata/catalog-vervet-apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,26 @@ spec:
owner: someone-else
definition:
$text: output/2023-06-03~experimental/spec.json
---
# Generated by vervet, DO NOT EDIT
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: Registry_2024-10-15_ga
title: Registry 2024-10-15 ga
annotations:
api.snyk.io/generated-by: vervet
labels:
api.snyk.io/version-date: "2024-10-15"
api.snyk.io/version-lifecycle: released
api.snyk.io/version-stability: ga
tags:
- 2024-10
- ga
- released
spec:
type: openapi
lifecycle: ga
owner: someone-else
definition:
$text: output/2024-10-15/spec.json
Loading

0 comments on commit efb5185

Please sign in to comment.