Skip to content

Commit

Permalink
handle nil OAuthFlows, fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Nov 3, 2021
1 parent df5619c commit 5684866
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions data/openapi-test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ components:
type: http
scheme: bearer
bearerFormat: JWT
AccessToken:
type: http
scheme: bearer
OAuth:
x-extension-test:
type: oauth2
Expand Down
8 changes: 8 additions & 0 deletions data/openapi-test5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ components:
type: http
scheme: bearer
bearerFormat: JWT
AccessToken:
type: oauth2
flows:
implicit:
authorizationUrl: https://example.com/api/oauth/dialog
scopes:
write:pets: modify pets in your account
read:pets": read your pets

9 changes: 7 additions & 2 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func TestSummary(t *testing.T) {
require.Equal(t, diff.SummaryDetails{0, 3, 0}, d.GetSummaryDetails(diff.HeadersDetail))
require.Equal(t, diff.SummaryDetails{0, 1, 0}, d.GetSummaryDetails(diff.RequestBodiesDetail))
require.Equal(t, diff.SummaryDetails{0, 1, 0}, d.GetSummaryDetails(diff.ResponsesDetail))
require.Equal(t, diff.SummaryDetails{0, 2, 0}, d.GetSummaryDetails(diff.SecuritySchemesDetail))
require.Equal(t, diff.SummaryDetails{0, 3, 0}, d.GetSummaryDetails(diff.SecuritySchemesDetail))
require.Equal(t, diff.SummaryDetails{}, d.GetSummaryDetails(diff.ExamplesDetail))
require.Equal(t, diff.SummaryDetails{}, d.GetSummaryDetails(diff.LinksDetail))
require.Equal(t, diff.SummaryDetails{}, d.GetSummaryDetails(diff.CallbacksDetail))
Expand All @@ -503,7 +503,7 @@ func TestSummary2(t *testing.T) {
require.Equal(t, diff.SummaryDetails{0, 3, 0}, d.GetSummaryDetails(diff.HeadersDetail))
require.Equal(t, diff.SummaryDetails{0, 0, 1}, d.GetSummaryDetails(diff.RequestBodiesDetail))
require.Equal(t, diff.SummaryDetails{0, 1, 0}, d.GetSummaryDetails(diff.ResponsesDetail))
require.Equal(t, diff.SummaryDetails{0, 1, 1}, d.GetSummaryDetails(diff.SecuritySchemesDetail))
require.Equal(t, diff.SummaryDetails{0, 2, 1}, d.GetSummaryDetails(diff.SecuritySchemesDetail))
require.Equal(t, diff.SummaryDetails{1, 0, 0}, d.GetSummaryDetails(diff.ExamplesDetail))
require.Equal(t, diff.SummaryDetails{1, 0, 0}, d.GetSummaryDetails(diff.LinksDetail))
require.Equal(t, diff.SummaryDetails{1, 0, 0}, d.GetSummaryDetails(diff.CallbacksDetail))
Expand Down Expand Up @@ -537,6 +537,11 @@ func TestModifiedSecurityRequirement(t *testing.T) {
"write:pets")
}

func TestAddedSecurityOAuthFlows(t *testing.T) {
require.True(t,
d(t, diff.NewConfig(), 1, 5).ComponentsDiff.SecuritySchemesDiff.Modified["AccessToken"].OAuthFlowsDiff.Added)
}

func TestOAS31(t *testing.T) {
loader := openapi3.NewLoader()

Expand Down
14 changes: 14 additions & 0 deletions diff/oauth_flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "github.com/getkin/kin-openapi/openapi3"

// OAuthFlowsDiff describes the changes between a pair of oauth flows objects: https://swagger.io/specification/#oauth-flows-object
type OAuthFlowsDiff struct {
Added bool `json:"added,omitempty" yaml:"added,omitempty"`
Deleted bool `json:"deleted,omitempty" yaml:"deleted,omitempty"`
ExtensionsDiff *ExtensionsDiff `json:"extensions,omitempty" yaml:"extensions,omitempty"`
ImplicitDiff *OAuthFlowDiff `json:"implicit,omitempty" yaml:"implicit,omitempty"`
PasswordDiff *OAuthFlowDiff `json:"password,omitempty" yaml:"password,omitempty"`
Expand All @@ -30,6 +32,18 @@ func getOAuthFlowsDiffInternal(config *Config, flows1, flows2 *openapi3.OAuthFlo
return nil
}

if flows1 == nil && flows2 != nil {
return &OAuthFlowsDiff{
Added: true,
}
}

if flows1 != nil && flows2 == nil {
return &OAuthFlowsDiff{
Deleted: true,
}
}

result := OAuthFlowsDiff{}

result.ExtensionsDiff = getExtensionsDiff(config, flows1.ExtensionProps, flows2.ExtensionProps)
Expand Down

0 comments on commit 5684866

Please sign in to comment.