-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request body media type update checker (#320)
- Loading branch information
Showing
10 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package checker | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tufin/oasdiff/diff" | ||
) | ||
|
||
const ( | ||
RequestBodyMediaTypeAdded = "request-body-media-type-added" | ||
RequestBodyMediaTypeRemoved = "request-body-media-type-removed" | ||
) | ||
|
||
func RequestBodyMediaTypeChangedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSourcesMap, config Config) Changes { | ||
result := make(Changes, 0) | ||
if diffReport.PathsDiff == nil { | ||
return result | ||
} | ||
for path, pathItem := range diffReport.PathsDiff.Modified { | ||
if pathItem.OperationsDiff == nil { | ||
continue | ||
} | ||
for operation, operationItem := range pathItem.OperationsDiff.Modified { | ||
if operationItem.RequestBodyDiff == nil || | ||
operationItem.RequestBodyDiff.ContentDiff == nil || | ||
operationItem.RequestBodyDiff.ContentDiff.MediaTypeModified == nil { | ||
continue | ||
} | ||
source := (*operationsSources)[operationItem.Revision] | ||
|
||
addedMediaTypes := operationItem.RequestBodyDiff.ContentDiff.MediaTypeAdded | ||
for _, mediaType := range addedMediaTypes { | ||
result = append(result, ApiChange{ | ||
Id: RequestBodyMediaTypeAdded, | ||
Level: INFO, | ||
Text: fmt.Sprintf(config.i18n(RequestBodyMediaTypeAdded), mediaType), | ||
Operation: operation, | ||
OperationId: operationItem.Revision.OperationID, | ||
Path: path, | ||
Source: source, | ||
}) | ||
} | ||
|
||
removedMediaTypes := operationItem.RequestBodyDiff.ContentDiff.MediaTypeDeleted | ||
for _, mediaType := range removedMediaTypes { | ||
result = append(result, ApiChange{ | ||
Id: RequestBodyMediaTypeRemoved, | ||
Level: ERR, | ||
Text: fmt.Sprintf(config.i18n(RequestBodyMediaTypeRemoved), mediaType), | ||
Operation: operation, | ||
OperationId: operationItem.Revision.OperationID, | ||
Path: path, | ||
Source: source, | ||
}) | ||
} | ||
|
||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tufin/oasdiff/checker" | ||
"github.com/tufin/oasdiff/diff" | ||
) | ||
|
||
// CL: Adding a new media type to request body | ||
func TestRequestBodyMediaTypeAdded(t *testing.T) { | ||
s1, _ := open("../data/checker/request_body_media_type_updated_base.yaml") | ||
s2, err := open("../data/checker/request_body_media_type_updated_revision.yaml") | ||
require.Empty(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyMediaTypeChangedCheck), d, osm, checker.INFO) | ||
require.Len(t, errs, 1) | ||
require.Equal(t, checker.ApiChange{ | ||
Id: "request-body-media-type-added", | ||
Text: "added the media type application/json to the request body", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/api/v1.0/groups", | ||
Source: "../data/checker/request_body_media_type_updated_revision.yaml", | ||
OperationId: "createOneGroup", | ||
}, errs[0]) | ||
} | ||
|
||
// CL: Removing media type from request body | ||
func TestRequestBodyMediaTypeRemoved(t *testing.T) { | ||
s1, _ := open("../data/checker/request_body_media_type_updated_revision.yaml") | ||
s2, err := open("../data/checker/request_body_media_type_updated_base.yaml") | ||
require.Empty(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyMediaTypeChangedCheck), d, osm, checker.INFO) | ||
require.Len(t, errs, 1) | ||
require.Equal(t, checker.ApiChange{ | ||
Id: "request-body-media-type-removed", | ||
Text: "removed the media type application/json from the request body", | ||
Comment: "", | ||
Level: checker.ERR, | ||
Operation: "POST", | ||
Path: "/api/v1.0/groups", | ||
Source: "../data/checker/request_body_media_type_updated_base.yaml", | ||
OperationId: "createOneGroup", | ||
}, errs[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: Tufin | ||
version: "2.0" | ||
servers: | ||
- url: https://localhost:9080 | ||
paths: | ||
/api/v1.0/groups: | ||
post: | ||
tags: | ||
- Group | ||
operationId: createOneGroup | ||
requestBody: | ||
content: | ||
text/plain: | ||
schema: | ||
type: string | ||
required: true | ||
responses: | ||
"200": | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/GroupView' | ||
description: OK | ||
"409": | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/GroupView' | ||
description: Conflict | ||
summary: Create One Project | ||
components: | ||
parameters: | ||
groupId: | ||
in: path | ||
name: groupId | ||
required: true | ||
schema: | ||
type: string | ||
schemas: | ||
GroupView: | ||
type: object | ||
properties: | ||
data: | ||
type: object | ||
properties: | ||
created: | ||
type: string | ||
format: date-time | ||
readOnly: true | ||
pattern: "^[a-z]+$" | ||
id: | ||
type: string | ||
readOnly: true | ||
name: | ||
type: string | ||
required: | ||
- name |
63 changes: 63 additions & 0 deletions
63
data/checker/request_body_media_type_updated_revision.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: Tufin | ||
version: "2.0" | ||
servers: | ||
- url: https://localhost:9080 | ||
paths: | ||
/api/v1.0/groups: | ||
post: | ||
tags: | ||
- Group | ||
operationId: createOneGroup | ||
requestBody: | ||
content: | ||
text/plain: | ||
schema: | ||
type: string | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/GroupView' | ||
description: Creates one project. | ||
required: true | ||
responses: | ||
"200": | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/GroupView' | ||
description: OK | ||
"409": | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/GroupView' | ||
description: Conflict | ||
summary: Create One Project | ||
components: | ||
parameters: | ||
groupId: | ||
in: path | ||
name: groupId | ||
required: true | ||
schema: | ||
type: string | ||
schemas: | ||
GroupView: | ||
type: object | ||
properties: | ||
data: | ||
type: object | ||
properties: | ||
created: | ||
type: string | ||
format: date-time | ||
readOnly: true | ||
pattern: "^[a-z]+$" | ||
id: | ||
type: string | ||
readOnly: true | ||
name: | ||
type: string | ||
required: | ||
- name |