diff --git a/internal/simplebuild/build.go b/internal/simplebuild/build.go index 0d94bc89..be603a5d 100644 --- a/internal/simplebuild/build.go +++ b/internal/simplebuild/build.go @@ -66,7 +66,11 @@ func Build( for _, op := range operations { op.Annotate() } - docs := operations.Build(startDate) + servers, err := FindServers(apiConfig) + if err != nil { + return err + } + docs := operations.Build(startDate, servers) writer, err := NewWriter(*apiConfig.Output, appendOutputFiles) if err != nil { return err @@ -111,6 +115,25 @@ func Build( return nil } +// FindServers returns the servers defined in the first version in the first resource of the API type. +func FindServers(api *config.API) (openapi3.Servers, error) { + if len(api.Resources) == 0 { + return nil, nil + } + paths, err := ResourceSpecFiles(api.Resources[0]) + if err != nil { + return nil, err + } + if len(paths) == 0 { + return nil, nil + } + doc, err := vervet.NewDocumentFile(paths[0]) + if err != nil { + return nil, err + } + return doc.Servers, nil +} + func sortDocsByVersionDate(docs DocSet) { slices.SortFunc(docs, func(a, b VersionedDoc) int { if a.VersionDate.Before(b.VersionDate) { @@ -144,7 +167,7 @@ type VersionedDoc struct { } type DocSet []VersionedDoc -func (ops Operations) Build(startVersion vervet.Version) DocSet { +func (ops Operations) Build(startVersion vervet.Version, servers openapi3.Servers) DocSet { filteredOps := filterBetaAndGAVersions(ops) versionDates := filteredOps.VersionDates() versionDates = filterVersionByStartDate(versionDates, startVersion.Date) @@ -157,7 +180,8 @@ func (ops Operations) Build(startVersion vervet.Version) DocSet { Title: "Snyk API", Version: "1.0.0", }, - Paths: openapi3.NewPaths(), + Paths: openapi3.NewPaths(), + Servers: servers, }, VersionDate: versionDate, } diff --git a/internal/simplebuild/build_test.go b/internal/simplebuild/build_test.go index 989045c7..1762f09d 100644 --- a/internal/simplebuild/build_test.go +++ b/internal/simplebuild/build_test.go @@ -104,11 +104,34 @@ func TestBuild(t *testing.T) { ResourceName: "foo", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) c.Assert(output[0].VersionDate, qt.Equals, time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)) c.Assert(output[0].Doc.Paths.Value("/foo").Get, qt.IsNotNil) }) + c.Run("copies servers to output", func(c *qt.C) { + ops := simplebuild.Operations{ + simplebuild.OpKey{ + Path: "/foo", + Method: "GET", + }: simplebuild.VersionSet{simplebuild.VersionedOp{ + Version: vervet.MustParseVersion("2024-01-01"), + Operation: openapi3.NewOperation(), + ResourceName: "foo", + }}, + } + expectedServer := &openapi3.Server{ + URL: "https://example.com", + } + output := ops.Build(vervet.MustParseVersion("2024-01-01"), openapi3.Servers{ + expectedServer, + }) + c.Assert(output[0].VersionDate, qt.Equals, time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)) + c.Assert(output[0].Doc.Paths.Value("/foo").Get, qt.IsNotNil) + c.Assert(output[0].Doc.Servers, qt.HasLen, 1) + c.Assert(output[0].Doc.Servers[0], qt.Equals, expectedServer) + }) + c.Run("merges operations from the same version", func(c *qt.C) { version := vervet.MustParseVersion("2024-01-01") @@ -142,7 +165,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) c.Assert(output[0].VersionDate, qt.Equals, version.Date) c.Assert(output[0].Doc.Paths.Value("/foo").Get, qt.Equals, getFoo) c.Assert(output[0].Doc.Paths.Value("/foo").Post, qt.Equals, postFoo) @@ -177,7 +200,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) inputVersions := make([]time.Time, len(versions)) for idx, in := range versions { @@ -229,7 +252,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) slices.SortFunc(output, compareDocs) @@ -279,7 +302,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) slices.SortFunc(output, compareDocs) @@ -326,7 +349,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-02")) + output := ops.Build(vervet.MustParseVersion("2024-01-02"), nil) slices.SortFunc(output, compareDocs) @@ -376,7 +399,7 @@ func TestBuild(t *testing.T) { ResourceName: "bar", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) slices.SortFunc(output, compareDocs) @@ -448,7 +471,7 @@ func TestBuild(t *testing.T) { ResourceName: "experimental-path-before-pivot-date", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) slices.SortFunc(output, compareDocs) @@ -524,7 +547,7 @@ func TestBuild(t *testing.T) { ResourceName: "wip-path-before-pivot-date", }}, } - output := ops.Build(vervet.MustParseVersion("2024-01-01")) + output := ops.Build(vervet.MustParseVersion("2024-01-01"), nil) slices.SortFunc(output, compareDocs)