Skip to content

Commit

Permalink
fix: populate the servers block for simple build
Browse files Browse the repository at this point in the history
- existing build implementation copies over the first server block from the spec, carry over the behaviour
  • Loading branch information
tinygrasshopper committed Oct 16, 2024
1 parent 90ba57a commit 8c18beb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
30 changes: 27 additions & 3 deletions internal/simplebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
Expand Down
41 changes: 32 additions & 9 deletions internal/simplebuild/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 8c18beb

Please sign in to comment.