Skip to content

Commit

Permalink
support additionalProperties (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison authored Sep 5, 2024
1 parent c4e5d3b commit 1719a2f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
44 changes: 44 additions & 0 deletions checker/check_response_property_type_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,47 @@ func TestResponseSchemaTypeMultiCheck(t *testing.T) {
OperationId: "createOneGroup",
}, errs[0])
}

// BC: changing an additionalResponse property schema type from integer to string is breaking
func TestResponseAdditionalPropertyTypeChangedCheck(t *testing.T) {
s1, err := open("../data/additional-properties/base.yaml")
require.NoError(t, err)
s2, err := open("../data/additional-properties/revision.yaml")
require.NoError(t, err)

d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.ResponsePropertyTypeChangedCheck), d, osm, checker.ERR)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.ResponsePropertyTypeChangedId,
Args: []any{"/additionalProperties/property1", utils.StringList{"integer"}, "", utils.StringList{"string"}, "", "200"},
Level: checker.ERR,
Operation: "GET",
Path: "/value",
Source: load.NewSource("../data/additional-properties/revision.yaml"),
OperationId: "get_value",
}, errs[0])
}

// BC: changing an embedded additionalResponse property schema type from integer to string is breaking
func TestResponseEmbeddedAdditionalPropertyTypeChangedCheck(t *testing.T) {
s1, err := open("../data/additional-properties/embedded-base.yaml")
require.NoError(t, err)
s2, err := open("../data/additional-properties/embedded-revision.yaml")
require.NoError(t, err)

d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.ResponsePropertyTypeChangedCheck), d, osm, checker.ERR)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.ResponsePropertyTypeChangedId,
Args: []any{"composite-property/additionalProperties/property1", utils.StringList{"integer"}, "", utils.StringList{"string"}, "", "200"},
Level: checker.ERR,
Operation: "GET",
Path: "/value",
Source: load.NewSource("../data/additional-properties/embedded-revision.yaml"),
OperationId: "get_value",
}, errs[0])
}
4 changes: 4 additions & 0 deletions checker/checks-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func processModifiedPropertiesDiff(propertyPath string, propertyName string, sch
processModifiedPropertiesDiff(propertyPath, i, v, schemaDiff, processor)
}
}

if schemaDiff.AdditionalPropertiesDiff != nil {
processModifiedPropertiesDiff(fmt.Sprintf("%s/additionalProperties", propertyPath), "", schemaDiff.AdditionalPropertiesDiff, schemaDiff, processor)
}
}

func CheckAddedPropertiesDiff(schemaDiff *diff.SchemaDiff, processor func(propertyPath string, propertyName string, propertyItem *openapi3.Schema, propertyParentDiff *diff.SchemaDiff)) {
Expand Down
28 changes: 28 additions & 0 deletions data/additional-properties/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.1
info:
title: Test API
version: v1
components:
schemas:
Schema1:
properties:
property1:
title: Value
type: integer
type: object
Schema2:
additionalProperties:
"$ref": "#/components/schemas/Schema1"
title: Composite
type: object
paths:
"/value":
get:
operationId: get_value
responses:
'200':
content:
application/json:
schema:
"$ref": "#/components/schemas/Schema2"
description: Successful Response
31 changes: 31 additions & 0 deletions data/additional-properties/embedded-base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.1
info:
title: Test API
version: v1
components:
schemas:
Schema1:
properties:
property1:
title: Value
type: integer
type: object
Schema2:
properties:
composite-property:
additionalProperties:
"$ref": "#/components/schemas/Schema1"
title: Composite
type: object
type: object
paths:
"/value":
get:
operationId: get_value
responses:
'200':
content:
application/json:
schema:
"$ref": "#/components/schemas/Schema2"
description: Successful Response
31 changes: 31 additions & 0 deletions data/additional-properties/embedded-revision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.1
info:
title: Test API
version: v1
components:
schemas:
Schema1:
properties:
property1:
title: Value
type: string
type: object
Schema2:
properties:
composite-property:
additionalProperties:
"$ref": "#/components/schemas/Schema1"
title: Composite
type: object
type: object
paths:
"/value":
get:
operationId: get_value
responses:
'200':
content:
application/json:
schema:
"$ref": "#/components/schemas/Schema2"
description: Successful Response
28 changes: 28 additions & 0 deletions data/additional-properties/revision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.1
info:
title: Test API
version: v1
components:
schemas:
Schema1:
properties:
property1:
title: Value
type: string
type: object
Schema2:
additionalProperties:
"$ref": "#/components/schemas/Schema1"
title: Composite
type: object
paths:
"/value":
get:
operationId: get_value
responses:
'200':
content:
application/json:
schema:
"$ref": "#/components/schemas/Schema2"
description: Successful Response
2 changes: 2 additions & 0 deletions docs/BREAKING-CHANGES-EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ These examples are automatically generated from unit tests.
[changing a response body to nullable is breaking](../checker/check_breaking_property_test.go?plain=1#L217)
[changing a response property to nullable is breaking](../checker/check_breaking_property_test.go?plain=1#L249)
[changing a response property to optional under AllOf, AnyOf or OneOf is breaking](../checker/check_breaking_property_test.go?plain=1#L660)
[changing an additionalResponse property schema type from integer to string is breaking](../checker/check_response_property_type_changed_test.go?plain=1#L150)
[changing an embedded additionalResponse property schema type from integer to string is breaking](../checker/check_response_property_type_changed_test.go?plain=1#L172)
[changing an embedded response property to nullable is breaking](../checker/check_breaking_property_test.go?plain=1#L265)
[changing an existing header param from optional to required is breaking](../checker/check_breaking_test.go?plain=1#L190)
[changing an existing path param to enum is breaking](../checker/check_breaking_property_test.go?plain=1#L185)
Expand Down

0 comments on commit 1719a2f

Please sign in to comment.