Skip to content

Commit

Permalink
Improve handling of x-sunset, which can mashalled as string or a date…
Browse files Browse the repository at this point in the history
… in RFC3339 format (#198)

* Improve handling of x-sunset, which can mashalled as string or a date in RFC3339 format

* update renamed func

* remove redundant BC comment

---------

Co-authored-by: Reuven <[email protected]>
  • Loading branch information
tibulca and Reuven authored Mar 21, 2023
1 parent ada264f commit d021b87
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
17 changes: 17 additions & 0 deletions checker/checker_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,20 @@ func TestBreaking_DeprecationPathMixed(t *testing.T) {
require.Len(t, errs, 1)
require.Equal(t, "api-path-removed-before-sunset", errs[0].Id)
}

// test sunset date without double quotes, see https://github.com/Tufin/oasdiff/pull/198/files
func TestBreaking_DeprecationPathMixed_RFC3339_Sunset(t *testing.T) {

s1, err := checker.LoadOpenAPISpecInfoFromFile(getDeprecationFile("deprecated-path-mixed-rfc3339-sunset.yaml"))
require.NoError(t, err)

s2, err := checker.LoadOpenAPISpecInfoFromFile(getDeprecationFile("sunset-path.yaml"))
require.NoError(t, err)

d, osm, err := diff.GetWithOperationsSourcesMap(&diff.Config{}, s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(checker.GetDefaultChecks(), d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-path-removed-before-sunset", errs[0].Id)
}
24 changes: 24 additions & 0 deletions data/deprecation/deprecated-path-mixed-rfc3339-sunset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
info:
title: Tufin
version: 1.0.0
openapi: 3.0.3
paths:
/api/placeholder:
get:
responses:
200:
description: OK
/api/test:
get:
deprecated: true
# kin-openapi will unmarshall x-sunset as "2022-08-10T00:00:00Z"
x-sunset: 2022-08-10
responses:
200:
description: OK
post:
deprecated: true
x-sunset: "9999-08-10"
responses:
201:
description: OK
9 changes: 5 additions & 4 deletions diff/deprecation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ func GetSunsetDate(Extensions map[string]interface{}) (civil.Date, error) {
}
}

date, err := civil.ParseDate(sunset)
if err != nil {
return civil.Date{}, errors.New("failed to parse time")
if date, err := civil.ParseDate(sunset); err == nil {
return date, nil
} else if date, err := time.Parse(time.RFC3339, sunset); err == nil {
return civil.DateOf(date), nil
}

return date, nil
return civil.Date{}, errors.New("failed to parse time")
}

// SunsetAllowed checks if an element can be deleted after deprecation period
Expand Down

0 comments on commit d021b87

Please sign in to comment.