Skip to content

Commit

Permalink
Add Component (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven Harrison authored Nov 19, 2023
1 parent 66b7f71 commit ba83377
Show file tree
Hide file tree
Showing 22 changed files with 391 additions and 132 deletions.
25 changes: 13 additions & 12 deletions BREAKING-CHANGES-EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ These examples are automatically generated from unit tests.
[adding a new global security to the API](checker/check-api-security-updated_test.go?plain=1#L11)
[adding a new media type to request body](checker/check-request-body-mediatype-updated_test.go?plain=1#L11)
[adding a new media type to response](checker/check-response-mediatype-updated_test.go?plain=1#L11)
[adding a new oauth security scope](checker/check-components-security-updated_test.go?plain=1#L95)
[adding a new oauth security scope](checker/check-components-security-updated_test.go?plain=1#L112)
[adding a new operation id](checker/check-api-operation-id-updated_test.go?plain=1#L61)
[adding a new optional request property](checker/check-request-property-updated_test.go?plain=1#L64)
[adding a new required request property](checker/check-request-property-updated_test.go?plain=1#L11)
[adding a new security component](checker/check-components-security-updated_test.go?plain=1#L55)
[adding a new security to the API endpoint](checker/check-api-security-updated_test.go?plain=1#L93)
[adding a new security component](checker/check-components-security-updated_test.go?plain=1#L74)
[adding a new security to the API endpoint](checker/check-api-security-updated_test.go?plain=1#L89)
[adding a new tag](checker/check-api-tag-updated_test.go?plain=1#L11)
[adding a non-success response status](checker/check-response-status-updated_test.go?plain=1#L37)
[adding a required property to response body is detected](checker/check-response-required-property-updated_test.go?plain=1#L11)
[adding a required write-only property to response body is detected](checker/check-response-required-property-updated_test.go?plain=1#L59)
[adding a security scope from an API global security](checker/check-api-security-updated_test.go?plain=1#L72)
[adding a security scope to an API endpoint security](checker/check-api-security-updated_test.go?plain=1#L162)
[adding a security scope from an API global security](checker/check-api-security-updated_test.go?plain=1#L69)
[adding a security scope to an API endpoint security](checker/check-api-security-updated_test.go?plain=1#L158)
[adding a success response status](checker/check-response-status-updated_test.go?plain=1#L11)
[adding an enum value to a response property](checker/check-response-property-enum-value-added_test.go?plain=1#L11)
[adding an enum value to a response write-only property](checker/check-response-property-enum-value-added_test.go?plain=1#L36)
Expand Down Expand Up @@ -238,7 +238,8 @@ These examples are automatically generated from unit tests.
[changing response body property default value](checker/check-response-property-default-value-changed_test.go?plain=1#L11)
[changing response property pattern](checker/check-response-pattern-added-or-changed_test.go?plain=1#L11)
[changing security component oauth's url](checker/check-components-security-updated_test.go?plain=1#L11)
[changing security component type](checker/check-components-security-updated_test.go?plain=1#L33)
[changing security component token url](checker/check-components-security-updated_test.go?plain=1#L32)
[changing security component type](checker/check-components-security-updated_test.go?plain=1#L53)
[changing write-only required response property to optional](checker/check-response-property-became-optional_test.go?plain=1#L33)
[decreasing max length of request body](checker/check-request-property-max-length-updated_test.go?plain=1#L39)
[decreasing max length of request property](checker/check-request-property-max-length-updated_test.go?plain=1#L67)
Expand Down Expand Up @@ -277,16 +278,16 @@ These examples are automatically generated from unit tests.
[removing 'anyOf' schema from the response body or response body property](checker/check-response-property-any-of-updated_test.go?plain=1#L47)
[removing 'oneOf' schema from the request body or request body property](checker/check-request-property-one-of-updated_test.go?plain=1#L47)
[removing 'oneOf' schema from the response body or response body property](checker/check-response-property-one-of-updated_test.go?plain=1#L57)
[removing a global security from the API](checker/check-api-security-updated_test.go?plain=1#L31)
[removing a global security from the API](checker/check-api-security-updated_test.go?plain=1#L30)
[removing a new media type to response](checker/check-response-mediatype-updated_test.go?plain=1#L34)
[removing a new oauth security scope](checker/check-components-security-updated_test.go?plain=1#L117)
[removing a new security component](checker/check-components-security-updated_test.go?plain=1#L75)
[removing a new security to the API endpoint](checker/check-api-security-updated_test.go?plain=1#L116)
[removing a new oauth security scope](checker/check-components-security-updated_test.go?plain=1#L133)
[removing a new security component](checker/check-components-security-updated_test.go?plain=1#L93)
[removing a new security to the API endpoint](checker/check-api-security-updated_test.go?plain=1#L112)
[removing a non-success response status](checker/check-response-status-updated_test.go?plain=1#L63)
[removing a required request property](checker/check-request-property-updated_test.go?plain=1#L87)
[removing a required write-only property that was required in response body is detected](checker/check-response-required-property-updated_test.go?plain=1#L85)
[removing a security scope from an API endpoint security](checker/check-api-security-updated_test.go?plain=1#L139)
[removing a security scope from an API global security](checker/check-api-security-updated_test.go?plain=1#L51)
[removing a security scope from an API endpoint security](checker/check-api-security-updated_test.go?plain=1#L135)
[removing a security scope from an API global security](checker/check-api-security-updated_test.go?plain=1#L49)
[removing an enum value from a response property](checker/check-response-property-enum-value-removed_test.go?plain=1#L11)
[removing an enum value from a response write-only property](checker/check-response-property-enum-value-removed_test.go?plain=1#L35)
[removing an enum value from request parameter](checker/check-request-parameter-enum-value-updated_test.go?plain=1#L11)
Expand Down
52 changes: 52 additions & 0 deletions checker/api_change_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package checker_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
)

var apiChange = checker.ApiChange{
Id: "id",
Text: "text",
Comment: "comment",
Level: checker.ERR,
Operation: "GET",
OperationId: "123",
Path: "/test",
Source: "source",
SourceFile: "sourceFile",
SourceLine: 1,
SourceLineEnd: 2,
SourceColumn: 3,
SourceColumnEnd: 4,
}

func TestApiChange(t *testing.T) {
require.Equal(t, "id", apiChange.GetId())
require.Equal(t, "text", apiChange.GetText())
require.Equal(t, "comment", apiChange.GetComment())
require.Equal(t, checker.ERR, apiChange.GetLevel())
require.Equal(t, "GET", apiChange.GetOperation())
require.Equal(t, "123", apiChange.GetOperationId())
require.Equal(t, "/test", apiChange.GetPath())
require.Equal(t, "sourceFile", apiChange.GetSourceFile())
require.Equal(t, 1, apiChange.GetSourceLine())
require.Equal(t, 2, apiChange.GetSourceLineEnd())
require.Equal(t, 3, apiChange.GetSourceColumn())
require.Equal(t, 4, apiChange.GetSourceColumnEnd())
require.Equal(t, "error at source, in API GET /test text [id]. comment", apiChange.LocalizedError(checker.NewDefaultLocalizer()))
require.Equal(t, "error at source, in API GET /test text [id]. comment", apiChange.Error())
}

func TestApiChange_MatchIgnore(t *testing.T) {
require.True(t, apiChange.MatchIgnore("/test", "error at source, in api get /test text [id]. comment"))
}

func TestApiChange_PrettyPiped(t *testing.T) {
piped := true
save := checker.SetPipedOutput(&piped)
defer checker.SetPipedOutput(save)
require.Equal(t, "error at source, in API GET /test text [id]. comment", apiChange.PrettyErrorText(checker.NewDefaultLocalizer()))
}
17 changes: 17 additions & 0 deletions checker/api_change_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build unix

package checker_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
)

func TestApiChange_PrettyNotPipedUnix(t *testing.T) {
piped := false
save := checker.SetPipedOutput(&piped)
defer checker.SetPipedOutput(save)
require.Equal(t, "\x1b[31merror\x1b[0m\t[\x1b[33mid\x1b[0m] at source\t\n\tin API \x1b[32mGET\x1b[0m \x1b[32m/test\x1b[0m\n\t\ttext\n\t\tcomment", apiChange.PrettyErrorText(checker.NewDefaultLocalizer()))
}
15 changes: 15 additions & 0 deletions checker/api_change_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package checker_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
)

func TestApiChange_PrettyNotPipedWindows(t *testing.T) {
piped := false
save := checker.SetPipedOutput(&piped)
defer checker.SetPipedOutput(save)
require.Equal(t, "error\t[id] at source\t\n\tin API GET /test\n\t\ttext\n\t\tcomment", apiChange.PrettyErrorText(checker.NewDefaultLocalizer()))
}
16 changes: 6 additions & 10 deletions checker/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@ var changes = checker.Changes{
checker.ApiChange{
Id: "api-deleted",
Text: "API deleted",
Comment: "",
Level: checker.ERR,
Operation: "GET",
Path: "/test",
},
checker.ApiChange{
Id: "api-added",
Text: "API added",
Comment: "",
Level: checker.INFO,
Operation: "GET",
Path: "/test",
},
checker.ComponentChange{
Id: "component-added",
Text: "component added",
Comment: "",
Level: checker.INFO,
Id: "component-added",
Text: "component added",
Level: checker.INFO,
},
checker.SecurityChange{
Id: "security-added",
Text: "security added",
Comment: "",
Level: checker.INFO,
Id: "security-added",
Text: "security added",
Level: checker.INFO,
},
}

Expand Down
28 changes: 12 additions & 16 deletions checker/check-api-security-updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,34 @@ func checkGlobalSecurity(diffReport *diff.Diff, operationsSources *diff.Operatio

for _, addedSecurity := range diffReport.SecurityDiff.Added {
result = append(result, SecurityChange{
Id: APIGlobalSecurityAddedCheckId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityAddedCheckId, ColorizedValue(addedSecurity)),
Source: "",
Id: APIGlobalSecurityAddedCheckId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityAddedCheckId, ColorizedValue(addedSecurity)),
})
}

for _, removedSecurity := range diffReport.SecurityDiff.Deleted {
result = append(result, SecurityChange{
Id: APIGlobalSecurityRemovedCheckId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityRemovedCheckId, ColorizedValue(removedSecurity)),
Source: "",
Id: APIGlobalSecurityRemovedCheckId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityRemovedCheckId, ColorizedValue(removedSecurity)),
})
}

for _, updatedSecurity := range diffReport.SecurityDiff.Modified {
for securitySchemeName, updatedSecuritySchemeScopes := range updatedSecurity {
for _, addedScope := range updatedSecuritySchemeScopes.Added {
result = append(result, SecurityChange{
Id: APIGlobalSecurityScopeAddedId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityScopeAddedId, ColorizedValue(addedScope), ColorizedValue(securitySchemeName)),
Source: "",
Id: APIGlobalSecurityScopeAddedId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityScopeAddedId, ColorizedValue(addedScope), ColorizedValue(securitySchemeName)),
})
}
for _, deletedScope := range updatedSecuritySchemeScopes.Deleted {
result = append(result, SecurityChange{
Id: APIGlobalSecurityScopeRemovedId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityScopeRemovedId, ColorizedValue(deletedScope), ColorizedValue(securitySchemeName)),
Source: "",
Id: APIGlobalSecurityScopeRemovedId,
Level: INFO,
Text: config.Localize(APIGlobalSecurityScopeRemovedId, ColorizedValue(deletedScope), ColorizedValue(securitySchemeName)),
})
}
}
Expand Down
4 changes: 0 additions & 4 deletions checker/check-api-security-updated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestAPIGlobalSecurityyAdded(t *testing.T) {
Text: "the security scheme 'petstore_auth' was added to the API",
Comment: "",
Level: checker.INFO,
Source: "",
}, errs[0])
}

Expand All @@ -44,7 +43,6 @@ func TestAPIGlobalSecurityyDeleted(t *testing.T) {
Text: "the security scheme 'petstore_auth' was removed from the API",
Comment: "",
Level: checker.INFO,
Source: "",
}, errs[0])
}

Expand All @@ -65,7 +63,6 @@ func TestAPIGlobalSecurityScopeRemoved(t *testing.T) {
Text: "the security scope 'read:pets' was removed from the global security scheme 'petstore_auth'",
Comment: "",
Level: checker.INFO,
Source: "",
}, errs[0])
}

Expand All @@ -86,7 +83,6 @@ func TestAPIGlobalSecurityScopeAdded(t *testing.T) {
Text: "the security scope 'read:pets' was added to the global security scheme 'petstore_auth'",
Comment: "",
Level: checker.INFO,
Source: "",
}, errs[0])
}

Expand Down
9 changes: 5 additions & 4 deletions checker/check-components-schemas-removed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

const (
APISchemasRemovedId = "api-schema-removed"
ComponentSchemas = "schemas"
)

func APIComponentsSchemaRemovedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSourcesMap, config Config) Changes {
Expand All @@ -16,10 +17,10 @@ func APIComponentsSchemaRemovedCheck(diffReport *diff.Diff, operationsSources *d

for _, deletedSchema := range diffReport.ComponentsDiff.SchemasDiff.Deleted {
result = append(result, ComponentChange{
Id: APISchemasRemovedId,
Level: config.getLogLevel(APISchemasRemovedId, INFO),
Text: config.Localize(APISchemasRemovedId, ColorizedValue(deletedSchema)),
Source: "Components",
Id: APISchemasRemovedId,
Level: config.getLogLevel(APISchemasRemovedId, INFO),
Text: config.Localize(APISchemasRemovedId, ColorizedValue(deletedSchema)),
Component: ComponentSchemas,
})
}
return result
Expand Down
Loading

0 comments on commit ba83377

Please sign in to comment.