Skip to content

Commit

Permalink
fix: resolve highest stability from versionware
Browse files Browse the repository at this point in the history
When compiling specs we always show the highest stability level for a
given version of an endpoint. As such we need to use the same logic when
resolving handlers for dealing with that endpoint.

Before this patch it is possible that a request for a beta version will
hit the handler for the beta endpoint, but then the request would be
validated for the GA version of the same endpoint causing the request to
fail.

This aligns endpoint behaviour with what we publicly document for a
specific version.
  • Loading branch information
jgresty committed Feb 15, 2024
1 parent 5aa254e commit 705eb13
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 0 additions & 4 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ package vervet
// TimeNow is a patchable func pointer to obtain time.Now in the
// version.go file, used for mocking time in tests.
var TimeNow = &timeNow

func (vi *VersionIndex) ResolveForBuild(v Version) (Version, error) {
return vi.resolveForBuild(v)
}
2 changes: 1 addition & 1 deletion resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (rv *ResourceVersions) At(vs string) (*ResourceVersion, error) {
if err != nil {
return nil, fmt.Errorf("invalid version %q: %w", vs, err)
}
resolvedVersion, err := rv.index.resolveForBuild(v)
resolvedVersion, err := rv.index.ResolveForBuild(v)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,13 @@ func (vi *VersionIndex) resolveIndex(query time.Time) (int, error) {
return lower, nil
}

// resolveForBuild returns the most stable version effective on the query
// ResolveForBuild returns the most stable version effective on the query
// version date with respect to the given version stability. Returns
// ErrNoMatchingVersion if no version matches.
//
// Use resolveForBuild when resolving version deprecation and effective releases
// Use ResolveForBuild when resolving version deprecation and effective releases
// _within a single resource_ during the "compilation" or "collation" process.
func (vi *VersionIndex) resolveForBuild(query Version) (Version, error) {
func (vi *VersionIndex) ResolveForBuild(query Version) (Version, error) {
i, err := vi.resolveIndex(query.Date)
if err != nil {
return Version{}, err
Expand Down
2 changes: 1 addition & 1 deletion versionware/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (h *Handler) HandleErrors(errFunc VersionErrorHandler) {
// Resolve returns the resolved version and its associated http.Handler for the
// requested version.
func (h *Handler) Resolve(requested vervet.Version) (*vervet.Version, http.Handler, error) {
resolvedVersion, err := h.index.Resolve(requested)
resolvedVersion, err := h.index.ResolveForBuild(requested)
if err != nil {
return nil, nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions versionware/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func ExampleHandler() {
func TestHandler(t *testing.T) {
c := qt.New(t)
h := versionware.NewHandler([]versionware.VersionHandler{{
Version: vervet.MustParseVersion("2021-08-01~beta"),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("aug beta"))
c.Assert(err, qt.IsNil)
}),
}, {
Version: vervet.MustParseVersion("2021-10-01"),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("oct"))
Expand Down Expand Up @@ -95,6 +101,12 @@ func TestHandler(t *testing.T) {
"2021-11-05", "2021-11-01", "nov", 200,
}, {
"2023-02-05", "2021-11-01", "nov", 200,
}, {
"2021-08-01", "", "Not Found\n", 404,
}, {
"2021-08-01~beta", "2021-08-01~beta", "aug beta", 200,
}, {
"2021-09-01~beta", "2021-09-01", "sept", 200,
}}
for i, test := range tests {
c.Run(fmt.Sprintf("%d requested %s resolved %s", i, test.requested, test.resolved), func(c *qt.C) {
Expand Down

0 comments on commit 705eb13

Please sign in to comment.