-
Notifications
You must be signed in to change notification settings - Fork 14
fix: Fix foascli sunset list
to print sunset endpoints with deterministic order
#804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d9220a9
c6386e1
2e0c99d
2718af1
653ea9d
55d7c7d
50098e5
9cd52d2
e3c42ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
package sunset | ||
|
||
import ( | ||
"maps" | ||
"regexp" | ||
"slices" | ||
"sort" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/tufin/oasdiff/load" | ||
) | ||
|
@@ -77,6 +82,19 @@ func teamName(op *openapi3.Operation) string { | |
return "" | ||
} | ||
|
||
// successResponseExtensions searches through a map of response objects for successful HTTP status | ||
// codes (200, 201, 202, 204) and returns the extensions from the content of the first successful | ||
// response found. | ||
// | ||
// The function prioritizes responses in the following order: 200, 201, 202, 204. For each found | ||
// response, it extracts extensions from its content using the contentExtensions helper function. | ||
// | ||
// Parameters: | ||
// - responsesMap: A map of HTTP status codes to OpenAPI response objects | ||
// | ||
// Returns: | ||
// - A map of extension names to their values from the first successful response content, | ||
// or nil if no successful responses are found or if none contain relevant extensions | ||
func successResponseExtensions(responsesMap map[string]*openapi3.ResponseRef) map[string]any { | ||
if val, ok := responsesMap["200"]; ok { | ||
return contentExtensions(val.Value.Content) | ||
|
@@ -94,9 +112,36 @@ func successResponseExtensions(responsesMap map[string]*openapi3.ResponseRef) ma | |
return nil | ||
} | ||
|
||
// contentExtensions extracts extensions from OpenAPI content objects, prioritizing content entries | ||
// with the oldest date in their keys. | ||
// | ||
// The function sorts content keys by date (in YYYY-MM-DD format) if present, with older dates taking | ||
// precedence. If multiple keys contain dates, it selects the entry with the earliest date. | ||
// | ||
// Parameters: | ||
// - content: An OpenAPI content map with media types as keys and schema objects as values | ||
// | ||
// Returns: | ||
// - A map of extension names to their values from the selected content entry, | ||
// or nil if the content map is empty or the selected entry has no extensions | ||
// | ||
// Assumption: the older version will have the earliest sunset date. | ||
func contentExtensions(content openapi3.Content) map[string]any { | ||
for _, v := range content { | ||
return v.Extensions | ||
} | ||
return nil | ||
keysContent := slices.Collect(maps.Keys(content)) | ||
// Regex to find a date in YYYY-MM-DD format. | ||
dateRegex := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: What about upcoming and preview? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Regarding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will create a follow up ticket for |
||
// we need the content of the API version with the older date. | ||
sort.Slice(keysContent, func(i, j int) bool { | ||
dateI := dateRegex.FindString(keysContent[i]) | ||
dateJ := dateRegex.FindString(keysContent[j]) | ||
|
||
// If both have dates, compare them as strings. | ||
if dateI != "" && dateJ != "" { | ||
return dateI < dateJ | ||
} | ||
// Strings with dates should come before those without. | ||
return dateI != "" | ||
}) | ||
|
||
return content[keysContent[0]].Extensions | ||
} |
Uh oh!
There was an error while loading. Please reload this page.